diff options
Diffstat (limited to 'src/org/json/simple/JSONValue.java')
-rw-r--r-- | src/org/json/simple/JSONValue.java | 317 |
1 files changed, 317 insertions, 0 deletions
diff --git a/src/org/json/simple/JSONValue.java b/src/org/json/simple/JSONValue.java new file mode 100644 index 0000000..3d42315 --- /dev/null +++ b/src/org/json/simple/JSONValue.java | |||
@@ -0,0 +1,317 @@ | |||
1 | /* | ||
2 | * $Id: JSONValue.java,v 1.1 2006/04/15 14:37:04 platform Exp $ | ||
3 | * Created on 2006-4-15 | ||
4 | */ | ||
5 | package org.json.simple; | ||
6 | |||
7 | import java.io.IOException; | ||
8 | import java.util.Collection; | ||
9 | // import java.util.List; | ||
10 | import java.util.Map; | ||
11 | |||
12 | import org.json.simple.parser.JSONParser; | ||
13 | import org.json.simple.parser.ParseException; | ||
14 | |||
15 | import rejava.io.Reader; | ||
16 | import rejava.io.StringReader; | ||
17 | import rejava.io.StringWriter; | ||
18 | import rejava.io.Writer; | ||
19 | |||
20 | /** | ||
21 | * @author FangYidong<fangyidong@yahoo.com.cn> | ||
22 | */ | ||
23 | public class JSONValue { | ||
24 | /** | ||
25 | * Parse JSON text into java object from the input source. Please use | ||
26 | * parseWithException() if you don't want to ignore the exception. | ||
27 | * | ||
28 | * @see org.json.simple.parser.JSONParser#parse(Reader) | ||
29 | * @see #parseWithException(Reader) | ||
30 | * | ||
31 | * @param in | ||
32 | * @return Instance of the following: org.json.simple.JSONObject, | ||
33 | * org.json.simple.JSONArray, java.lang.String, java.lang.Number, | ||
34 | * java.lang.Boolean, null | ||
35 | * | ||
36 | * @deprecated this method may throw an {@code Error} instead of returning | ||
37 | * {@code null}; please use | ||
38 | * {@link JSONValue#parseWithException(Reader)} instead | ||
39 | */ | ||
40 | @Deprecated | ||
41 | public static Object parse(final Reader in) { | ||
42 | try { | ||
43 | final JSONParser parser = new JSONParser(); | ||
44 | return parser.parse(in); | ||
45 | } catch (final Exception e) { | ||
46 | return null; | ||
47 | } | ||
48 | } | ||
49 | |||
50 | /** | ||
51 | * Parse JSON text into java object from the given string. Please use | ||
52 | * parseWithException() if you don't want to ignore the exception. | ||
53 | * | ||
54 | * @see org.json.simple.parser.JSONParser#parse(Reader) | ||
55 | * @see #parseWithException(Reader) | ||
56 | * | ||
57 | * @param s | ||
58 | * @return Instance of the following: org.json.simple.JSONObject, | ||
59 | * org.json.simple.JSONArray, java.lang.String, java.lang.Number, | ||
60 | * java.lang.Boolean, null | ||
61 | * | ||
62 | * @deprecated this method may throw an {@code Error} instead of returning | ||
63 | * {@code null}; please use | ||
64 | * {@link JSONValue#parseWithException(String)} instead | ||
65 | */ | ||
66 | @Deprecated | ||
67 | public static Object parse(final String s) { | ||
68 | final StringReader in = new StringReader(s); | ||
69 | return JSONValue.parse(in); | ||
70 | } | ||
71 | |||
72 | /** | ||
73 | * Parse JSON text into java object from the input source. | ||
74 | * | ||
75 | * @see org.json.simple.parser.JSONParser | ||
76 | * | ||
77 | * @param in | ||
78 | * @return Instance of the following: org.json.simple.JSONObject, | ||
79 | * org.json.simple.JSONArray, java.lang.String, java.lang.Number, | ||
80 | * java.lang.Boolean, null | ||
81 | * | ||
82 | * @throws IOException | ||
83 | * @throws ParseException | ||
84 | */ | ||
85 | public static Object parseWithException(final Reader in) throws IOException, ParseException { | ||
86 | final JSONParser parser = new JSONParser(); | ||
87 | return parser.parse(in); | ||
88 | } | ||
89 | |||
90 | public static Object parseWithException(final String s) throws ParseException { | ||
91 | final JSONParser parser = new JSONParser(); | ||
92 | return parser.parse(s); | ||
93 | } | ||
94 | |||
95 | /** | ||
96 | * Encode an object into JSON text and write it to out. | ||
97 | * <p> | ||
98 | * If this object is a Map or a List, and it's also a JSONStreamAware or a | ||
99 | * JSONAware, JSONStreamAware or JSONAware will be considered firstly. | ||
100 | * <p> | ||
101 | * DO NOT call this method from writeJSONString(Writer) of a class that | ||
102 | * implements both JSONStreamAware and (Map or List) with "this" as the | ||
103 | * first parameter, use JSONObject.writeJSONString(Map, Writer) or | ||
104 | * JSONArray.writeJSONString(List, Writer) instead. | ||
105 | * | ||
106 | * @see org.json.simple.JSONObject#writeJSONString(Map, Writer) | ||
107 | * @see org.json.simple.JSONArray#writeJSONString(List, Writer) | ||
108 | * | ||
109 | * @param value | ||
110 | * @param writer | ||
111 | */ | ||
112 | @SuppressWarnings("rawtypes") | ||
113 | public static void writeJSONString(final Object value, final Writer out) throws IOException { | ||
114 | if (value == null) { | ||
115 | out.write("null"); | ||
116 | return; | ||
117 | } | ||
118 | |||
119 | if (value instanceof String) { | ||
120 | out.write('\"'); | ||
121 | out.write(JSONValue.escape((String) value)); | ||
122 | out.write('\"'); | ||
123 | return; | ||
124 | } | ||
125 | |||
126 | if (value instanceof Double) { | ||
127 | if (((Double) value).isInfinite() || ((Double) value).isNaN()) { | ||
128 | out.write("null"); | ||
129 | } else { | ||
130 | out.write(value.toString()); | ||
131 | } | ||
132 | return; | ||
133 | } | ||
134 | |||
135 | if (value instanceof Float) { | ||
136 | if (((Float) value).isInfinite() || ((Float) value).isNaN()) { | ||
137 | out.write("null"); | ||
138 | } else { | ||
139 | out.write(value.toString()); | ||
140 | } | ||
141 | return; | ||
142 | } | ||
143 | |||
144 | if (value instanceof Number) { | ||
145 | out.write(value.toString()); | ||
146 | return; | ||
147 | } | ||
148 | |||
149 | if (value instanceof Boolean) { | ||
150 | out.write(value.toString()); | ||
151 | return; | ||
152 | } | ||
153 | |||
154 | if ((value instanceof JSONStreamAware)) { | ||
155 | ((JSONStreamAware) value).writeJSONString(out); | ||
156 | return; | ||
157 | } | ||
158 | |||
159 | if ((value instanceof JSONAware)) { | ||
160 | out.write(((JSONAware) value).toJSONString()); | ||
161 | return; | ||
162 | } | ||
163 | |||
164 | if (value instanceof Map) { | ||
165 | JSONObject.writeJSONString((Map) value, out); | ||
166 | return; | ||
167 | } | ||
168 | |||
169 | if (value instanceof Collection) { | ||
170 | JSONArray.writeJSONString((Collection) value, out); | ||
171 | return; | ||
172 | } | ||
173 | |||
174 | if (value instanceof byte[]) { | ||
175 | JSONArray.writeJSONString((byte[]) value, out); | ||
176 | return; | ||
177 | } | ||
178 | |||
179 | if (value instanceof short[]) { | ||
180 | JSONArray.writeJSONString((short[]) value, out); | ||
181 | return; | ||
182 | } | ||
183 | |||
184 | if (value instanceof int[]) { | ||
185 | JSONArray.writeJSONString((int[]) value, out); | ||
186 | return; | ||
187 | } | ||
188 | |||
189 | if (value instanceof long[]) { | ||
190 | JSONArray.writeJSONString((long[]) value, out); | ||
191 | return; | ||
192 | } | ||
193 | |||
194 | if (value instanceof float[]) { | ||
195 | JSONArray.writeJSONString((float[]) value, out); | ||
196 | return; | ||
197 | } | ||
198 | |||
199 | if (value instanceof double[]) { | ||
200 | JSONArray.writeJSONString((double[]) value, out); | ||
201 | return; | ||
202 | } | ||
203 | |||
204 | if (value instanceof boolean[]) { | ||
205 | JSONArray.writeJSONString((boolean[]) value, out); | ||
206 | return; | ||
207 | } | ||
208 | |||
209 | if (value instanceof char[]) { | ||
210 | JSONArray.writeJSONString((char[]) value, out); | ||
211 | return; | ||
212 | } | ||
213 | |||
214 | if (value instanceof Object[]) { | ||
215 | JSONArray.writeJSONString((Object[]) value, out); | ||
216 | return; | ||
217 | } | ||
218 | |||
219 | out.write(value.toString()); | ||
220 | } | ||
221 | |||
222 | /** | ||
223 | * Convert an object to JSON text. | ||
224 | * <p> | ||
225 | * If this object is a Map or a List, and it's also a JSONAware, JSONAware | ||
226 | * will be considered firstly. | ||
227 | * <p> | ||
228 | * DO NOT call this method from toJSONString() |