comparison src/luan/modules/parsers/LuanToString.java @ 1419:59fd2e8b1b9d

stringify and json_string
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 25 Oct 2019 22:12:06 -0600
parents 9103a61e64bc
children 6a24c8b33d6b
comparison
equal deleted inserted replaced
1418:732b5de211fc 1419:59fd2e8b1b9d
9 9
10 10
11 public final class LuanToString { 11 public final class LuanToString {
12 public boolean strict = false; 12 public boolean strict = false;
13 public boolean numberTypes = false; 13 public boolean numberTypes = false;
14 public boolean compressed = false;
15
16 private String equal;
17 private String comma;
14 18
15 public String toString(Object obj) throws LuanException { 19 public String toString(Object obj) throws LuanException {
20 equal = compressed ? "=" : " = ";
21 comma = compressed ? "," : ", ";
16 StringBuilder sb = new StringBuilder(); 22 StringBuilder sb = new StringBuilder();
17 toString(obj,sb,0); 23 toString(obj,sb,0);
18 return sb.toString(); 24 return sb.toString();
19 } 25 }
20 26
51 private void toString(LuanTable tbl,StringBuilder sb,int indented) throws LuanException { 57 private void toString(LuanTable tbl,StringBuilder sb,int indented) throws LuanException {
52 List list = tbl.asList(); 58 List list = tbl.asList();
53 Map map = tbl.rawMap(); 59 Map map = tbl.rawMap();
54 sb.append( '{' ); 60 sb.append( '{' );
55 if( !list.isEmpty() ) { 61 if( !list.isEmpty() ) {
56 indent(sb,indented+1); 62 if( !compressed )
63 indent(sb,indented+1);
57 for( Object obj : list ) { 64 for( Object obj : list ) {
58 toString(obj,sb,indented+1); 65 toString(obj,sb,indented+1);
59 sb.append( ", " ); 66 sb.append( comma );
60 } 67 }
68 if( compressed )
69 sb.setLength(sb.length()-1);
61 } 70 }
71 boolean first = true;
62 for( Object obj : map.entrySet() ) { 72 for( Object obj : map.entrySet() ) {
63 Map.Entry entry = (Map.Entry)obj; 73 Map.Entry entry = (Map.Entry)obj;
64 indent(sb,indented+1); 74 if( compressed && first )
75 first = false;
76 else
77 indent(sb,indented+1);
65 toString(entry,sb,indented+1); 78 toString(entry,sb,indented+1);
66 } 79 }
67 if( !list.isEmpty() || !map.isEmpty() ) 80 if( !compressed && (!list.isEmpty() || !map.isEmpty()) )
68 indent(sb,indented); 81 indent(sb,indented);
69 sb.append( '}' ); 82 sb.append( '}' );
70 return; 83 return;
71 } 84 }
72 85
77 } else { 90 } else {
78 sb.append( '[' ); 91 sb.append( '[' );
79 toString( key, sb, indented ); 92 toString( key, sb, indented );
80 sb.append( ']' ); 93 sb.append( ']' );
81 } 94 }
82 sb.append( " = " ); 95 sb.append( equal );
83 toString( entry.getValue(), sb, indented ); 96 toString( entry.getValue(), sb, indented );
84 } 97 }
85 98
86 private void indent(StringBuilder sb,int indented) { 99 private void indent(StringBuilder sb,int indented) {
100 if( compressed ) {
101 sb.append( comma );
102 return;
103 }
87 sb.append( '\n' ); 104 sb.append( '\n' );
88 for( int i=0; i<indented; i++ ) { 105 for( int i=0; i<indented; i++ ) {
89 sb.append( '\t' ); 106 sb.append( '\t' );
90 } 107 }
91 } 108 }