001/*******************************************************************************
002 * Copyright (c) 2015, 2016 EclipseSource.
003 *
004 * Permission is hereby granted, free of charge, to any person obtaining a copy
005 * of this software and associated documentation files (the "Software"), to deal
006 * in the Software without restriction, including without limitation the rights
007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008 * copies of the Software, and to permit persons to whom the Software is
009 * furnished to do so, subject to the following conditions:
010 *
011 * The above copyright notice and this permission notice shall be included in all
012 * copies or substantial portions of the Software.
013 *
014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020 * SOFTWARE.
021 ******************************************************************************/
022package com.restfb.json;
023
024import java.io.IOException;
025import java.io.Reader;
026
027/**
028 * This class serves as the entry point to the minimal-json API.
029 * <p>
030 * To <strong>parse</strong> a given JSON input, use the <code>parse()</code> methods like in this example:
031 * </p>
032 * 
033 * <pre>
034 * JsonObject object = Json.parse(string).asObject();
035 * </pre>
036 * <p>
037 * To <strong>create</strong> a JSON data structure to be serialized, use the methods <code>value()</code>,
038 * <code>array()</code>, and <code>object()</code>. For example, the following snippet will produce the JSON string
039 * <em>{"foo": 23, "bar": true}</em>:
040 * </p>
041 * 
042 * <pre>
043 * String string = Json.object().add("foo", 23).add("bar", true).toString();
044 * </pre>
045 * <p>
046 * To create a JSON array from a given Java array, you can use one of the <code>array()</code> methods with varargs
047 * parameters:
048 * </p>
049 * 
050 * <pre>
051 * String[] names = ...
052 * JsonArray array = Json.array(names);
053 * </pre>
054 */
055public final class Json {
056
057  // String constants
058  private static final String VALUES_IS_NULL = "values is null";
059  private static final String STRING_IS_NULL = "string is null";
060  private static final String READER_IS_NULL = "reader is null";
061  private static final String INFINITE_AND_NAN = "Infinite and NaN values not permitted in JSON";
062
063  private Json() {
064    // not meant to be instantiated
065  }
066
067  /**
068   * Represents the JSON literal <code>null</code>.
069   */
070  public static final JsonValue NULL = new JsonLiteral("null");
071
072  /**
073   * Represents the JSON literal <code>true</code>.
074   */
075  public static final JsonValue TRUE = new JsonLiteral("true");
076
077  /**
078   * Represents the JSON literal <code>false</code>.
079   */
080  public static final JsonValue FALSE = new JsonLiteral("false");
081
082  /**
083   * Returns a JsonValue instance that represents the given <code>int</code> value.
084   *
085   * @param value
086   *          the value to get a JSON representation for
087   * @return a JSON value that represents the given value
088   */
089  public static JsonValue value(int value) {
090    return new JsonNumber(Integer.toString(value, 10));
091  }
092
093  /**
094   * Returns a JsonValue instance that represents the given <code>long</code> value.
095   *
096   * @param value
097   *          the value to get a JSON representation for
098   * @return a JSON value that represents the given value
099   */
100  public static JsonValue value(long value) {
101    return new JsonNumber(Long.toString(value, 10));
102  }
103
104  /**
105   * Returns a JsonValue instance that represents the given <code>float</code> value.
106   *
107   * @param value
108   *          the value to get a JSON representation for
109   * @return a JSON value that represents the given value
110   */
111  public static JsonValue value(float value) {
112    if (Float.isInfinite(value) || Float.isNaN(value)) {
113      throw new IllegalArgumentException(INFINITE_AND_NAN);
114    }
115    return new JsonNumber(cutOffPointZero(Float.toString(value)));
116  }
117
118  /**
119   * Returns a JsonValue instance that represents the given <code>double</code> value.
120   *
121   * @param value
122   *          the value to get a JSON representation for
123   * @return a JSON value that represents the given value
124   */
125  public static JsonValue value(double value) {
126    if (Double.isInfinite(value) || Double.isNaN(value)) {
127      throw new IllegalArgumentException(INFINITE_AND_NAN);
128    }
129    return new JsonNumber(cutOffPointZero(Double.toString(value)));
130  }
131
132  /**
133   * Returns a JsonValue instance that represents the given string.
134   *
135   * @param string
136   *          the string to get a JSON representation for
137   * @return a JSON value that represents the given string
138   */
139  public static JsonValue value(String string) {
140    return string == null ? NULL : new JsonString(string);
141  }
142
143  /**
144   * Returns a JsonValue instance that represents the given <code>boolean</code> value.
145   *
146   * @param value
147   *          the value to get a JSON representation for
148   * @return a JSON value that represents the given value
149   */
150  public static JsonValue value(boolean value) {
151    return value ? TRUE : FALSE;
152  }
153
154  /**
155   * Creates a new empty JsonArray. This is equivalent to creating a new JsonArray using the constructor.
156   *
157   * @return a new empty JSON array
158   */
159  public static JsonArray array() {
160    return new JsonArray();
161  }
162
163  /**
164   * Creates a new JsonArray that contains the JSON representations of the given <code>int</code> values.
165   *
166   * @param values
167   *          the values to be included in the new JSON array
168   * @return a new JSON array that contains the given values
169   */
170  public static JsonArray array(int... values) {
171    if (values == null) {
172      throw new NullPointerException(VALUES_IS_NULL);
173    }
174    JsonArray array = new JsonArray();
175    for (int value : values) {
176      array.add(value);
177    }
178    return array;
179  }
180
181  /**
182   * Creates a new JsonArray that contains the JSON representations of the given <code>long</code> values.
183   *
184   * @param values
185   *          the values to be included in the new JSON array
186   * @return a new JSON array that contains the given values
187   */
188  public static JsonArray array(long... values) {
189    if (values == null) {
190      throw new NullPointerException(VALUES_IS_NULL);
191    }
192    JsonArray array = new JsonArray();
193    for (long value : values) {
194      array.add(value);
195    }
196    return array;
197  }
198
199  /**
200   * Creates a new JsonArray that contains the JSON representations of the given <code>float</code> values.
201   *
202   * @param values
203   *          the values to be included in the new JSON array
204   * @return a new JSON array that contains the given values
205   */
206  public static JsonArray array(float... values) {
207    if (values == null) {
208      throw new NullPointerException(VALUES_IS_NULL);
209    }
210    JsonArray array = new JsonArray();
211    for (float value : values) {
212      array.add(value);
213    }
214    return array;
215  }
216
217  /**
218   * Creates a new JsonArray that contains the JSON representations of the given <code>double</code> values.
219   *
220   * @param values
221   *          the values to be included in the new JSON array
222   * @return a new JSON array that contains the given values
223   */
224  public static JsonArray array(double... values) {
225    if (values == null) {
226      throw new NullPointerException(VALUES_IS_NULL);
227    }
228    JsonArray array = new JsonArray();
229    for (double value : values) {
230      array.add(value);
231    }
232    return array;
233  }
234
235  /**
236   * Creates a new JsonArray that contains the JSON representations of the given <code>boolean</code> values.
237   *
238   * @param values
239   *          the values to be included in the new JSON array
240   * @return a new JSON array that contains the given values
241   */
242  public static JsonArray array(boolean... values) {
243    if (values == null) {
244      throw new NullPointerException(VALUES_IS_NULL);
245    }
246    JsonArray array = new JsonArray();
247    for (boolean value : values) {
248      array.add(value);
249    }
250    return array;
251  }
252
253  /**
254   * Creates a new JsonArray that contains the JSON representations of the given strings.
255   *
256   * @param strings
257   *          the strings to be included in the new JSON array
258   * @return a new JSON array that contains the given strings
259   */
260  public static JsonArray array(String... strings) {
261    if (strings == null) {
262      throw new NullPointerException(VALUES_IS_NULL);
263    }
264    JsonArray array = new JsonArray();
265    for (String value : strings) {
266      array.add(value);
267    }
268    return array;
269  }
270
271  /**
272   * Creates a new empty JsonObject. This is equivalent to creating a new JsonObject using the constructor.
273   *
274   * @return a new empty JSON object
275   */
276  public static JsonObject object() {
277    return new JsonObject();
278  }
279
280  /**
281   * Parses the given input string as JSON. The input must contain a valid JSON value, optionally padded with
282   * whitespace.
283   *
284   * @param string
285   *          the input string, must be valid JSON
286   * @return a value that represents the parsed JSON
287   * @throws ParseException
288   *           if the input is not valid JSON
289   */
290  public static JsonValue parse(String string) {
291    if (string == null) {
292      throw new NullPointerException(STRING_IS_NULL);
293    }
294    DefaultHandler handler = new DefaultHandler();
295    new JsonParser(handler).parse(string);
296    return handler.getValue();
297  }
298
299  /**
300   * Reads the entire input from the given reader and parses it as JSON. The input must contain a
301   * valid JSON value, optionally padded with whitespace.
302   * <p>
303   * Characters are read in chunks into an input buffer. Hence, wrapping a reader in an additional
304   * <code>BufferedReader</code> likely won't improve reading performance.
305   * </p>
306   *
307   * @param reader
308   *          the reader to read the JSON value from
309   * @return a value that represents the parsed JSON
310   * @throws IOException
311   *           if an I/O error occurs in the reader
312   * @throws ParseException
313   *           if the input is not valid JSON
314   */
315  public static JsonValue parse(Reader reader) throws IOException {
316    if (reader == null) {
317      throw new NullPointerException(READER_IS_NULL);
318    }
319    DefaultHandler handler = new DefaultHandler();
320    new JsonParser(handler).parse(reader);
321    return handler.getValue();
322  }
323
324  private static String cutOffPointZero(String string) {
325    if (string.endsWith(".0")) {
326      return string.substring(0, string.length() - 2);
327    }
328    return string;
329  }
330
331  static class DefaultHandler extends JsonHandler<JsonArray, JsonObject> {
332
333    protected JsonValue value;
334
335    @Override
336    public JsonArray startArray() {
337      return new JsonArray();
338    }
339
340    @Override
341    public JsonObject startObject() {
342      return new JsonObject();
343    }
344
345    @Override
346    public void endNull() {
347      value = NULL;
348    }
349
350    @Override
351    public void endBoolean(boolean bool) {
352      value = bool ? TRUE : FALSE;
353    }
354
355    @Override
356    public void endString(String string) {
357      value = new JsonString(string);
358    }
359
360    @Override
361    public void endNumber(String string) {
362      value = new JsonNumber(string);
363    }
364
365    @Override
366    public void endArray(JsonArray array) {
367      value = array;
368    }
369
370    @Override
371    public void endObject(JsonObject object) {
372      value = object;
373    }
374
375    @Override
376    public void endArrayValue(JsonArray array) {
377      array.add(value);
378    }
379
380    @Override
381    public void endObjectValue(JsonObject object, String name) {
382      object.add(name, value);
383    }
384
385    JsonValue getValue() {
386      return value;
387    }
388
389  }
390
391}