001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.json;
014
015import java.util.*;
016import java.util.concurrent.*;
017
018import org.apache.juneau.*;
019import org.apache.juneau.annotation.*;
020import org.apache.juneau.parser.*;
021
022/**
023 * Parses any valid JSON text into a POJO model.
024 *
025 * <h5 class='topic'>Media types</h5>
026 *
027 * Handles <c>Content-Type</c> types:  <bc>application/json, text/json</bc>
028 *
029 * <h5 class='topic'>Description</h5>
030 *
031 * This parser uses a state machine, which makes it very fast and efficient.  It parses JSON in about 70% of the
032 * time that it takes the built-in Java DOM parsers to parse equivalent XML.
033 *
034 * <p>
035 * This parser handles all valid JSON syntax.
036 * In addition, when strict mode is disable, the parser also handles the following:
037 * <ul class='spaced-list'>
038 *    <li>
039 *       Javascript comments (both {@code /*} and {@code //}) are ignored.
040 *    <li>
041 *       Both single and double quoted strings.
042 *    <li>
043 *       Automatically joins concatenated strings (e.g. <code><js>"aaa"</js> + <js>'bbb'</js></code>).
044 *    <li>
045 *       Unquoted attributes.
046 * </ul>
047 *
048 * <p>
049 * Also handles negative, decimal, hexadecimal, octal, and double numbers, including exponential notation.
050 *
051 * <p>
052 * This parser handles the following input, and automatically returns the corresponding Java class.
053 * <ul class='spaced-list'>
054 *    <li>
055 *       JSON objects (<js>"{...}"</js>) are converted to {@link ObjectMap ObjectMaps}.
056 *       <b>Note:</b>  If a <code><xa>_type</xa>=<xs>'xxx'</xs></code> attribute is specified on the object, then an
057 *       attempt is made to convert the object to an instance of the specified Java bean class.
058 *       See the <c>beanTypeName</c> setting on the {@link PropertyStore} for more information about parsing
059 *       beans from JSON.
060 *    <li>
061 *       JSON arrays (<js>"[...]"</js>) are converted to {@link ObjectList ObjectLists}.
062 *    <li>
063 *       JSON string literals (<js>"'xyz'"</js>) are converted to {@link String Strings}.
064 *    <li>
065 *       JSON numbers (<js>"123"</js>, including octal/hexadecimal/exponential notation) are converted to
066 *       {@link Integer Integers}, {@link Long Longs}, {@link Float Floats}, or {@link Double Doubles} depending on
067 *       whether the number is decimal, and the size of the number.
068 *    <li>
069 *       JSON booleans (<js>"false"</js>) are converted to {@link Boolean Booleans}.
070 *    <li>
071 *       JSON nulls (<js>"null"</js>) are converted to <jk>null</jk>.
072 *    <li>
073 *       Input consisting of only whitespace or JSON comments are converted to <jk>null</jk>.
074 * </ul>
075 *
076 * <p>
077 * Input can be any of the following:
078 * <ul class='spaced-list'>
079 *    <li>
080 *       <js>"{...}"</js> - Converted to a {@link ObjectMap} or an instance of a Java bean if a <xa>_type</xa>
081 *       attribute is present.
082 *    <li>
083 *       <js>"[...]"</js> - Converted to a {@link ObjectList}.
084 *    <li>
085 *       <js>"123..."</js> - Converted to a {@link Number} (either {@link Integer}, {@link Long}, {@link Float},
086 *       or {@link Double}).
087 *    <li>
088 *       <js>"true"</js>/<js>"false"</js> - Converted to a {@link Boolean}.
089 *    <li>
090 *       <js>"null"</js> - Returns <jk>null</jk>.
091 *    <li>
092 *       <js>"'xxx'"</js> - Converted to a {@link String}.
093 *    <li>
094 *       <js>"\"xxx\""</js> - Converted to a {@link String}.
095 *    <li>
096 *       <js>"'xxx' + \"yyy\""</js> - Converted to a concatenated {@link String}.
097 * </ul>
098 *
099 * <p>
100 * TIP:  If you know you're parsing a JSON object or array, it can be easier to parse it using the
101 * {@link ObjectMap#ObjectMap(CharSequence) ObjectMap(CharSequence)} or {@link ObjectList#ObjectList(CharSequence)
102 * ObjectList(CharSequence)} constructors instead of using this class.
103 * The end result should be the same.
104 */
105@ConfigurableContext
106public class JsonParser extends ReaderParser implements JsonMetaProvider, JsonCommon {
107
108   //-------------------------------------------------------------------------------------------------------------------
109   // Configurable properties
110   //-------------------------------------------------------------------------------------------------------------------
111
112   static final String PREFIX = "JsonParser";
113
114   /**
115    * Configuration property:  Validate end.
116    *
117    * <h5 class='section'>Property:</h5>
118    * <ul class='spaced-list'>
119    *    <li><b>ID:</b>  {@link org.apache.juneau.json.JsonParser#JSON_validateEnd JSON_validateEnd}
120    *    <li><b>Name:</b>  <js>"JsonParser.validateEnd.b"</js>
121    *    <li><b>Data type:</b>  <jk>boolean</jk>
122    *    <li><b>System property:</b>  <c>JsonParser.validateEnd</c>
123    *    <li><b>Environment variable:</b>  <c>JSONPARSER_VALIDATEEND</c>
124    *    <li><b>Default:</b>  <jk>false</jk>
125    *    <li><b>Session property:</b>  <jk>false</jk>
126    *    <li><b>Annotations:</b>
127    *       <ul>
128    *          <li class='ja'>{@link org.apache.juneau.json.annotation.JsonConfig#validateEnd()}
129    *       </ul>
130    *    <li><b>Methods:</b>
131    *       <ul>
132    *          <li class='jm'>{@link org.apache.juneau.json.JsonParserBuilder#validateEnd(boolean)}
133    *          <li class='jm'>{@link org.apache.juneau.json.JsonParserBuilder#validateEnd()}
134    *       </ul>
135    * </ul>
136    *
137    * <h5 class='section'>Description:</h5>
138    * <p>
139    * If <jk>true</jk>, after parsing a POJO from the input, verifies that the remaining input in
140    * the stream consists of only comments or whitespace.
141    *
142    * <h5 class='section'>Example:</h5>
143    * <p class='bcode w800'>
144    *    <jc>// Create a parser that validates that there's no garbage at the end of the input.</jc>
145    *    ReaderParser p = JsonParser.
146    *       .<jsm>create</jsm>()
147    *       .validateEnd()
148    *       .build();
149    *
150    *    <jc>// Same, but use property.</jc>
151    *    ReaderParser p = JsonParser.
152    *       .<jsm>create</jsm>()
153    *       .set(<jsf>JSON_validateEnd</jsf>, <jk>true</jk>)
154    *       .build();
155    *
156    *    <jc>// Should fail because input has multiple POJOs.</jc>
157    *    String in = <js>"{foo:'bar'}{baz:'qux'}"</js>;
158    *    MyBean myBean = p.parse(in, MyBean.<jk>class</jk>);
159    * </p>
160    */
161   public static final String JSON_validateEnd = PREFIX + ".validateEnd.b";
162
163   //-------------------------------------------------------------------------------------------------------------------
164   // Predefined instances
165   //-------------------------------------------------------------------------------------------------------------------
166
167   /** Default parser, all default settings.*/
168   public static final JsonParser DEFAULT = new JsonParser.Simple(PropertyStore.DEFAULT);
169
170   /** Default parser, all default settings.*/
171   public static final JsonParser DEFAULT_STRICT = new JsonParser.Strict(PropertyStore.DEFAULT);
172
173
174   //-------------------------------------------------------------------------------------------------------------------
175   // Predefined subclasses
176   //-------------------------------------------------------------------------------------------------------------------
177
178   /** Default parser, strict mode. */
179   public static class Strict extends JsonParser {
180
181      /**
182       * Constructor.
183       *
184       * @param ps The property store containing all the settings for this object.
185       */
186      public Strict(PropertyStore ps) {
187         super(ps.builder().set(PARSER_strict, true).set(JSON_validateEnd, true).build());
188      }
189   }
190
191   /** Default parser, simple mode. */
192   public static class Simple extends JsonParser {
193
194      /**
195       * Constructor.
196       *
197       * @param ps The property store containing all the settings for this object.
198       */
199      public Simple(PropertyStore ps) {
200         super(ps, "application/json+simple", "text/json+simple");
201      }
202   }
203
204   //-------------------------------------------------------------------------------------------------------------------
205   // Instance
206   //-------------------------------------------------------------------------------------------------------------------
207
208   private final boolean validateEnd;
209   private final Map<ClassMeta<?>,JsonClassMeta> jsonClassMetas = new ConcurrentHashMap<>();
210   private final Map<BeanPropertyMeta,JsonBeanPropertyMeta> jsonBeanPropertyMetas = new ConcurrentHashMap<>();
211
212   /**
213    * Constructor.
214    *
215    * @param ps The property store containing all the settings for this object.
216    */
217   public JsonParser(PropertyStore ps) {
218      this(ps, "application/json", "text/json");
219   }
220
221   /**
222    * Constructor.
223    *
224    * @param ps The property store containing all the settings for this object.
225    * @param consumes The list of media types that this parser consumes (e.g. <js>"application/json"</js>).
226    */
227   public JsonParser(PropertyStore ps, String...consumes) {
228      super(ps, consumes);
229      validateEnd = getBooleanProperty(JSON_validateEnd, false);
230   }
231
232   @Override /* Context */
233   public JsonParserBuilder builder() {
234      return new JsonParserBuilder(getPropertyStore());
235   }
236
237   /**
238    * Instantiates a new clean-slate {@link JsonParserBuilder} object.
239    *
240    * <p>
241    * This is equivalent to simply calling <code><jk>new</jk> JsonParserBuilder()</code>.
242    *
243    * <p>
244    * Note that this method creates a builder initialized to all default settings, whereas {@link #builder()} copies
245    * the settings of the object called on.
246    *
247    * @return A new {@link JsonParserBuilder} object.
248    */
249   public static JsonParserBuilder create() {
250      return new JsonParserBuilder();
251   }
252
253   @Override /* Parser */
254   public JsonParserSession createSession() {
255      return createSession(createDefaultSessionArgs());
256   }
257
258   @Override /* Parser */
259   public JsonParserSession createSession(ParserSessionArgs args) {
260      return new JsonParserSession(this, args);
261   }
262
263   //-----------------------------------------------------------------------------------------------------------------
264   // Extended metadata
265   //-----------------------------------------------------------------------------------------------------------------
266
267   @Override /* JsonMetaProvider */
268   public JsonClassMeta getJsonClassMeta(ClassMeta<?> cm) {
269      JsonClassMeta m = jsonClassMetas.get(cm);
270      if (m == null) {
271         m = new JsonClassMeta(cm, this);
272         jsonClassMetas.put(cm, m);
273      }
274      return m;
275   }
276
277   @Override /* JsonMetaProvider */
278   public JsonBeanPropertyMeta getJsonBeanPropertyMeta(BeanPropertyMeta bpm) {
279      if (bpm == null)
280         return JsonBeanPropertyMeta.DEFAULT;
281      JsonBeanPropertyMeta m = jsonBeanPropertyMetas.get(bpm);
282      if (m == null) {
283         m = new JsonBeanPropertyMeta(bpm.getDelegateFor(), this);
284         jsonBeanPropertyMetas.put(bpm, m);
285      }
286      return m;
287   }
288
289   //-----------------------------------------------------------------------------------------------------------------
290   // Properties
291   //-----------------------------------------------------------------------------------------------------------------
292
293   /**
294    * Configuration property:  Validate end.
295    *
296    * @see #JSON_validateEnd
297    * @return
298    *    <jk>true</jk> if after parsing a POJO from the input, verifies that the remaining input in
299    *    the stream consists of only comments or whitespace.
300    */
301   protected final boolean isValidateEnd() {
302      return validateEnd;
303   }
304
305   //-----------------------------------------------------------------------------------------------------------------
306   // Other methods
307   //-----------------------------------------------------------------------------------------------------------------
308
309   @Override /* Context */
310   public ObjectMap toMap() {
311      return super.toMap()
312         .append("JsonParser", new DefaultFilteringObjectMap());
313   }
314}