|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2015 EclipseSource. |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in all |
| 12 | + * copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | + * SOFTWARE. |
| 21 | + ******************************************************************************/ |
| 22 | +package com.eclipsesource.json; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.io.Reader; |
| 26 | + |
| 27 | + |
| 28 | +/** |
| 29 | + * This class serves as the entry point to the minimal-json API. |
| 30 | + * <p> |
| 31 | + * To <strong>parse</strong> a given JSON input, use the <code>parse()</code> methods like in this |
| 32 | + * example: |
| 33 | + * </p> |
| 34 | + * <pre> |
| 35 | + * JsonObject object = Json.parse(string).asObject(); |
| 36 | + * </pre> |
| 37 | + * <p> |
| 38 | + * To <strong>create</strong> a JSON data structure to be serialized, use the methods |
| 39 | + * <code>value()</code>, <code>array()</code>, and <code>object()</code>. For example, the following |
| 40 | + * snippet will produce the JSON string <em>{"foo": 23, "bar": true}</em>: |
| 41 | + * </p> |
| 42 | + * <pre> |
| 43 | + * String string = Json.object().add("foo", 23).add("bar", true).toString(); |
| 44 | + * </pre> |
| 45 | + * <p> |
| 46 | + * To create a JSON array from a given Java array, you can use one of the <code>array()</code> |
| 47 | + * methods with varargs parameters: |
| 48 | + * </p> |
| 49 | + * <pre> |
| 50 | + * String[] names = ... |
| 51 | + * JsonArray array = Json.array(names); |
| 52 | + * </pre> |
| 53 | + */ |
| 54 | +public final class Json { |
| 55 | + |
| 56 | + private Json() { |
| 57 | + // not meant to be instantiated |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Represents the JSON literal <code>null</code>. |
| 62 | + */ |
| 63 | + public static final JsonValue NULL = new JsonLiteral("null"); |
| 64 | + |
| 65 | + /** |
| 66 | + * Represents the JSON literal <code>true</code>. |
| 67 | + */ |
| 68 | + public static final JsonValue TRUE = new JsonLiteral("true"); |
| 69 | + |
| 70 | + /** |
| 71 | + * Represents the JSON literal <code>false</code>. |
| 72 | + */ |
| 73 | + public static final JsonValue FALSE = new JsonLiteral("false"); |
| 74 | + |
| 75 | + /** |
| 76 | + * Returns a JsonValue instance that represents the given <code>int</code> value. |
| 77 | + * |
| 78 | + * @param value |
| 79 | + * the value to get a JSON representation for |
| 80 | + * @return a JSON value that represents the given value |
| 81 | + */ |
| 82 | + public static JsonValue value(int value) { |
| 83 | + return new JsonNumber(Integer.toString(value, 10)); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Returns a JsonValue instance that represents the given <code>long</code> value. |
| 88 | + * |
| 89 | + * @param value |
| 90 | + * the value to get a JSON representation for |
| 91 | + * @return a JSON value that represents the given value |
| 92 | + */ |
| 93 | + public static JsonValue value(long value) { |
| 94 | + return new JsonNumber(Long.toString(value, 10)); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Returns a JsonValue instance that represents the given <code>float</code> value. |
| 99 | + * |
| 100 | + * @param value |
| 101 | + * the value to get a JSON representation for |
| 102 | + * @return a JSON value that represents the given value |
| 103 | + */ |
| 104 | + public static JsonValue value(float value) { |
| 105 | + if (Float.isInfinite(value) || Float.isNaN(value)) { |
| 106 | + throw new IllegalArgumentException("Infinite and NaN values not permitted in JSON"); |
| 107 | + } |
| 108 | + return new JsonNumber(cutOffPointZero(Float.toString(value))); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Returns a JsonValue instance that represents the given <code>double</code> value. |
| 113 | + * |
| 114 | + * @param value |
| 115 | + * the value to get a JSON representation for |
| 116 | + * @return a JSON value that represents the given value |
| 117 | + */ |
| 118 | + public static JsonValue value(double value) { |
| 119 | + if (Double.isInfinite(value) || Double.isNaN(value)) { |
| 120 | + throw new IllegalArgumentException("Infinite and NaN values not permitted in JSON"); |
| 121 | + } |
| 122 | + return new JsonNumber(cutOffPointZero(Double.toString(value))); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Returns a JsonValue instance that represents the given string. |
| 127 | + * |
| 128 | + * @param string |
| 129 | + * the string to get a JSON representation for |
| 130 | + * @return a JSON value that represents the given string |
| 131 | + */ |
| 132 | + public static JsonValue value(String string) { |
| 133 | + return string == null ? NULL : new JsonString(string); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Returns a JsonValue instance that represents the given <code>boolean</code> value. |
| 138 | + * |
| 139 | + * @param value |
| 140 | + * the value to get a JSON representation for |
| 141 | + * @return a JSON value that represents the given value |
| 142 | + */ |
| 143 | + public static JsonValue value(boolean value) { |
| 144 | + return value ? TRUE : FALSE; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Creates a new empty JsonArray. This is equivalent to creating a new JsonArray using the |
| 149 | + * constructor. |
| 150 | + * |
| 151 | + * @return a new empty JSON array |
| 152 | + */ |
| 153 | + public static JsonValue array() { |
| 154 | + return new JsonArray(); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Creates a new JsonArray that contains the JSON representations of the given <code>int</code> |
| 159 | + * values. |
| 160 | + * |
| 161 | + * @param values |
| 162 | + * the values to be included in the new JSON array |
| 163 | + * @return a new JSON array that contains the given values |
| 164 | + */ |
| 165 | + public static JsonArray array(int... values) { |
| 166 | + if (values == null) { |
| 167 | + throw new NullPointerException("values is null"); |
| 168 | + } |
| 169 | + JsonArray array = new JsonArray(); |
| 170 | + for (int value : values) { |
| 171 | + array.add(value); |
| 172 | + } |
| 173 | + return array; |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Creates a new JsonArray that contains the JSON representations of the given <code>long</code> |
| 178 | + * values. |
| 179 | + * |
| 180 | + * @param values |
| 181 | + * the values to be included in the new JSON array |
| 182 | + * @return a new JSON array that contains the given values |
| 183 | + */ |
| 184 | + public static JsonArray array(long... values) { |
| 185 | + if (values == null) { |
| 186 | + throw new NullPointerException("values is null"); |
| 187 | + } |
| 188 | + JsonArray array = new JsonArray(); |
| 189 | + for (long value : values) { |
| 190 | + array.add(value); |
| 191 | + } |
| 192 | + return array; |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * Creates a new JsonArray that contains the JSON representations of the given <code>float</code> |
| 197 | + * values. |
| 198 | + * |
| 199 | + * @param values |
| 200 | + * the values to be included in the new JSON array |
| 201 | + * @return a new JSON array that contains the given values |
| 202 | + */ |
| 203 | + public static JsonArray array(float... values) { |
| 204 | + if (values == null) { |
| 205 | + throw new NullPointerException("values is null"); |
| 206 | + } |
| 207 | + JsonArray array = new JsonArray(); |
| 208 | + for (float value : values) { |
| 209 | + array.add(value); |
| 210 | + } |
| 211 | + return array; |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * Creates a new JsonArray that contains the JSON representations of the given <code>double</code> |
| 216 | + * values. |
| 217 | + * |
| 218 | + * @param values |
| 219 | + * the values to be included in the new JSON array |
| 220 | + * @return a new JSON array that contains the given values |
| 221 | + */ |
| 222 | + public static JsonArray array(double... values) { |
| 223 | + if (values == null) { |
| 224 | + throw new NullPointerException("values is null"); |
| 225 | + } |
| 226 | + JsonArray array = new JsonArray(); |
| 227 | + for (double value : values) { |
| 228 | + array.add(value); |
| 229 | + } |
| 230 | + return array; |
| 231 | + } |
| 232 | + |
| 233 | + /** |
| 234 | + * Creates a new JsonArray that contains the JSON representations of the given |
| 235 | + * <code>boolean</code> values. |
| 236 | + * |
| 237 | + * @param values |
| 238 | + * the values to be included in the new JSON array |
| 239 | + * @return a new JSON array that contains the given values |
| 240 | + */ |
| 241 | + public static JsonArray array(boolean... values) { |
| 242 | + if (values == null) { |
| 243 | + throw new NullPointerException("values is null"); |
| 244 | + } |
| 245 | + JsonArray array = new JsonArray(); |
| 246 | + for (boolean value : values) { |
| 247 | + array.add(value); |
| 248 | + } |
| 249 | + return array; |
| 250 | + } |
| 251 | + |
| 252 | + /** |
| 253 | + * Creates a new JsonArray that contains the JSON representations of the given strings. |
| 254 | + * |
| 255 | + * @param strings |
| 256 | + * the strings to be included in the new JSON array |
| 257 | + * @return a new JSON array that contains the given strings |
| 258 | + */ |
| 259 | + public static JsonArray array(String... strings) { |
| 260 | + if (strings == null) { |
| 261 | + throw new NullPointerException("values is null"); |
| 262 | + } |
| 263 | + JsonArray array = new JsonArray(); |
| 264 | + for (String value : strings) { |
| 265 | + array.add(value); |
| 266 | + } |
| 267 | + return array; |
| 268 | + } |
| 269 | + |
| 270 | + /** |
| 271 | + * Creates a new empty JsonObject. This is equivalent to creating a new JsonObject using the |
| 272 | + * 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 |
| 282 | + * padded with 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 | + try { |
| 295 | + return new JsonParser(string).parse(); |
| 296 | + } catch (IOException exception) { |
| 297 | + // JsonParser does not throw IOException for String |
| 298 | + throw new RuntimeException(exception); |
| 299 | + } |
| 300 | + } |
| 301 | + |
| 302 | + /** |
| 303 | + * Reads the entire input stream from the given reader and parses it as JSON. The input must |
| 304 | + * contain a valid JSON value, optionally padded with whitespace. |
| 305 | + * <p> |
| 306 | + * Characters are read in chunks and buffered internally, therefore wrapping an existing reader in |
| 307 | + * an additional <code>BufferedReader</code> does <strong>not</strong> improve reading |
| 308 | + * performance. |
| 309 | + * </p> |
| 310 | + * |
| 311 | + * @param reader |
| 312 | + * the reader to read the JSON value from |
| 313 | + * @return a value that represents the parsed JSON |
| 314 | + * @throws IOException |
| 315 | + * if an I/O error occurs in the reader |
| 316 | + * @throws ParseException |
| 317 | + * if the input is not valid JSON |
| 318 | + */ |
| 319 | + public static JsonValue parse(Reader reader) throws IOException { |
| 320 | + if (reader == null) { |
| 321 | + throw new NullPointerException("reader is null"); |
| 322 | + } |
| 323 | + return new JsonParser( reader ).parse(); |
| 324 | + } |
| 325 | + |
| 326 | + private static String cutOffPointZero(String string) { |
| 327 | + if (string.endsWith(".0")) { |
| 328 | + return string.substring(0, string.length() - 2); |
| 329 | + } |
| 330 | + return string; |
| 331 | + } |
| 332 | + |
| 333 | +} |
0 commit comments