Mercurial Hosting > luan
comparison 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 |
comparison
equal
deleted
inserted
replaced
1401:ef1620aa99cb | 1402:27efb1fcbcb5 |
---|---|
1 package goodjava.json; | |
2 | |
3 import java.util.List; | |
4 import java.util.Map; | |
5 import java.util.Iterator; | |
6 | |
7 | |
8 public class JsonToString { | |
9 | |
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 { | |
17 StringBuilder sb = new StringBuilder(); | |
18 new JsonToString().toString(obj,sb,0); | |
19 sb.append('\n'); | |
20 return sb.toString(); | |
21 } | |
22 | |
23 public static String toCompressedString(Object obj) throws JsonException { | |
24 StringBuilder sb = new StringBuilder(); | |
25 JsonToString jts = new JsonToString() { | |
26 void indent(StringBuilder sb,int indented) {} | |
27 }; | |
28 jts.toString(obj,sb,0); | |
29 return sb.toString(); | |
30 } | |
31 | |
32 private void toString(Object obj,StringBuilder sb,int indented) throws JsonException { | |
33 if( obj == null || obj instanceof Boolean || obj instanceof Number ) { | |
34 sb.append(obj); | |
35 return; | |
36 } | |
37 if( obj instanceof String ) { | |
38 toString((String)obj,sb); | |
39 return; | |
40 } | |
41 if( obj instanceof List ) { | |
42 toString((List)obj,sb,indented); | |
43 return; | |
44 } | |
45 if( obj instanceof Map ) { | |
46 toString((Map)obj,sb,indented); | |
47 return; | |
48 } | |
49 throw new JsonException("can't handle type "+obj.getClass().getName()); | |
50 } | |
51 | |
52 private static void toString(final String s,StringBuilder sb) { | |
53 sb.append('"'); | |
54 for( int i=0; i<s.length(); i++ ) { | |
55 char c = s.charAt(i); | |
56 switch(c) { | |
57 case '"': | |
58 sb.append("\\\""); | |
59 break; | |
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 | |
94 sb.append("\\'"); | |
95 break; | |
96 case '\\': | |
97 sb.append("\\\\"); | |
98 break; | |
99 case '\b': | |
100 sb.append("\\b"); | |
101 break; | |
102 case '\f': | |
103 sb.append("\\f"); | |
104 break; | |
105 case '\n': | |
106 sb.append("\\n"); | |
107 break; | |
108 case '\r': | |
109 sb.append("\\r"); | |
110 break; | |
111 case '\t': | |
112 sb.append("\\t"); | |
113 break; | |
114 default: | |
115 sb.append(c); | |
116 } | |
117 } | |
118 return sb.toString(); | |
119 } | |
120 | |
121 private void toString(List list,StringBuilder sb,int indented) { | |
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++ ) { | |
127 sb.append(", "); | |
128 toString(list.get(i),sb,indented+1); | |
129 } | |
130 indent(sb,indented); | |
131 } | |
132 sb.append(']'); | |
133 return; | |
134 } | |
135 | |
136 private void toString(Map map,StringBuilder sb,int indented) throws JsonException { | |
137 sb.append('{'); | |
138 if( !map.isEmpty() ) { | |
139 Iterator<Map.Entry> i = map.entrySet().iterator(); | |
140 indent(sb,indented+1); | |
141 toString(i.next(),sb,indented+1); | |
142 while( i.hasNext() ) { | |
143 sb.append(','); | |
144 indent(sb,indented+1); | |
145 toString(i.next(),sb,indented+1); | |
146 } | |
147 indent(sb,indented); | |
148 } | |
149 sb.append('}'); | |
150 } | |
151 | |
152 private void toString(Map.Entry entry,StringBuilder sb,int indented) throws JsonException { | |
153 Object key = entry.getKey(); | |
154 if( !(key instanceof String) ) | |
155 throw new JsonException("table keys must be strings"); | |
156 toString((String)key,sb); | |
157 sb.append(": "); | |
158 toString(entry.getValue(),sb,indented); | |
159 } | |
160 | |
161 void indent(StringBuilder sb,int indented) { | |
162 sb.append('\n'); | |
163 for( int i=0; i<indented; i++ ) { | |
164 sb.append('\t'); | |
165 } | |
166 } | |
167 | |
168 private JsonToString() {} | |
169 } |