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