annotate src/goodjava/json/JsonToString.java @ 1412:4ba0377afa3e

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 02 Oct 2019 12:25:06 -0600
parents 27efb1fcbcb5
children 59fd2e8b1b9d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1402
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1307
diff changeset
1 package goodjava.json;
720
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import java.util.List;
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
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
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7
1132
b70102bab110 add json_compressed_string()
Franklin Schmidt <fschmidt@gmail.com>
parents: 1117
diff changeset
8 public class JsonToString {
720
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9
1113
22652f4020fb add JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 1112
diff changeset
10 public static final class JsonException extends RuntimeException {
22652f4020fb add JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 1112
diff changeset
11 private JsonException(String msg) {
22652f4020fb add JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 1112
diff changeset
12 super(msg);
22652f4020fb add JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 1112
diff changeset
13 }
22652f4020fb add JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 1112
diff changeset
14 }
22652f4020fb add JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 1112
diff changeset
15
22652f4020fb add JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 1112
diff changeset
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
22652f4020fb add JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 1112
diff changeset
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
22652f4020fb add JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 1112
diff changeset
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
22652f4020fb add JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 1112
diff changeset
47 return;
22652f4020fb add JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 1112
diff changeset
48 }
22652f4020fb add JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 1112
diff changeset
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
9dca1e912658 improve web shell
Franklin Schmidt <fschmidt@gmail.com>
parents: 1132
diff changeset
52 private static void toString(final String s,StringBuilder sb) {
9dca1e912658 improve web shell
Franklin Schmidt <fschmidt@gmail.com>
parents: 1132
diff changeset
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
6c8ceca4a10b json fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1295
diff changeset
60 case '\\':
6c8ceca4a10b json fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1295
diff changeset
61 sb.append("\\\\");
6c8ceca4a10b json fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1295
diff changeset
62 break;
6c8ceca4a10b json fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1295
diff changeset
63 case '\b':
6c8ceca4a10b json fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1295
diff changeset
64 sb.append("\\b");
6c8ceca4a10b json fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1295
diff changeset
65 break;
6c8ceca4a10b json fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1295
diff changeset
66 case '\f':
6c8ceca4a10b json fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1295
diff changeset
67 sb.append("\\f");
6c8ceca4a10b json fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1295
diff changeset
68 break;
6c8ceca4a10b json fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1295
diff changeset
69 case '\n':
6c8ceca4a10b json fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1295
diff changeset
70 sb.append("\\n");
6c8ceca4a10b json fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1295
diff changeset
71 break;
6c8ceca4a10b json fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1295
diff changeset
72 case '\r':
6c8ceca4a10b json fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1295
diff changeset
73 sb.append("\\r");
6c8ceca4a10b json fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1295
diff changeset
74 break;
6c8ceca4a10b json fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1295
diff changeset
75 case '\t':
6c8ceca4a10b json fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1295
diff changeset
76 sb.append("\\t");
6c8ceca4a10b json fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1295
diff changeset
77 break;
6c8ceca4a10b json fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1295
diff changeset
78 default:
6c8ceca4a10b json fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1295
diff changeset
79 sb.append(c);
6c8ceca4a10b json fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1295
diff changeset
80 }
6c8ceca4a10b json fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1295
diff changeset
81 }
6c8ceca4a10b json fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1295
diff changeset
82 sb.append('"');
6c8ceca4a10b json fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1295
diff changeset
83 }
6c8ceca4a10b json fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1295
diff changeset
84
6c8ceca4a10b json fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1295
diff changeset
85 public static String javascriptEncode(String s) {
6c8ceca4a10b json fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1295
diff changeset
86 StringBuilder sb = new StringBuilder();
6c8ceca4a10b json fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1295
diff changeset
87 for( int i=0; i<s.length(); i++ ) {
6c8ceca4a10b json fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1295
diff changeset
88 char c = s.charAt(i);
6c8ceca4a10b json fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1295
diff changeset
89 switch(c) {
6c8ceca4a10b json fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1295
diff changeset
90 case '"':
6c8ceca4a10b json fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1295
diff changeset
91 sb.append("\\\"");
6c8ceca4a10b json fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1295
diff changeset
92 break;
6c8ceca4a10b json fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1295
diff changeset
93 case '\'': // added for javascript
1295
9dca1e912658 improve web shell
Franklin Schmidt <fschmidt@gmail.com>
parents: 1132
diff changeset
94 sb.append("\\'");
9dca1e912658 improve web shell
Franklin Schmidt <fschmidt@gmail.com>
parents: 1132
diff changeset
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
6c8ceca4a10b json fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1295
diff changeset
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
633ca24d4d6e fix JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 1115
diff changeset
122 sb.append('[');
633ca24d4d6e fix JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 1115
diff changeset
123 if( !list.isEmpty() ) {
633ca24d4d6e fix JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 1115
diff changeset
124 indent(sb,indented+1);
633ca24d4d6e fix JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 1115
diff changeset
125 toString(list.get(0),sb,indented+1);
633ca24d4d6e fix JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 1115
diff changeset
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
633ca24d4d6e fix JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 1115
diff changeset
128 toString(list.get(i),sb,indented+1);
633ca24d4d6e fix JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 1115
diff changeset
129 }
633ca24d4d6e fix JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 1115
diff changeset
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
633ca24d4d6e fix JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 1115
diff changeset
138 if( !map.isEmpty() ) {
633ca24d4d6e fix JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 1115
diff changeset
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
633ca24d4d6e fix JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 1115
diff changeset
142 while( i.hasNext() ) {
633ca24d4d6e fix JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 1115
diff changeset
143 sb.append(',');
633ca24d4d6e fix JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 1115
diff changeset
144 indent(sb,indented+1);
633ca24d4d6e fix JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 1115
diff changeset
145 toString(i.next(),sb,indented+1);
633ca24d4d6e fix JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 1115
diff changeset
146 }
633ca24d4d6e fix JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 1115
diff changeset
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
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
169 }