| 1 | /* |
| 2 | * Copyright (c) 2011, 2021 Oracle and/or its affiliates. All rights reserved. |
| 3 | * |
| 4 | * This program and the accompanying materials are made available under the |
| 5 | * terms of the Eclipse Public License v. 2.0, which is available at |
| 6 | * http://www.eclipse.org/legal/epl-2.0. |
| 7 | * |
| 8 | * This Source Code may also be made available under the following Secondary |
| 9 | * Licenses when the conditions for such availability set forth in the |
| 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, |
| 11 | * version 2 with the GNU Classpath Exception, which is available at |
| 12 | * https://www.gnu.org/software/classpath/license.html. |
| 13 | * |
| 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| 15 | */ |
| 16 | |
| 17 | package jakarta.json; |
| 18 | |
| 19 | import java.io.InputStream; |
| 20 | import java.io.OutputStream; |
| 21 | import java.io.Reader; |
| 22 | import java.io.Writer; |
| 23 | import java.math.BigDecimal; |
| 24 | import java.math.BigInteger; |
| 25 | import java.util.Collection; |
| 26 | import java.util.Map; |
| 27 | import java.util.Optional; |
| 28 | import jakarta.json.spi.JsonProvider; |
| 29 | import jakarta.json.stream.JsonGenerator; |
| 30 | import jakarta.json.stream.JsonGeneratorFactory; |
| 31 | import jakarta.json.stream.JsonParser; |
| 32 | import jakarta.json.stream.JsonParserFactory; |
| 33 | |
| 34 | /** |
| 35 | * Factory class for creating JSON processing objects. |
| 36 | * This class provides the most commonly used methods for creating these |
| 37 | * objects and their corresponding factories. The factory classes provide |
| 38 | * all the various ways to create these objects. |
| 39 | * |
| 40 | * <p> |
| 41 | * The methods in this class locate a provider instance using the method |
| 42 | * {@link JsonProvider#provider()}. This class uses the provider instance |
| 43 | * to create JSON processing objects. |
| 44 | * |
| 45 | * <p> |
| 46 | * The following example shows how to create a JSON parser to parse |
| 47 | * an empty array: |
| 48 | * <pre> |
| 49 | * <code> |
| 50 | * StringReader reader = new StringReader("[]"); |
| 51 | * JsonParser parser = Json.createParser(reader); |
| 52 | * </code> |
| 53 | * </pre> |
| 54 | * |
| 55 | * <p> |
| 56 | * All the methods in this class are safe for use by multiple concurrent |
| 57 | * threads. |
| 58 | */ |
| 59 | public final class Json { |
| 60 | |
| 61 | /** |
| 62 | * No instantiation. |
| 63 | */ |
| 64 | private Json() { |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Creates a JSON parser from a character stream. |
| 69 | * |
| 70 | * @param reader i/o reader from which JSON is to be read |
| 71 | * @return a JSON parser |
| 72 | */ |
| 73 | public static JsonParser createParser(Reader reader) { |
| 74 | return JsonProvider.provider().createParser(reader); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Creates a JSON parser from a byte stream. |
| 79 | * The character encoding of the stream is determined as specified in |
| 80 | * <a href="http://tools.ietf.org/rfc/rfc7159.txt">RFC 7159</a>. |
| 81 | * |
| 82 | * @param in i/o stream from which JSON is to be read |
| 83 | * @throws JsonException if encoding cannot be determined |
| 84 | * or i/o error (IOException would be cause of JsonException) |
| 85 | * @return a JSON parser |
| 86 | */ |
| 87 | public static JsonParser createParser(InputStream in) { |
| 88 | return JsonProvider.provider().createParser(in); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Creates a JSON generator for writing JSON to a character stream. |
| 93 | * |
| 94 | * @param writer a i/o writer to which JSON is written |
| 95 | * @return a JSON generator |
| 96 | */ |
| 97 | public static JsonGenerator createGenerator(Writer writer) { |
| 98 | return JsonProvider.provider().createGenerator(writer); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Creates a JSON generator for writing JSON to a byte stream. |
| 103 | * |
| 104 | * @param out i/o stream to which JSON is written |
| 105 | * @return a JSON generator |
| 106 | */ |
| 107 | public static JsonGenerator createGenerator(OutputStream out) { |
| 108 | return JsonProvider.provider().createGenerator(out); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Creates a parser factory for creating {@link JsonParser} objects. |
| 113 | * |
| 114 | * @return JSON parser factory. |
| 115 | * |
| 116 | public static JsonParserFactory createParserFactory() { |
| 117 | return JsonProvider.provider().createParserFactory(); |
| 118 | } |
| 119 | */ |
| 120 | |
| 121 | /** |
| 122 | * Creates a parser factory for creating {@link JsonParser} objects. |
| 123 | * The factory is configured with the specified map of provider specific |
| 124 | * configuration properties. Provider implementations should ignore any |
| 125 | * unsupported configuration properties specified in the map. |
| 126 | * |
| 127 | * @param config a map of provider specific properties to configure the |
| 128 | * JSON parsers. The map may be empty or null |
| 129 | * @return JSON parser factory |
| 130 | */ |
| 131 | public static JsonParserFactory createParserFactory(Map<String, ?> config) { |
| 132 | return JsonProvider.provider().createParserFactory(config); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Creates a generator factory for creating {@link JsonGenerator} objects. |
| 137 | * |
| 138 | * @return JSON generator factory |
| 139 | * |
| 140 | public static JsonGeneratorFactory createGeneratorFactory() { |
| 141 | return JsonProvider.provider().createGeneratorFactory(); |
| 142 | } |
| 143 | */ |
| 144 | |
| 145 | /** |
| 146 | * Creates a generator factory for creating {@link JsonGenerator} objects. |
| 147 | * The factory is configured with the specified map of provider specific |
| 148 | * configuration properties. Provider implementations should ignore any |
| 149 | * unsupported configuration properties specified in the map. |
| 150 | * |
| 151 | * @param config a map of provider specific properties to configure the |
| 152 | * JSON generators. The map may be empty or null |
| 153 | * @return JSON generator factory |
| 154 | */ |
| 155 | public static JsonGeneratorFactory createGeneratorFactory( |
| 156 | Map<String, ?> config) { |
| 157 | return JsonProvider.provider().createGeneratorFactory(config); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Creates a JSON writer to write a |
| 162 | * JSON {@link JsonObject object} or {@link JsonArray array} |
| 163 | * structure to the specified character stream. |
| 164 | * |
| 165 | * @param writer to which JSON object or array is written |
| 166 | * @return a JSON writer |
| 167 | */ |
| 168 | public static JsonWriter createWriter(Writer writer) { |
| 169 | return JsonProvider.provider().createWriter(writer); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Creates a JSON writer to write a |
| 174 | * JSON {@link JsonObject object} or {@link JsonArray array} |
| 175 | * structure to the specified byte stream. Characters written to |
| 176 | * the stream are encoded into bytes using UTF-8 encoding. |
| 177 | * |
| 178 | * @param out to which JSON object or array is written |
| 179 | * @return a JSON writer |
| 180 | */ |
| 181 | public static JsonWriter createWriter(OutputStream out) { |
| 182 | return JsonProvider.provider().createWriter(out); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Creates a JSON reader from a character stream. |
| 187 | * |
| 188 | * @param reader a reader from which JSON is to be read |
| 189 | * @return a JSON reader |
| 190 | */ |
| 191 | public static JsonReader createReader(Reader reader) { |
| 192 | return JsonProvider.provider().createReader(reader); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Creates a JSON reader from a byte stream. The character encoding of |
| 197 | * the stream is determined as described in |
| 198 | * <a href="http://tools.ietf.org/rfc/rfc7159.txt">RFC 7159</a>. |
| 199 | * |
| 200 | * @param in a byte stream from which JSON is to be read |
| 201 | * @return a JSON reader |
| 202 | */ |
| 203 | public static JsonReader createReader(InputStream in) { |
| 204 | return JsonProvider.provider().createReader(in); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Creates a reader factory for creating {@link JsonReader} objects. |
| 209 | * The factory is configured with the specified map of provider specific |
| 210 | * configuration properties. Provider implementations should ignore any |
| 211 | * unsupported configuration properties specified in the map. |
| 212 | * |
| 213 | * @param config a map of provider specific properties to configure the |
| 214 | * JSON readers. The map may be empty or null |
| 215 | * @return a JSON reader factory |
| 216 | */ |
| 217 | public static JsonReaderFactory createReaderFactory(Map<String, ?> config) { |
| 218 | return JsonProvider.provider().createReaderFactory(config); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Creates a writer factory for creating {@link JsonWriter} objects. |
| 223 | * The factory is configured with the specified map of provider specific |
| 224 | * configuration properties. Provider implementations should ignore any |
| 225 | * unsupported configuration properties specified in the map. |
| 226 | * |
| 227 | * @param config a map of provider specific properties to configure the |
| 228 | * JSON writers. The map may be empty or null |
| 229 | * @return a JSON writer factory |
| 230 | */ |
| 231 | public static JsonWriterFactory createWriterFactory(Map<String, ?> config) { |
| 232 | return JsonProvider.provider().createWriterFactory(config); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Creates a JSON array builder |
| 237 | * |
| 238 | * @return a JSON array builder |
| 239 | */ |
| 240 | public static JsonArrayBuilder createArrayBuilder() { |
| 241 | return JsonProvider.provider().createArrayBuilder(); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Creates a JSON array builder, initialized with the specified array |
| 246 | * |
| 247 | * @param array the initial array in the builder |
| 248 | * @return a JSON array builder |
| 249 | * |
| 250 | * @since 1.1 |
| 251 | */ |
| 252 | public static JsonArrayBuilder createArrayBuilder(JsonArray array) { |
| 253 | return JsonProvider.provider().createArrayBuilder(array); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Creates a JSON array builder, initialized with the content of specified {@code collection}. |
| 258 | * If the @{code collection} contains {@link Optional}s then resulting JSON array builder |
| 259 | * contains the value from the {@code collection} only if the {@link Optional} is not empty. |
| 260 | * |
| 261 | * @param collection the initial data for the builder |
| 262 | * @return a JSON array builder |
| 263 | * @exception IllegalArgumentException if the value from the {@code collection} cannot be converted |
| 264 | * to the corresponding {@link JsonValue} |
| 265 | * |
| 266 | * @since 1.1 |
| 267 | */ |
| 268 | public static JsonArrayBuilder createArrayBuilder(Collection<?> collection) { |
| 269 | return JsonProvider.provider().createArrayBuilder(collection); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Creates a JSON object builder |
| 274 | * |
| 275 | * @return a JSON object builder |
| 276 | */ |
| 277 | public static JsonObjectBuilder createObjectBuilder() { |
| 278 | return JsonProvider.provider().createObjectBuilder(); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Creates a JSON object builder, initialized with the specified object. |
| 283 | * |
| 284 | * @param object the initial object in the builder |
| 285 | * @return a JSON object builder |
| 286 | * |
| 287 | * @since 1.1 |
| 288 | */ |
| 289 | public static JsonObjectBuilder createObjectBuilder(JsonObject object) { |
| 290 | return JsonProvider.provider().createObjectBuilder(object); |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Creates a JSON object builder, initialized with the data from specified {@code map}. |
| 295 | * If the @{code map} contains {@link Optional}s then resulting JSON object builder |
| 296 | * contains the key from the {@code map} only if the {@link Optional} is not empty. |
| 297 | * |
| 298 | * @param map the initial object in the builder |
| 299 | * @return a JSON object builder |
| 300 | * @exception IllegalArgumentException if the value from the {@code map} cannot be converted |
| 301 | * to the corresponding {@link JsonValue} |
| 302 | * |
| 303 | * @since 1.1 |
| 304 | */ |
| 305 | public static JsonObjectBuilder createObjectBuilder(Map<String, ?> map) { |
| 306 | return JsonProvider.provider().createObjectBuilder(map); |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Creates JSON Pointer (<a href="http://tools.ietf.org/html/rfc6901">RFC 6901</a>) |
| 311 | * from given {@code jsonPointer} string. |
| 312 | * <ul> |
| 313 | * <li>An empty {@code jsonPointer} string defines a reference to the target itself.</li> |
| 314 | * <li>If the {@code jsonPointer} string is non-empty, it must be a sequence of '{@code /}' prefixed tokens.</li> |
| 315 | * </ul> |
| 316 | * |
| 317 | * @param jsonPointer the valid escaped JSON Pointer string |
| 318 | * @throws NullPointerException if {@code jsonPointer} is {@code null} |
| 319 | * @throws JsonException if {@code jsonPointer} is not a valid JSON Pointer |
| 320 | * @return a JSON Pointer |
| 321 | * |
| 322 | * @since 1.1 |
| 323 | */ |
| 324 | public static JsonPointer createPointer(String jsonPointer) { |
| 325 | return JsonProvider.provider().createPointer(jsonPointer); |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Creates a JSON Patch builder (<a href="http://tools.ietf.org/html/rfc6902">RFC 6902</a>). |
| 330 | * |
| 331 | * @return a JSON Patch builder |
| 332 | * |
| 333 | * @since 1.1 |
| 334 | */ |
| 335 | public static JsonPatchBuilder createPatchBuilder() { |
| 336 | return JsonProvider.provider().createPatchBuilder(); |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Creates a JSON Patch builder |
| 341 | * (<a href="http://tools.ietf.org/html/rfc6902">RFC 6902</a>), |
| 342 | * initialized with the specified operations. |
| 343 | * |
| 344 | * @param array the initial patch operations |
| 345 | * @return a JSON Patch builder |
| 346 | * |
| 347 | * @since 1.1 |
| 348 | */ |
| 349 | public static JsonPatchBuilder createPatchBuilder(JsonArray array) { |
| 350 | return JsonProvider.provider().createPatchBuilder(array); |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Creates a JSON Patch (<a href="http://tools.ietf.org/html/rfc6902">RFC 6902</a>) |
| 355 | * from the specified operations. |
| 356 | * |
| 357 | * @param array patch operations |
| 358 | * @return a JSON Patch |
| 359 | * |
| 360 | * @since 1.1 |
| 361 | */ |
| 362 | public static JsonPatch createPatch(JsonArray array) { |
| 363 | return JsonProvider.provider().createPatch(array); |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * Generates a JSON Patch (<a href="http://tools.ietf.org/html/rfc6902">RFC 6902</a>) |
| 368 | * from the source and target {@code JsonStructure}. |
| 369 | * The generated JSON Patch need not be unique. |
| 370 | * |
| 371 | * @param source the source |
| 372 | * @param target the target, must be the same type as the source |
| 373 | * @return a JSON Patch which when applied to the source, yields the target |
| 374 | * |
| 375 | * @since 1.1 |
| 376 | */ |
| 377 | public static JsonPatch createDiff(JsonStructure source, JsonStructure target) { |
| 378 | return JsonProvider.provider().createDiff(source, target); |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * Creates JSON Merge Patch (<a href="http://tools.ietf.org/html/rfc7396">RFC 7396</a>) |
| 383 | * from specified {@code JsonValue}. |
| 384 | * |
| 385 | * @param patch the patch |
| 386 | * @return a JSON Merge Patch |
| 387 | * |
| 388 | * @since 1.1 |
| 389 | */ |
| 390 | public static JsonMergePatch createMergePatch(JsonValue patch) { |
| 391 | return JsonProvider.provider().createMergePatch(patch); |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Generates a JSON Merge Patch (<a href="http://tools.ietf.org/html/rfc7396">RFC 7396</a>) |
| 396 | * from the source and target {@code JsonValue}s |
| 397 | * which when applied to the {@code source}, yields the {@code target}. |
| 398 | * |
| 399 | * @param source the source |
| 400 | * @param target the target |
| 401 | * @return a JSON Merge Patch |
| 402 | * |
| 403 | * @since 1.1 |
| 404 | */ |
| 405 | public static JsonMergePatch createMergeDiff(JsonValue source, JsonValue target) { |
| 406 | return JsonProvider.provider().createMergeDiff(source, target); |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * Creates a builder factory for creating {@link JsonArrayBuilder} |
| 411 | * and {@link JsonObjectBuilder} objects. |
| 412 | * The factory is configured with the specified map of provider specific |
| 413 | * configuration properties. Provider implementations should ignore any |
| 414 | * unsupported configuration properties specified in the map. |
| 415 | * |
| 416 | * @param config a map of provider specific properties to configure the |
| 417 | * JSON builders. The map may be empty or null |
| 418 | * @return a JSON builder factory |
| 419 | */ |
| 420 | public static JsonBuilderFactory createBuilderFactory( |
| 421 | Map<String, ?> config) { |
| 422 | return JsonProvider.provider().createBuilderFactory(config); |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * Creates a JsonString. |
| 427 | * |
| 428 | * @param value a JSON string |
| 429 | * @return the JsonString for the string |
| 430 | * |
| 431 | * @since 1.1 |
| 432 | */ |
| 433 | public static JsonString createValue(String value) { |
| 434 | return JsonProvider.provider().createValue(value); |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * Creates a JsonNumber. |
| 439 | * |
| 440 | * @param value a JSON number |
| 441 | * @return the JsonNumber for the number |
| 442 | * |
| 443 | * @since 1.1 |
| 444 | */ |
| 445 | public static JsonNumber createValue(int value) { |
| 446 | return JsonProvider.provider().createValue(value); |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Creates a JsonNumber. |
| 451 | * |
| 452 | * @param value a JSON number |
| 453 | * @return the JsonNumber for the number |
| 454 | * |
| 455 | * @since 1.1 |
| 456 | */ |
| 457 | public static JsonNumber createValue(long value) { |
| 458 | return JsonProvider.provider().createValue(value); |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * Creates a JsonNumber. |
| 463 | * |
| 464 | * @param value a JSON number |
| 465 | * @return the JsonNumber for the number |
| 466 | * |
| 467 | * @since 1.1 |
| 468 | */ |
| 469 | public static JsonNumber createValue(double value) { |
| 470 | return JsonProvider.provider().createValue(value); |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * Creates a JsonNumber. |
| 475 | * |
| 476 | * @param value a JSON number |
| 477 | * @return the JsonNumber for the number |
| 478 | * |
| 479 | * @since 1.1 |
| 480 | */ |
| 481 | public static JsonNumber createValue(BigDecimal value) { |
| 482 | return JsonProvider.provider().createValue(value); |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * Creates a JsonNumber. |
| 487 | * |
| 488 | * @param value a JSON number |
| 489 | * @return the JsonNumber for the number |
| 490 | * |
| 491 | * @since 1.1 |
| 492 | */ |
| 493 | public static JsonNumber createValue(BigInteger value) { |
| 494 | return JsonProvider.provider().createValue(value); |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * Encodes (escapes) a passed string as defined by <a href="http://tools.ietf.org/html/rfc6901">RFC 6901</a>. |
| 499 | * This method doesn't validate the passed JSON-pointer string. |
| 500 | * |
| 501 | * @param pointer the JSON-pointer string to encode |
| 502 | * @return encoded JSON-pointer string |
| 503 | * |
| 504 | * @since 1.1 |
| 505 | */ |
| 506 | public static String encodePointer(String pointer) { |
| 507 | return pointer.replace("~", "~0").replace("/", "~1"); |
| 508 | } |
| 509 | |
| 510 | /** |
| 511 | * Decodes a passed JSON-pointer string as defined by <a href="http://tools.ietf.org/html/rfc6901">RFC 6901</a>. |
| 512 | * This method doesn't validate the passed JSON-pointer string. |
| 513 | * |
| 514 | * @param escaped the JSON-pointer string to decode |
| 515 | * @return decoded JSON-pointer string |
| 516 | * |
| 517 | * @since 1.1 |
| 518 | */ |
| 519 | public static String decodePointer(String escaped) { |
| 520 | return escaped.replace("~1", "/").replace("~0", "~"); |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * Creates a JsonNumber. |
| 525 | * |
| 526 | * @param value a JSON number |
| 527 | * @return the JsonNumber for the number |
| 528 | * |
| 529 | * @since 2.1 |
| 530 | */ |
| 531 | public static JsonNumber createValue(Number value) { |
| 532 | return JsonProvider.provider().createValue(value); |
| 533 | } |
| 534 | } |