source: josm/trunk/src/javax/json/spi/JsonProvider.java@ 13231

Last change on this file since 13231 was 13231, checked in by Don-vip, 8 years ago

see #15682 - upgrade to JSR 374 (JSON Processing) API 1.1.2

File size: 16.9 KB
Line 
1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright (c) 2011-2017 Oracle and/or its affiliates. All rights reserved.
5 *
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common Development
8 * and Distribution License("CDDL") (collectively, the "License"). You
9 * may not use this file except in compliance with the License. You can
10 * obtain a copy of the License at
11 * https://oss.oracle.com/licenses/CDDL+GPL-1.1
12 * or LICENSE.txt. See the License for the specific
13 * language governing permissions and limitations under the License.
14 *
15 * When distributing the software, include this License Header Notice in each
16 * file and include the License file at LICENSE.txt.
17 *
18 * GPL Classpath Exception:
19 * Oracle designates this particular file as subject to the "Classpath"
20 * exception as provided by Oracle in the GPL Version 2 section of the License
21 * file that accompanied this code.
22 *
23 * Modifications:
24 * If applicable, add the following below the License Header, with the fields
25 * enclosed by brackets [] replaced by your own identifying information:
26 * "Portions Copyright [year] [name of copyright owner]"
27 *
28 * Contributor(s):
29 * If you wish your version of this file to be governed by only the CDDL or
30 * only the GPL Version 2, indicate your decision by adding "[Contributor]
31 * elects to include this software in this distribution under the [CDDL or GPL
32 * Version 2] license." If you don't indicate a single choice of license, a
33 * recipient has the option to distribute your version of this file under
34 * either the CDDL, the GPL Version 2 or to extend the choice of license to
35 * its licensees as provided above. However, if you add GPL Version 2 code
36 * and therefore, elected the GPL Version 2 license, then the option applies
37 * only if the new code is made subject to such option by the copyright
38 * holder.
39 */
40
41package javax.json.spi;
42
43import javax.json.*;
44import javax.json.stream.JsonGenerator;
45import javax.json.stream.JsonGeneratorFactory;
46import javax.json.stream.JsonParser;
47import javax.json.stream.JsonParserFactory;
48import java.io.InputStream;
49import java.io.OutputStream;
50import java.io.Reader;
51import java.io.Writer;
52import java.util.Collection;
53import java.util.Iterator;
54import java.util.Map;
55import java.util.ServiceLoader;
56import java.math.BigDecimal;
57import java.math.BigInteger;
58import java.util.Optional;
59
60/**
61 * Service provider for JSON processing objects.
62 *
63 * <p>All the methods in this class are safe for use by multiple concurrent
64 * threads.
65 *
66 * @see ServiceLoader
67 */
68public abstract class JsonProvider {
69
70 /**
71 * A constant representing the name of the default
72 * {@code JsonProvider} implementation class.
73 */
74 private static final String DEFAULT_PROVIDER
75 = "org.glassfish.json.JsonProviderImpl";
76
77 protected JsonProvider() {
78 }
79
80 /**
81 * Creates a JSON provider object. The provider is loaded using the
82 * {@link ServiceLoader#load(Class)} method. If there are no available
83 * service providers, this method returns the default service provider.
84 * Users are recommended to cache the result of this method.
85 *
86 * @see ServiceLoader
87 * @return a JSON provider
88 */
89 public static JsonProvider provider() {
90 ServiceLoader<JsonProvider> loader = ServiceLoader.load(JsonProvider.class);
91 Iterator<JsonProvider> it = loader.iterator();
92 if (it.hasNext()) {
93 return it.next();
94 }
95 try {
96 Class<?> clazz = Class.forName(DEFAULT_PROVIDER);
97 return (JsonProvider) clazz.newInstance();
98 } catch (ClassNotFoundException x) {
99 throw new JsonException(
100 "Provider " + DEFAULT_PROVIDER + " not found", x);
101 } catch (Exception x) {
102 throw new JsonException(
103 "Provider " + DEFAULT_PROVIDER + " could not be instantiated: " + x,
104 x);
105 }
106 }
107
108 /**
109 * Creates a JSON parser from a character stream.
110 *
111 * @param reader i/o reader from which JSON is to be read
112 * @return a JSON parser
113 */
114 public abstract JsonParser createParser(Reader reader);
115
116 /**
117 * Creates a JSON parser from the specified byte stream.
118 * The character encoding of the stream is determined
119 * as defined in <a href="http://tools.ietf.org/rfc/rfc7159.txt">RFC 7159
120 * </a>.
121 *
122 * @param in i/o stream from which JSON is to be read
123 * @throws JsonException if encoding cannot be determined
124 * or i/o error (IOException would be cause of JsonException)
125 * @return a JSON parser
126 */
127 public abstract JsonParser createParser(InputStream in);
128
129 /**
130 * Creates a parser factory for creating {@link JsonParser} instances.
131 *
132 * @return a JSON parser factory
133 *
134 public abstract JsonParserFactory createParserFactory();
135 */
136
137 /**
138 * Creates a parser factory for creating {@link JsonParser} instances.
139 * The factory is configured with the specified map of
140 * provider specific configuration properties. Provider implementations
141 * should ignore any unsupported configuration properties specified in
142 * the map.
143 *
144 * @param config a map of provider specific properties to configure the
145 * JSON parsers. The map may be empty or null
146 * @return a JSON parser factory
147 */
148 public abstract JsonParserFactory createParserFactory(Map<String, ?> config);
149
150 /**
151 * Creates a JSON generator for writing JSON text to a character stream.
152 *
153 * @param writer a i/o writer to which JSON is written
154 * @return a JSON generator
155 */
156 public abstract JsonGenerator createGenerator(Writer writer);
157
158 /**
159 * Creates a JSON generator for writing JSON text to a byte stream.
160 *
161 * @param out i/o stream to which JSON is written
162 * @return a JSON generator
163 */
164 public abstract JsonGenerator createGenerator(OutputStream out);
165
166 /**
167 * Creates a generator factory for creating {@link JsonGenerator} instances.
168 *
169 * @return a JSON generator factory
170 *
171 public abstract JsonGeneratorFactory createGeneratorFactory();
172 */
173
174 /**
175 * Creates a generator factory for creating {@link JsonGenerator} instances.
176 * The factory is configured with the specified map of provider specific
177 * configuration properties. Provider implementations should
178 * ignore any unsupported configuration properties specified in the map.
179 *
180 * @param config a map of provider specific properties to configure the
181 * JSON generators. The map may be empty or null
182 * @return a JSON generator factory
183 */
184 public abstract JsonGeneratorFactory createGeneratorFactory(Map<String, ?> config);
185
186 /**
187 * Creates a JSON reader from a character stream.
188 *
189 * @param reader a reader from which JSON is to be read
190 * @return a JSON reader
191 */
192 public abstract JsonReader createReader(Reader reader);
193
194 /**
195 * Creates a JSON reader from a byte stream. The character encoding of
196 * the stream is determined as described in
197 * <a href="http://tools.ietf.org/rfc/rfc7159.txt">RFC 7159</a>.
198 *
199 * @param in a byte stream from which JSON is to be read
200 * @return a JSON reader
201 */
202 public abstract JsonReader createReader(InputStream in);
203
204 /**
205 * Creates a JSON writer to write a
206 * JSON {@link JsonObject object} or {@link JsonArray array}
207 * structure to the specified character stream.
208 *
209 * @param writer to which JSON object or array is written
210 * @return a JSON writer
211 */
212 public abstract JsonWriter createWriter(Writer writer);
213
214 /**
215 * Creates a JSON writer to write a
216 * JSON {@link JsonObject object} or {@link JsonArray array}
217 * structure to the specified byte stream. Characters written to
218 * the stream are encoded into bytes using UTF-8 encoding.
219 *
220 * @param out to which JSON object or array is written
221 * @return a JSON writer
222 */
223 public abstract JsonWriter createWriter(OutputStream out);
224
225 /**
226 * Creates a writer factory for creating {@link JsonWriter} objects.
227 * The factory is configured with the specified map of provider specific
228 * configuration properties. Provider implementations should ignore any
229 * unsupported configuration properties specified in the map.
230 *
231 * @param config a map of provider specific properties to configure the
232 * JSON writers. The map may be empty or null
233 * @return a JSON writer factory
234 */
235 public abstract JsonWriterFactory createWriterFactory(Map<String,?> config);
236
237 /**
238 * Creates a reader factory for creating {@link JsonReader} objects.
239 * The factory is configured with the specified map of provider specific
240 * configuration properties. Provider implementations should ignore any
241 * unsupported configuration properties specified in the map.
242 *
243 * @param config a map of provider specific properties to configure the
244 * JSON readers. The map may be empty or null
245 * @return a JSON reader factory
246 */
247 public abstract JsonReaderFactory createReaderFactory(Map<String,?> config);
248
249 /**
250 * Creates a JSON object builder.
251 *
252 * @return a JSON object builder
253 */
254 public abstract JsonObjectBuilder createObjectBuilder();
255
256 /**
257 * Creates a JSON object builder, initialized with the specified object.
258 *
259 * @param object the initial JSON object in the builder
260 * @return a JSON object builder
261 *
262 * @since 1.1
263 */
264 public JsonObjectBuilder createObjectBuilder(JsonObject object) {
265 throw new UnsupportedOperationException();
266 }
267
268 /**
269 * Creates a JSON object builder, initialized with the data from specified {@code map}.
270 * If the @{code map} contains {@link Optional}s then resulting JSON object builder
271 * contains the key from the {@code map} only if the {@link Optional} is not empty.
272 *
273 * @param map the initial object in the builder
274 * @return a JSON object builder
275 * @exception IllegalArgumentException if the value from the {@code map} cannot be converted
276 * to the corresponding {@link JsonValue}
277 *
278 * @since 1.1
279 */
280 public JsonObjectBuilder createObjectBuilder(Map<String, Object> map) {
281 throw new UnsupportedOperationException();
282 }
283
284 /**
285 * Creates a JSON array builder.
286 *
287 * @return a JSON array builder
288 */
289 public abstract JsonArrayBuilder createArrayBuilder();
290
291 /**
292 * Creates a JSON array builder, initialized with the specified array.
293 *
294 * @param array the initial JSON array in the builder
295 * @return a JSON array builder
296 *
297 * @since 1.1
298 */
299 public JsonArrayBuilder createArrayBuilder(JsonArray array) {
300 throw new UnsupportedOperationException();
301 }
302
303 /**
304 * Creates JSON Pointer (<a href="http://tools.ietf.org/html/rfc6901">RFC 6901</a>)
305 * from given {@code jsonPointer} string.
306 * <ul>
307 * <li>An empty {@code jsonPointer} string defines a reference to the target itself.</li>
308 * <li>If the {@code jsonPointer} string is non-empty, it must be a sequence of '{@code /}' prefixed tokens.</li>
309 * </ul>
310 *
311 * @param jsonPointer the JSON Pointer string
312 * @throws NullPointerException if {@code jsonPointer} is {@code null}
313 * @throws JsonException if {@code jsonPointer} is not a valid JSON Pointer
314 * @return a JSON Pointer
315 *
316 * @since 1.1
317 */
318 public JsonPointer createPointer(String jsonPointer) {
319 throw new UnsupportedOperationException();
320 }
321
322 /**
323 * Creates a JSON Patch builder (<a href="http://tools.ietf.org/html/rfc6902">RFC 6902</a>).
324 *
325 * @return a JSON Patch builder
326 *
327 * @since 1.1
328 */
329 public JsonPatchBuilder createPatchBuilder() {
330 throw new UnsupportedOperationException();
331 }
332
333 /**
334 * Creates a JSON Patch builder
335 * (<a href="http://tools.ietf.org/html/rfc6902">RFC 6902</a>),
336 * initialized with the specified operations.
337 *
338 * @param array the initial patch operations
339 * @return a JSON Patch builder
340 *
341 * @since 1.1
342 */
343 public JsonPatchBuilder createPatchBuilder(JsonArray array) {
344 throw new UnsupportedOperationException();
345 }
346
347 /**
348 * Creates a JSON Patch (<a href="http://tools.ietf.org/html/rfc6902">RFC 6902</a>)
349 * from the specified operations.
350 *
351 * @param array patch operations
352 * @return a JSON Patch
353 *
354 * @since 1.1
355 */
356 public JsonPatch createPatch(JsonArray array) {
357 throw new UnsupportedOperationException();
358 }
359
360 /**
361 * Generates a JSON Patch (<a href="http://tools.ietf.org/html/rfc6902">RFC 6902</a>)
362 * from the source and target {@code JsonStructure}.
363 * The generated JSON Patch need not be unique.
364 *
365 * @param source the source
366 * @param target the target, must be the same type as the source
367 * @return a JSON Patch which when applied to the source, yields the target
368 *
369 * @since 1.1
370 */
371 public JsonPatch createDiff(JsonStructure source, JsonStructure target) {
372 throw new UnsupportedOperationException();
373 }
374
375 /**
376 * Creates JSON Merge Patch (<a href="http://tools.ietf.org/html/rfc7396">RFC 7396</a>)
377 * from specified {@code JsonValue}.
378 *
379 * @param patch the patch
380 * @return a JSON Merge Patch
381 *
382 * @since 1.1
383 */
384 public JsonMergePatch createMergePatch(JsonValue patch) {
385 throw new UnsupportedOperationException();
386 }
387
388 /**
389 * Generates a JSON Merge Patch (<a href="http://tools.ietf.org/html/rfc7396">RFC 7396</a>)
390 * from the source and target {@code JsonValue}s
391 * which when applied to the {@code source}, yields the {@code target}.
392 *
393 * @param source the source
394 * @param target the target
395 * @return a JSON Merge Patch
396 *
397 * @since 1.1
398 */
399 public JsonMergePatch createMergeDiff(JsonValue source, JsonValue target) {
400 throw new UnsupportedOperationException();
401 }
402
403 /**
404 * Creates a JSON array builder, initialized with the content of specified {@code collection}.
405 * If the @{code collection} contains {@link Optional}s then resulting JSON array builder
406 * contains the value from the {@code collection} only if the {@link Optional} is not empty.
407 *
408 * @param collection the initial data for the builder
409 * @return a JSON array builder
410 * @exception IllegalArgumentException if the value from the {@code collection} cannot be converted
411 * to the corresponding {@link JsonValue}
412 *
413 * @since 1.1
414 */
415 public JsonArrayBuilder createArrayBuilder(Collection<?> collection) {
416 throw new UnsupportedOperationException();
417 }
418
419
420 /**
421 * Creates a builder factory for creating {@link JsonArrayBuilder}
422 * and {@link JsonObjectBuilder} objects.
423 * The factory is configured with the specified map of provider specific
424 * configuration properties. Provider implementations should ignore any
425 * unsupported configuration properties specified in the map.
426 *
427 * @param config a map of provider specific properties to configure the
428 * JSON builders. The map may be empty or null
429 * @return a JSON builder factory
430 */
431 public abstract JsonBuilderFactory createBuilderFactory(Map<String,?> config);
432
433 /**
434 * Creates a JsonString.
435 *
436 * @param value a JSON string
437 * @return the JsonString for the string
438 *
439 * @since 1.1
440 */
441 public JsonString createValue(String value) {
442 throw new UnsupportedOperationException();
443 }
444
445 /**
446 * Creates a JsonNumber.
447 *
448 * @param value a JSON number
449 * @return the JsonNumber for the number
450 *
451 * @since 1.1
452 */
453 public JsonNumber createValue(int value) {
454 throw new UnsupportedOperationException();
455 }
456
457 /**
458 * Creates a JsonNumber.
459 *
460 * @param value a JSON number
461 * @return the JsonNumber for the number
462 *
463 * @since 1.1
464 */
465 public JsonNumber createValue(long value) {
466 throw new UnsupportedOperationException();
467 }
468
469 /**
470 * Creates a JsonNumber.
471 *
472 * @param value a JSON number
473 * @return the JsonNumber for the number
474 *
475 * @since 1.1
476 */
477 public JsonNumber createValue(double value) {
478 throw new UnsupportedOperationException();
479 }
480
481 /**
482 * Creates a JsonNumber.
483 *
484 * @param value a JSON number
485 * @return the JsonNumber for the number
486 *
487 * @since 1.1
488 */
489 public JsonNumber createValue(BigDecimal value) {
490 throw new UnsupportedOperationException();
491 }
492
493 /**
494 * Creates a JsonNumber.
495 *
496 * @param value a JSON number
497 * @return the JsonNumber for the number
498 *
499 * @since 1.1
500 */
501 public JsonNumber createValue(BigInteger value) {
502 throw new UnsupportedOperationException();
503 }
504}
Note: See TracBrowser for help on using the repository browser.