unused old SwingJS dependencies
[jalview.git] / unused / org / json / simple / JSONValue.java
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.io.Reader;
9 import java.io.StringReader;
10 import java.io.StringWriter;
11 import java.io.Writer;
12 import java.util.Collection;
13 // import java.util.List;
14 import java.util.Map;
15
16 import org.json.simple.parser.JSONParser;
17 import org.json.simple.parser.ParseException;
18
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. 
26          * Please use 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:
33          *      org.json.simple.JSONObject,
34          *      org.json.simple.JSONArray,
35          *      java.lang.String,
36          *      java.lang.Number,
37          *      java.lang.Boolean,
38          *      null
39          * 
40          * @deprecated this method may throw an {@code Error} instead of returning
41          * {@code null}; please use {@link JSONValue#parseWithException(Reader)}
42          * instead
43          */
44         public static Object parse(Reader in){
45                 try{
46                         JSONParser parser=new JSONParser();
47                         return parser.parse(in);
48                 }
49                 catch(Exception e){
50                         return null;
51                 }
52         }
53         
54         /**
55          * Parse JSON text into java object from the given string. 
56          * Please use parseWithException() if you don't want to ignore the exception.
57          * 
58          * @see org.json.simple.parser.JSONParser#parse(Reader)
59          * @see #parseWithException(Reader)
60          * 
61          * @param s
62          * @return Instance of the following:
63          *      org.json.simple.JSONObject,
64          *      org.json.simple.JSONArray,
65          *      java.lang.String,
66          *      java.lang.Number,
67          *      java.lang.Boolean,
68          *      null
69          * 
70          * @deprecated this method may throw an {@code Error} instead of returning
71          * {@code null}; please use {@link JSONValue#parseWithException(String)}
72          * instead
73          */
74         public static Object parse(String s){
75                 StringReader in=new StringReader(s);
76                 return parse(in);
77         }
78         
79         /**
80          * Parse JSON text into java object from the input source.
81          * 
82          * @see org.json.simple.parser.JSONParser
83          * 
84          * @param in
85          * @return Instance of the following:
86          *      org.json.simple.JSONObject,
87          *      org.json.simple.JSONArray,
88          *      java.lang.String,
89          *      java.lang.Number,
90          *      java.lang.Boolean,
91          *      null
92          * 
93          * @throws IOException
94          * @throws ParseException
95          */
96         public static Object parseWithException(Reader in) throws IOException, ParseException{
97                 JSONParser parser=new JSONParser();
98                 return parser.parse(in);
99         }
100         
101         public static Object parseWithException(String s) throws ParseException{
102                 JSONParser parser=new JSONParser();
103                 return parser.parse(s);
104         }
105         
106     /**
107      * Encode an object into JSON text and write it to out.
108      * <p>
109      * If this object is a Map or a List, and it's also a JSONStreamAware or a JSONAware, JSONStreamAware or JSONAware will be considered firstly.
110      * <p>
111      * DO NOT call this method from writeJSONString(Writer) of a class that implements both JSONStreamAware and (Map or List) with 
112      * "this" as the first parameter, use JSONObject.writeJSONString(Map, Writer) or JSONArray.writeJSONString(List, Writer) instead. 
113      * 
114      * @see org.json.simple.JSONObject#writeJSONString(Map, Writer)
115      * @see org.json.simple.JSONArray#writeJSONString(List, Writer)
116      * 
117      * @param value
118      * @param writer
119      */
120         public static void writeJSONString(Object value, Writer out) throws IOException {
121                 if(value == null){
122                         out.write("null");
123                         return;
124                 }
125                 
126                 if(value instanceof String){            
127             out.write('\"');
128                         out.write(escape((String)value));
129             out.write('\"');
130                         return;
131                 }
132                 
133                 if(value instanceof Double){
134                         if(((Double)value).isInfinite() || ((Double)value).isNaN())
135                                 out.write("null");
136                         else
137                                 out.write(value.toString());
138                         return;
139                 }
140                 
141                 if(value instanceof Float){
142                         if(((Float)value).isInfinite() || ((Float)value).isNaN())
143                                 out.write("null");
144                         else
145                                 out.write(value.toString());
146                         return;
147                 }               
148                 
149                 if(value instanceof Number){
150                         out.write(value.toString());
151                         return;
152                 }
153                 
154                 if(value instanceof Boolean){
155                         out.write(value.toString());
156                         return;
157                 }
158                 
159                 if((value instanceof JSONStreamAware)){
160                         ((JSONStreamAware)value).writeJSONString(out);
161                         return;
162                 }
163                 
164                 if((value instanceof JSONAware)){
165                         out.write(((JSONAware)value).toJSONString());
166                         return;
167                 }
168                 
169                 if(value instanceof Map){
170                         JSONObject.writeJSONString((Map)value, out);
171                         return;
172                 }
173                 
174                 if(value instanceof Collection){
175                         JSONArray.writeJSONString((Collection)value, out);
176             return;
177                 }
178                 
179                 if(value instanceof byte[]){
180                         JSONArray.writeJSONString((byte[])value, out);
181                         return;
182                 }
183                 
184                 if(value instanceof short[]){
185                         JSONArray.writeJSONString((short[])value, out);
186                         return;
187                 }
188                 
189                 if(value instanceof int[]){
190                         JSONArray.writeJSONString((int[])value, out);
191                         return;
192                 }
193                 
194                 if(value instanceof long[]){
195                         JSONArray.writeJSONString((long[])value, out);
196                         return;
197                 }
198                 
199                 if(value instanceof float[]){
200                         JSONArray.writeJSONString((float[])value, out);
201                         return;
202                 }
203                 
204                 if(value instanceof double[]){
205                         JSONArray.writeJSONString((double[])value, out);
206                         return;
207                 }
208                 
209                 if(value instanceof boolean[]){
210                         JSONArray.writeJSONString((boolean[])value, out);
211                         return;
212                 }
213                 
214                 if(value instanceof char[]){
215                         JSONArray.writeJSONString((char[])value, out);
216                         return;
217                 }
218                 
219                 if(value instanceof Object[]){
220                         JSONArray.writeJSONString((Object[])value, out);
221                         return;
222                 }
223                 
224                 out.write(value.toString());
225         }
226
227         /**
228          * Convert an object to JSON text.
229          * <p>
230          * If this object is a Map or a List, and it's also a JSONAware, JSONAware will be considered firstly.
231          * <p>
232          * DO NOT call this method from toJSONString() of a class that implements both JSONAware and Map or List with 
233          * "this" as the parameter, use JSONObject.toJSONString(Map) or JSONArray.toJSONString(List) instead. 
234          * 
235          * @see org.json.simple.JSONObject#toJSONString(Map)
236          * @see org.json.simple.JSONArray#toJSONString(List)
237          * 
238          * @param value
239          * @return JSON text, or "null" if value is null or it's an NaN or an INF number.
240          */
241         public static String toJSONString(Object value){
242                 final StringWriter writer = new StringWriter();
243                 
244                 try{
245                         writeJSONString(value, writer);
246                         return writer.toString();
247                 } catch(IOException e){
248                         // This should never happen for a StringWriter
249                         throw new RuntimeException(e);
250                 }
251         }
252
253         /**
254          * Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters (U+0000 through U+001F).
255          * @param s
256          * @return
257          */
258         public static String escape(String s){
259                 if(s==null)
260                         return null;
261         StringBuffer sb = new StringBuffer();
262         escape(s, sb);
263         return sb.toString();
264     }
265
266     /**
267      * @param s - Must not be null.
268      * @param sb
269      */
270     static void escape(String s, StringBuffer sb) {
271         final int len = s.length();
272                 for(int i=0;i<len;i++){
273                         char ch=s.charAt(i);
274                         switch(ch){
275                         case '"':
276                                 sb.append("\\\"");
277                                 break;
278                         case '\\':
279                                 sb.append("\\\\");
280                                 break;
281                         case '\b':
282                                 sb.append("\\b");
283                                 break;
284                         case '\f':
285                                 sb.append("\\f");
286                                 break;
287                         case '\n':
288                                 sb.append("\\n");
289                                 break;
290                         case '\r':
291                                 sb.append("\\r");
292                                 break;
293                         case '\t':
294                                 sb.append("\\t");
295                                 break;
296                         case '/':
297                                 sb.append("\\/");
298                                 break;
299                         default:
300                 //Reference: http://www.unicode.org/versions/Unicode5.1.0/
301                                 if((ch>='\u0000' && ch<='\u001F') || (ch>='\u007F' && ch<='\u009F') || (ch>='\u2000' && ch<='\u20FF')){
302                                         String ss=Integer.toHexString(ch);
303                                         sb.append("\\u");
304                                         for(int k=0;k<4-ss.length();k++){
305                                                 sb.append('0');
306                                         }
307                                         sb.append(ss.toUpperCase());
308                                 }
309                                 else{
310                                         sb.append(ch);
311                                 }
312                         }
313                 }//for
314         }
315
316 }