Mercurial Hosting > luan
comparison src/luan/modules/parsers/LuanToString.java @ 1518:f989778ca9e1
stringify, json_string
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 19 Jun 2020 17:50:25 -0600 |
parents | 6a24c8b33d6b |
children | 0dc3be25ad20 |
comparison
equal
deleted
inserted
replaced
1517:04615093b19d | 1518:f989778ca9e1 |
---|---|
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; | 14 public boolean compressed = false; |
15 | 15 |
16 private String equal; | |
17 private String comma; | |
18 | |
19 public String toString(Object obj) throws LuanException { | 16 public String toString(Object obj) throws LuanException { |
20 equal = compressed ? "=" : " = "; | |
21 comma = compressed ? "," : ", "; | |
22 StringBuilder sb = new StringBuilder(); | 17 StringBuilder sb = new StringBuilder(); |
23 toString(obj,sb,0); | 18 toString(obj,sb,0); |
24 return sb.toString(); | 19 return sb.toString(); |
25 } | 20 } |
26 | 21 |
56 | 51 |
57 private void toString(LuanTable tbl,StringBuilder sb,int indented) throws LuanException { | 52 private void toString(LuanTable tbl,StringBuilder sb,int indented) throws LuanException { |
58 List list = tbl.asList(); | 53 List list = tbl.asList(); |
59 Map map = tbl.rawMap(); | 54 Map map = tbl.rawMap(); |
60 sb.append( '{' ); | 55 sb.append( '{' ); |
61 if( !list.isEmpty() ) { | 56 boolean first = true; |
57 for( Object obj : list ) { | |
62 if( !compressed ) | 58 if( !compressed ) |
63 indent(sb,indented+1); | 59 indent(sb,indented+1); |
64 toString(list.get(0),sb,indented+1); | 60 else if( first ) |
65 final int n = list.size(); | 61 first = false; |
66 for( int i=1; i<n; i++ ) { | 62 else |
67 sb.append(comma); | 63 sb.append( ',' ); |
68 toString(list.get(i),sb,indented+1); | 64 toString(obj,sb,indented+1); |
69 } | |
70 } | 65 } |
71 boolean first = true; | |
72 for( Object obj : map.entrySet() ) { | 66 for( Object obj : map.entrySet() ) { |
73 Map.Entry entry = (Map.Entry)obj; | 67 Map.Entry entry = (Map.Entry)obj; |
74 if( compressed && first ) | 68 if( !compressed ) |
69 indent(sb,indented+1); | |
70 else if( first ) | |
75 first = false; | 71 first = false; |
76 else | 72 else |
77 indent(sb,indented+1); | 73 sb.append( ',' ); |
78 toString(entry,sb,indented+1); | 74 toString(entry,sb,indented+1); |
79 } | 75 } |
80 if( !compressed && (!list.isEmpty() || !map.isEmpty()) ) | 76 if( !compressed && (!list.isEmpty() || !map.isEmpty()) ) |
81 indent(sb,indented); | 77 indent(sb,indented); |
82 sb.append( '}' ); | 78 sb.append( '}' ); |
90 } else { | 86 } else { |
91 sb.append( '[' ); | 87 sb.append( '[' ); |
92 toString( key, sb, indented ); | 88 toString( key, sb, indented ); |
93 sb.append( ']' ); | 89 sb.append( ']' ); |
94 } | 90 } |
95 sb.append( equal ); | 91 sb.append( compressed ? "=" : " = " ); |
96 toString( entry.getValue(), sb, indented ); | 92 toString( entry.getValue(), sb, indented ); |
97 } | 93 } |
98 | 94 |
99 private void indent(StringBuilder sb,int indented) { | 95 private void indent(StringBuilder sb,int indented) { |
100 if( compressed ) { | |
101 sb.append( comma ); | |
102 return; | |
103 } | |
104 sb.append( '\n' ); | 96 sb.append( '\n' ); |
105 for( int i=0; i<indented; i++ ) { | 97 for( int i=0; i<indented; i++ ) { |
106 sb.append( '\t' ); | 98 sb.append( '\t' ); |
107 } | 99 } |
108 } | 100 } |