1 package net.minidev.json.parser;
2
3 /*
4  *    Copyright 2011 JSON-SMART authors
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 import java.io.InputStream;
19 import java.io.Reader;
20 import java.io.UnsupportedEncodingException;
21
22 import net.minidev.json.JSONValue;
23 import net.minidev.json.writer.JsonReaderI;
24
25 public class JSONParser {
26     /**
27      * allow simple quote as String quoting char
28      */

29     public final static int ACCEPT_SIMPLE_QUOTE = 1;
30     /**
31      * allow non quoted test
32      */

33     public final static int ACCEPT_NON_QUOTE = 2;
34     /**
35      * Parse NaN as Float.NaN
36      */

37     public final static int ACCEPT_NAN = 4;
38     /**
39      * Ignore control char in input text.
40      */

41     public final static int IGNORE_CONTROL_CHAR = 8;
42     /**
43      * Use int datatype to store number when it's possible.
44      * 
45      * @since 1.0.7
46      */

47     public final static int USE_INTEGER_STORAGE = 16;
48     /**
49      * Throws exception on excessive 0 leading in digits
50      * 
51      * @since 1.0.7
52      */

53     public final static int ACCEPT_LEADING_ZERO = 32;
54     /**
55      * Throws exception on useless comma in object and array
56      * 
57      * @since 1.0.8
58      */

59     public final static int ACCEPT_USELESS_COMMA = 64;
60     /**
61      * Allow Json-smart to use Double or BigDecimal to store floating point
62      * value
63      * 
64      * You may need to disable HI_PRECISION_FLOAT feature on 32bit to improve
65      * parsing performances.
66      * 
67      * @since 1.0.9
68      */

69     public final static int USE_HI_PRECISION_FLOAT = 128;
70     /**
71      * If enabled json-smart will throws exception if datas are present after
72      * the end of the Json data.
73      * 
74      * @since 1.0.9-2
75      */

76     public final static int ACCEPT_TAILLING_DATA = 256;
77     /**
78      * smart mode, fastest parsing mode. accept lots of non standard json syntax
79      * 
80      * @since 2.0.1
81      */

82     public final static int ACCEPT_TAILLING_SPACE = 512;
83     /**
84      * smart mode, fastest parsing mode. accept lots of non standard json syntax
85      * 
86      * @since 2.2.2
87      */

88     public final static int REJECT_127_CHAR = 1024;
89
90     
91     /**
92      * smart mode, fastest parsing mode. accept lots of non standard json syntax
93      * 
94      * @since 1.0.6
95      */

96     public final static int MODE_PERMISSIVE = -1;
97     /**
98      * strict RFC4627 mode.
99      * 
100      * slower than PERMISIF MODE.
101      * 
102      * @since 1.0.6
103      */

104     public final static int MODE_RFC4627 = USE_INTEGER_STORAGE | USE_HI_PRECISION_FLOAT | ACCEPT_TAILLING_SPACE;
105     /**
106      * Parse Object like json-simple
107      * 
108      * Best for an iso-bug json-simple API port.
109      * 
110      * @since 1.0.7
111      */

112     public final static int MODE_JSON_SIMPLE = ACCEPT_USELESS_COMMA | USE_HI_PRECISION_FLOAT | ACCEPT_TAILLING_DATA | ACCEPT_TAILLING_SPACE | REJECT_127_CHAR;
113     /**
114      * Strictest parsing mode
115      * 
116      * @since 2.0.1
117      */

118     public final static int MODE_STRICTEST = USE_INTEGER_STORAGE | USE_HI_PRECISION_FLOAT | REJECT_127_CHAR;
119     /**
120      * Default json-smart processing mode
121      */

122     public static int DEFAULT_PERMISSIVE_MODE = (System.getProperty("JSON_SMART_SIMPLE") != null) ? MODE_JSON_SIMPLE
123             : MODE_PERMISSIVE;
124
125     /*
126      * internal fields
127      */

128     private int mode;
129
130     private JSONParserInputStream pBinStream;
131     private JSONParserByteArray pBytes;
132     private JSONParserReader pStream;
133     private JSONParserString pString;
134
135     private JSONParserReader getPStream() {
136         if (pStream == null)
137             pStream = new JSONParserReader(mode);
138         return pStream;
139     }
140
141     /**
142      * cached construcor
143      * 
144      * @return instance of JSONParserInputStream
145      */

146     private JSONParserInputStream getPBinStream() {
147         if (pBinStream == null)
148             pBinStream = new JSONParserInputStream(mode);
149         return pBinStream;
150     }
151
152     /**
153      * cached construcor
154      * 
155      * @return instance of JSONParserString
156      */

157     private JSONParserString getPString() {
158         if (pString == null)
159             pString = new JSONParserString(mode);
160         return pString;
161     }
162
163     /**
164      * cached construcor
165      * 
166      * @return instance of JSONParserByteArray
167      */

168     private JSONParserByteArray getPBytes() {
169         if (pBytes == null)
170             pBytes = new JSONParserByteArray(mode);
171         return pBytes;
172     }
173
174     /**
175      * @deprecated prefer usage of new JSONParser(JSONParser.MODE_*)
176      */

177     public JSONParser() {
178         this.mode = DEFAULT_PERMISSIVE_MODE;
179     }
180
181     public JSONParser(int permissifMode) {
182         this.mode = permissifMode;
183     }
184
185     /**
186      * use to return Primitive Type, or String, Or JsonObject or JsonArray
187      * generated by a ContainerFactory
188      */

189     public Object parse(byte[] in) throws ParseException {
190         return getPBytes().parse(in);
191     }
192
193     /**
194      * use to return Primitive Type, or String, Or JsonObject or JsonArray
195      * generated by a ContainerFactory
196      */

197     public <T> T parse(byte[] in, JsonReaderI<T> mapper) throws ParseException {
198         return getPBytes().parse(in, mapper);
199     }
200
201     /**
202      * use to return Primitive Type, or String, Or JsonObject or JsonArray
203      * generated by a ContainerFactory
204      */

205     public <T> T parse(byte[] in, Class<T> mapTo) throws ParseException {
206         return getPBytes().parse(in, JSONValue.defaultReader.getMapper(mapTo));
207     }
208
209     /**
210      * use to return Primitive Type, or String, Or JsonObject or JsonArray
211      * generated by a ContainerFactory
212      * @throws UnsupportedEncodingException 
213      */

214     public Object parse(InputStream in) throws ParseException, UnsupportedEncodingException {
215         return getPBinStream().parse(in);
216     }
217
218     /**
219      * use to return Primitive Type, or String, Or JsonObject or JsonArray
220      * generated by a ContainerFactory
221      */

222     public <T> T parse(InputStream in, JsonReaderI<T> mapper) throws ParseException, UnsupportedEncodingException {
223         return getPBinStream().parse(in, mapper);
224     }
225
226     /**
227      * use to return Primitive Type, or String, Or JsonObject or JsonArray
228      * generated by a ContainerFactory
229      */

230     public <T> T parse(InputStream in, Class<T> mapTo) throws ParseException, UnsupportedEncodingException {
231         return getPBinStream().parse(in, JSONValue.defaultReader.getMapper(mapTo));
232     }
233
234     /**
235      * use to return Primitive Type, or String, Or JsonObject or JsonArray
236      * generated by a ContainerFactory
237      */

238     public Object parse(Reader in) throws ParseException {
239         return getPStream().parse(in);
240     }
241
242     /**
243      * use to return Primitive Type, or String, Or JsonObject or JsonArray
244      * generated by a ContainerFactory
245      */

246     public <T> T parse(Reader in, JsonReaderI<T> mapper) throws ParseException {
247         return getPStream().parse(in, mapper);
248     }
249
250     /**
251      * use to return Primitive Type, or String, Or JsonObject or JsonArray
252      * generated by a ContainerFactory
253      */

254     public <T> T parse(Reader in, Class<T> mapTo) throws ParseException {
255         return getPStream().parse(in, JSONValue.defaultReader.getMapper(mapTo));
256     }
257
258     /**
259      * use to return Primitive Type, or String, Or JsonObject or JsonArray
260      * generated by a ContainerFactory
261      */

262     public Object parse(String in) throws ParseException {
263         return getPString().parse(in);
264     }
265
266     /**
267      * use to return Primitive Type, or String, Or JsonObject or JsonArray
268      * generated by a ContainerFactory
269      */

270     public <T> T parse(String in, JsonReaderI<T> mapper) throws ParseException {
271         return getPString().parse(in, mapper);
272     }
273
274     /**
275      * use to return Primitive Type, or String, Or JsonObject or JsonArray
276      * generated by a ContainerFactory
277      */

278     public <T> T parse(String in, Class<T> mapTo) throws ParseException {
279         return getPString().parse(in, JSONValue.defaultReader.getMapper(mapTo));
280     }
281 }
282