Mercurial Hosting > luan
annotate src/goodjava/json/JsonToString.java @ 1500:f01abd6d5858
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 10 May 2020 13:06:11 -0600 |
parents | 6a24c8b33d6b |
children | f989778ca9e1 |
rev | line source |
---|---|
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1307
diff
changeset
|
1 package goodjava.json; |
720 | 2 |
3 import java.util.List; | |
4 import java.util.Map; | |
758
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
5 import java.util.Iterator; |
720 | 6 |
7 | |
1419
59fd2e8b1b9d
stringify and json_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
8 public final class JsonToString { |
59fd2e8b1b9d
stringify and json_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
9 public boolean compressed = false; |
59fd2e8b1b9d
stringify and json_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
10 |
59fd2e8b1b9d
stringify and json_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
11 private String colon; |
59fd2e8b1b9d
stringify and json_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
12 private String comma; |
720 | 13 |
1113 | 14 public static final class JsonException extends RuntimeException { |
15 private JsonException(String msg) { | |
16 super(msg); | |
17 } | |
18 } | |
19 | |
1419
59fd2e8b1b9d
stringify and json_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
20 public String toString(Object obj) throws JsonException { |
59fd2e8b1b9d
stringify and json_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
21 colon = compressed ? ":" : ": "; |
59fd2e8b1b9d
stringify and json_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
22 comma = compressed ? "," : ", "; |
758
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
23 StringBuilder sb = new StringBuilder(); |
1419
59fd2e8b1b9d
stringify and json_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
24 toString(obj,sb,0); |
59fd2e8b1b9d
stringify and json_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
25 if( !compressed ) |
59fd2e8b1b9d
stringify and json_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
26 sb.append('\n'); |
1132
b70102bab110
add json_compressed_string()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1117
diff
changeset
|
27 return sb.toString(); |
b70102bab110
add json_compressed_string()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1117
diff
changeset
|
28 } |
b70102bab110
add json_compressed_string()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1117
diff
changeset
|
29 |
b70102bab110
add json_compressed_string()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1117
diff
changeset
|
30 private void toString(Object obj,StringBuilder sb,int indented) throws JsonException { |
758
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
31 if( obj == null || obj instanceof Boolean || obj instanceof Number ) { |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
32 sb.append(obj); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
33 return; |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
34 } |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
35 if( obj instanceof String ) { |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
36 toString((String)obj,sb); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
37 return; |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
38 } |
1113 | 39 if( obj instanceof List ) { |
1117
9a1aa6fc0b4e
remove json_pretty_string and make json_string somewhat pretty
Franklin Schmidt <fschmidt@gmail.com>
parents:
1116
diff
changeset
|
40 toString((List)obj,sb,indented); |
758
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
41 return; |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
42 } |
1113 | 43 if( obj instanceof Map ) { |
1117
9a1aa6fc0b4e
remove json_pretty_string and make json_string somewhat pretty
Franklin Schmidt <fschmidt@gmail.com>
parents:
1116
diff
changeset
|
44 toString((Map)obj,sb,indented); |
1113 | 45 return; |
46 } | |
47 throw new JsonException("can't handle type "+obj.getClass().getName()); | |
758
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
48 } |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
49 |
1295 | 50 private static void toString(final String s,StringBuilder sb) { |
51 sb.append('"'); | |
758
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
52 for( int i=0; i<s.length(); i++ ) { |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
53 char c = s.charAt(i); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
54 switch(c) { |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
55 case '"': |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
56 sb.append("\\\""); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
57 break; |
1307 | 58 case '\\': |
59 sb.append("\\\\"); | |
60 break; | |
61 case '\b': | |
62 sb.append("\\b"); | |
63 break; | |
64 case '\f': | |
65 sb.append("\\f"); | |
66 break; | |
67 case '\n': | |
68 sb.append("\\n"); | |
69 break; | |
70 case '\r': | |
71 sb.append("\\r"); | |
72 break; | |
73 case '\t': | |
74 sb.append("\\t"); | |
75 break; | |
76 default: | |
77 sb.append(c); | |
78 } | |
79 } | |
80 sb.append('"'); | |
81 } | |
82 | |
83 public static String javascriptEncode(String s) { | |
84 StringBuilder sb = new StringBuilder(); | |
85 for( int i=0; i<s.length(); i++ ) { | |
86 char c = s.charAt(i); | |
87 switch(c) { | |
88 case '"': | |
89 sb.append("\\\""); | |
90 break; | |
91 case '\'': // added for javascript | |
1295 | 92 sb.append("\\'"); |
93 break; | |
758
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
94 case '\\': |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
95 sb.append("\\\\"); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
96 break; |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
97 case '\b': |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
98 sb.append("\\b"); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
99 break; |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
100 case '\f': |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
101 sb.append("\\f"); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
102 break; |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
103 case '\n': |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
104 sb.append("\\n"); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
105 break; |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
106 case '\r': |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
107 sb.append("\\r"); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
108 break; |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
109 case '\t': |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
110 sb.append("\\t"); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
111 break; |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
112 default: |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
113 sb.append(c); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
114 } |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
115 } |
1307 | 116 return sb.toString(); |
758
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
117 } |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
118 |
1132
b70102bab110
add json_compressed_string()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1117
diff
changeset
|
119 private void toString(List list,StringBuilder sb,int indented) { |
1116 | 120 sb.append('['); |
121 if( !list.isEmpty() ) { | |
122 indent(sb,indented+1); | |
123 toString(list.get(0),sb,indented+1); | |
1485 | 124 final int n = list.size(); |
125 for( int i=1; i<n; i++ ) { | |
1419
59fd2e8b1b9d
stringify and json_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
126 sb.append(comma); |
1116 | 127 toString(list.get(i),sb,indented+1); |
128 } | |
129 indent(sb,indented); | |
1115
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
130 } |
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
131 sb.append(']'); |
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
132 return; |
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
133 } |
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
134 |
1132
b70102bab110
add json_compressed_string()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1117
diff
changeset
|
135 private void toString(Map map,StringBuilder sb,int indented) throws JsonException { |
1115
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
136 sb.append('{'); |
1116 | 137 if( !map.isEmpty() ) { |
138 Iterator<Map.Entry> i = map.entrySet().iterator(); | |
1115
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
139 indent(sb,indented+1); |
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
140 toString(i.next(),sb,indented+1); |
1116 | 141 while( i.hasNext() ) { |
142 sb.append(','); | |
143 indent(sb,indented+1); | |
144 toString(i.next(),sb,indented+1); | |
145 } | |
146 indent(sb,indented); | |
1115
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
147 } |
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
148 sb.append('}'); |
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
149 } |
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
150 |
1132
b70102bab110
add json_compressed_string()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1117
diff
changeset
|
151 private void toString(Map.Entry entry,StringBuilder sb,int indented) throws JsonException { |
1115
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
152 Object key = entry.getKey(); |
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
153 if( !(key instanceof String) ) |
1477 | 154 throw new JsonException("table keys must be strings but got "+key.getClass().getSimpleName()+" ("+key+"="+entry.getValue()+")"); |
1115
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
155 toString((String)key,sb); |
1419
59fd2e8b1b9d
stringify and json_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
156 sb.append(colon); |
1115
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
157 toString(entry.getValue(),sb,indented); |
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
158 } |
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
159 |
1419
59fd2e8b1b9d
stringify and json_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
160 private void indent(StringBuilder sb,int indented) { |
59fd2e8b1b9d
stringify and json_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
161 if( compressed ) |
59fd2e8b1b9d
stringify and json_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
162 return; |
1117
9a1aa6fc0b4e
remove json_pretty_string and make json_string somewhat pretty
Franklin Schmidt <fschmidt@gmail.com>
parents:
1116
diff
changeset
|
163 sb.append('\n'); |
9a1aa6fc0b4e
remove json_pretty_string and make json_string somewhat pretty
Franklin Schmidt <fschmidt@gmail.com>
parents:
1116
diff
changeset
|
164 for( int i=0; i<indented; i++ ) { |
9a1aa6fc0b4e
remove json_pretty_string and make json_string somewhat pretty
Franklin Schmidt <fschmidt@gmail.com>
parents:
1116
diff
changeset
|
165 sb.append('\t'); |
9a1aa6fc0b4e
remove json_pretty_string and make json_string somewhat pretty
Franklin Schmidt <fschmidt@gmail.com>
parents:
1116
diff
changeset
|
166 } |
9a1aa6fc0b4e
remove json_pretty_string and make json_string somewhat pretty
Franklin Schmidt <fschmidt@gmail.com>
parents:
1116
diff
changeset
|
167 } |
1115
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
168 |
720 | 169 } |