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