Mercurial Hosting > luan
comparison src/luan/lib/json/JsonToString.java @ 1117:9a1aa6fc0b4e
remove json_pretty_string and make json_string somewhat pretty
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 04 Aug 2017 11:15:53 -0600 |
parents | 633ca24d4d6e |
children | b70102bab110 |
comparison
equal
deleted
inserted
replaced
1116:633ca24d4d6e | 1117:9a1aa6fc0b4e |
---|---|
13 } | 13 } |
14 } | 14 } |
15 | 15 |
16 public static String toString(Object obj) throws JsonException { | 16 public static String toString(Object obj) throws JsonException { |
17 StringBuilder sb = new StringBuilder(); | 17 StringBuilder sb = new StringBuilder(); |
18 toString(obj,sb); | 18 toString(obj,sb,0); |
19 sb.append('\n'); | |
19 return sb.toString(); | 20 return sb.toString(); |
20 } | 21 } |
21 | 22 |
22 private static void toString(Object obj,StringBuilder sb) throws JsonException { | 23 private static void toString(Object obj,StringBuilder sb,int indented) throws JsonException { |
23 if( obj == null || obj instanceof Boolean || obj instanceof Number ) { | 24 if( obj == null || obj instanceof Boolean || obj instanceof Number ) { |
24 sb.append(obj); | 25 sb.append(obj); |
25 return; | 26 return; |
26 } | 27 } |
27 if( obj instanceof String ) { | 28 if( obj instanceof String ) { |
28 toString((String)obj,sb); | 29 toString((String)obj,sb); |
29 return; | 30 return; |
30 } | 31 } |
31 if( obj instanceof List ) { | 32 if( obj instanceof List ) { |
32 toString((List)obj,sb); | 33 toString((List)obj,sb,indented); |
33 return; | 34 return; |
34 } | 35 } |
35 if( obj instanceof Map ) { | 36 if( obj instanceof Map ) { |
36 toString((Map)obj,sb); | 37 toString((Map)obj,sb,indented); |
37 return; | 38 return; |
38 } | 39 } |
39 throw new JsonException("can't handle type "+obj.getClass().getName()); | 40 throw new JsonException("can't handle type "+obj.getClass().getName()); |
40 } | 41 } |
41 | 42 |
70 } | 71 } |
71 } | 72 } |
72 sb.append('"'); | 73 sb.append('"'); |
73 } | 74 } |
74 | 75 |
75 private static void toString(List list,StringBuilder sb) { | |
76 sb.append('['); | |
77 if( !list.isEmpty() ) { | |
78 toString(list.get(0),sb); | |
79 for( int i=1; i<list.size(); i++ ) { | |
80 sb.append(','); | |
81 toString(list.get(i),sb); | |
82 } | |
83 } | |
84 sb.append(']'); | |
85 return; | |
86 } | |
87 | |
88 private static void toString(Map map,StringBuilder sb) throws JsonException { | |
89 sb.append('{'); | |
90 if( !map.isEmpty() ) { | |
91 Iterator<Map.Entry> i = map.entrySet().iterator(); | |
92 toString(i.next(),sb); | |
93 while( i.hasNext() ) { | |
94 sb.append(','); | |
95 toString(i.next(),sb); | |
96 } | |
97 } | |
98 sb.append('}'); | |
99 } | |
100 | |
101 private static void toString(Map.Entry entry,StringBuilder sb) throws JsonException { | |
102 Object key = entry.getKey(); | |
103 if( !(key instanceof String) ) | |
104 throw new JsonException("table keys must be strings"); | |
105 toString((String)key,sb); | |
106 sb.append(':'); | |
107 toString(entry.getValue(),sb); | |
108 } | |
109 | |
110 | |
111 public static String toPrettyString(Object obj) throws JsonException { | |
112 StringBuilder sb = new StringBuilder(); | |
113 toString(obj,sb,0); | |
114 sb.append('\n'); | |
115 return sb.toString(); | |
116 } | |
117 | |
118 private static void toString(Object obj,StringBuilder sb,int indented) throws JsonException { | |
119 if( obj instanceof List ) { | |
120 toString((List)obj,sb,indented); | |
121 return; | |
122 } | |
123 if( obj instanceof Map ) { | |
124 toString((Map)obj,sb,indented); | |
125 return; | |
126 } | |
127 toString(obj,sb); | |
128 } | |
129 | |
130 private static void indent(StringBuilder sb,int indented) { | |
131 sb.append('\n'); | |
132 for( int i=0; i<indented; i++ ) { | |
133 sb.append('\t'); | |
134 } | |
135 } | |
136 | |
137 private static void toString(List list,StringBuilder sb,int indented) { | 76 private static void toString(List list,StringBuilder sb,int indented) { |
138 sb.append('['); | 77 sb.append('['); |
139 if( !list.isEmpty() ) { | 78 if( !list.isEmpty() ) { |
140 indent(sb,indented+1); | 79 indent(sb,indented+1); |
141 toString(list.get(0),sb,indented+1); | 80 toString(list.get(0),sb,indented+1); |
142 for( int i=1; i<list.size(); i++ ) { | 81 for( int i=1; i<list.size(); i++ ) { |
143 sb.append(','); | 82 sb.append(", "); |
144 indent(sb,indented+1); | |
145 toString(list.get(i),sb,indented+1); | 83 toString(list.get(i),sb,indented+1); |
146 } | 84 } |
147 indent(sb,indented); | 85 indent(sb,indented); |
148 } | 86 } |
149 sb.append(']'); | 87 sb.append(']'); |
173 toString((String)key,sb); | 111 toString((String)key,sb); |
174 sb.append(": "); | 112 sb.append(": "); |
175 toString(entry.getValue(),sb,indented); | 113 toString(entry.getValue(),sb,indented); |
176 } | 114 } |
177 | 115 |
116 private static void indent(StringBuilder sb,int indented) { | |
117 sb.append('\n'); | |
118 for( int i=0; i<indented; i++ ) { | |
119 sb.append('\t'); | |
120 } | |
121 } | |
178 | 122 |
179 private JsonToString() {} // never | 123 private JsonToString() {} // never |
180 } | 124 } |