source: josm/trunk/src/org/glassfish/json/JsonProviderImpl.java@ 15928

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

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

File size: 9.4 KB
Line 
1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright (c) 2012-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 org.glassfish.json;
42
43import org.glassfish.json.api.BufferPool;
44
45import javax.json.*;
46import javax.json.stream.JsonGenerator;
47import javax.json.stream.JsonGeneratorFactory;
48import javax.json.stream.JsonParser;
49import javax.json.stream.JsonParserFactory;
50import javax.json.spi.JsonProvider;
51import java.io.InputStream;
52import java.io.OutputStream;
53import java.io.Reader;
54import java.io.Writer;
55import java.util.Collection;
56import java.util.Collections;
57import java.util.HashMap;
58import java.util.Map;
59import java.math.BigDecimal;
60import java.math.BigInteger;
61
62/**
63 * @author Jitendra Kotamraju
64 * @author Kin-man Chung
65 * @author Alex Soto
66 */
67public class JsonProviderImpl extends JsonProvider {
68
69 private final BufferPool bufferPool = new BufferPoolImpl();
70
71 @Override
72 public JsonGenerator createGenerator(Writer writer) {
73 return new JsonGeneratorImpl(writer, bufferPool);
74 }
75
76 @Override
77 public JsonGenerator createGenerator(OutputStream out) {
78 return new JsonGeneratorImpl(out, bufferPool);
79 }
80
81 @Override
82 public JsonParser createParser(Reader reader) {
83 return new JsonParserImpl(reader, bufferPool);
84 }
85
86 @Override
87 public JsonParser createParser(InputStream in) {
88 return new JsonParserImpl(in, bufferPool);
89 }
90
91 @Override
92 public JsonParserFactory createParserFactory(Map<String, ?> config) {
93 BufferPool pool = null;
94 if (config != null && config.containsKey(BufferPool.class.getName())) {
95 pool = (BufferPool)config.get(BufferPool.class.getName());
96 }
97 if (pool == null) {
98 pool = bufferPool;
99 }
100 return new JsonParserFactoryImpl(pool);
101 }
102
103 @Override
104 public JsonGeneratorFactory createGeneratorFactory(Map<String, ?> config) {
105 Map<String, Object> providerConfig;
106 boolean prettyPrinting;
107 BufferPool pool;
108 if (config == null) {
109 providerConfig = Collections.emptyMap();
110 prettyPrinting = false;
111 pool = bufferPool;
112 } else {
113 providerConfig = new HashMap<>();
114 if (prettyPrinting=JsonProviderImpl.isPrettyPrintingEnabled(config)) {
115 providerConfig.put(JsonGenerator.PRETTY_PRINTING, true);
116 }
117 pool = (BufferPool)config.get(BufferPool.class.getName());
118 if (pool != null) {
119 providerConfig.put(BufferPool.class.getName(), pool);
120 } else {
121 pool = bufferPool;
122 }
123 providerConfig = Collections.unmodifiableMap(providerConfig);
124 }
125
126 return new JsonGeneratorFactoryImpl(providerConfig, prettyPrinting, pool);
127 }
128
129 @Override
130 public JsonReader createReader(Reader reader) {
131 return new JsonReaderImpl(reader, bufferPool);
132 }
133
134 @Override
135 public JsonReader createReader(InputStream in) {
136 return new JsonReaderImpl(in, bufferPool);
137 }
138
139 @Override
140 public JsonWriter createWriter(Writer writer) {
141 return new JsonWriterImpl(writer, bufferPool);
142 }
143
144 @Override
145 public JsonWriter createWriter(OutputStream out) {
146 return new JsonWriterImpl(out, bufferPool);
147 }
148
149 @Override
150 public JsonWriterFactory createWriterFactory(Map<String, ?> config) {
151 Map<String, Object> providerConfig;
152 boolean prettyPrinting;
153 BufferPool pool;
154 if (config == null) {
155 providerConfig = Collections.emptyMap();
156 prettyPrinting = false;
157 pool = bufferPool;
158 } else {
159 providerConfig = new HashMap<>();
160 if (prettyPrinting=JsonProviderImpl.isPrettyPrintingEnabled(config)) {
161 providerConfig.put(JsonGenerator.PRETTY_PRINTING, true);
162 }
163 pool = (BufferPool)config.get(BufferPool.class.getName());
164 if (pool != null) {
165 providerConfig.put(BufferPool.class.getName(), pool);
166 } else {
167 pool = bufferPool;
168 }
169 providerConfig = Collections.unmodifiableMap(providerConfig);
170 }
171 return new JsonWriterFactoryImpl(providerConfig, prettyPrinting, pool);
172 }
173
174 @Override
175 public JsonReaderFactory createReaderFactory(Map<String, ?> config) {
176 BufferPool pool = null;
177 if (config != null && config.containsKey(BufferPool.class.getName())) {
178 pool = (BufferPool)config.get(BufferPool.class.getName());
179 }
180 if (pool == null) {
181 pool = bufferPool;
182 }
183 return new JsonReaderFactoryImpl(pool);
184 }
185
186 @Override
187 public JsonObjectBuilder createObjectBuilder() {
188 return new JsonObjectBuilderImpl(bufferPool);
189 }
190
191 @Override
192 public JsonObjectBuilder createObjectBuilder(JsonObject object) {
193 return new JsonObjectBuilderImpl(object, bufferPool);
194 }
195
196 @Override
197 public JsonObjectBuilder createObjectBuilder(Map<String, Object> map) {
198 return new JsonObjectBuilderImpl(map, bufferPool);
199 }
200
201 @Override
202 public JsonArrayBuilder createArrayBuilder() {
203 return new JsonArrayBuilderImpl(bufferPool);
204 }
205
206 @Override
207 public JsonArrayBuilder createArrayBuilder(JsonArray array) {
208 return new JsonArrayBuilderImpl(array, bufferPool);
209 }
210
211 @Override
212 public JsonArrayBuilder createArrayBuilder(Collection<?> collection) {
213 return new JsonArrayBuilderImpl(collection, bufferPool);
214 }
215
216 @Override
217 public JsonPointer createPointer(String jsonPointer) {
218 return new JsonPointerImpl(jsonPointer);
219 }
220
221 @Override
222 public JsonPatchBuilder createPatchBuilder() {
223 return new JsonPatchBuilderImpl();
224 }
225
226 @Override
227 public JsonPatchBuilder createPatchBuilder(JsonArray array) {
228 return new JsonPatchBuilderImpl(array);
229 }
230
231 @Override
232 public JsonPatch createPatch(JsonArray array) {
233 return new JsonPatchImpl(array);
234 }
235
236 @Override
237 public JsonPatch createDiff(JsonStructure source, JsonStructure target) {
238 return new JsonPatchImpl(JsonPatchImpl.diff(source, target));
239 }
240
241 @Override
242 public JsonMergePatch createMergePatch(JsonValue patch) {
243 return new JsonMergePatchImpl(patch);
244 }
245
246 @Override
247 public JsonMergePatch createMergeDiff(JsonValue source, JsonValue target) {
248 return new JsonMergePatchImpl(JsonMergePatchImpl.diff(source, target));
249 }
250
251 @Override
252 public JsonString createValue(String value) {
253 return new JsonStringImpl(value);
254 }
255
256 @Override
257 public JsonNumber createValue(int value) {
258 return JsonNumberImpl.getJsonNumber(value);
259 }
260
261 @Override
262 public JsonNumber createValue(long value) {
263 return JsonNumberImpl.getJsonNumber(value);
264 }
265
266 @Override
267 public JsonNumber createValue(double value) {
268 return JsonNumberImpl.getJsonNumber(value);
269 }
270
271 @Override
272 public JsonNumber createValue(BigInteger value) {
273 return JsonNumberImpl.getJsonNumber(value);
274 }
275
276 @Override
277 public JsonNumber createValue(BigDecimal value) {
278 return JsonNumberImpl.getJsonNumber(value);
279 }
280
281 @Override
282 public JsonBuilderFactory createBuilderFactory(Map<String,?> config) {
283 BufferPool pool = null ;
284 if (config != null && config.containsKey(BufferPool.class.getName())) {
285 pool = (BufferPool)config.get(BufferPool.class.getName());
286 }
287 if (pool == null) {
288 pool = bufferPool;
289 }
290 return new JsonBuilderFactoryImpl(pool);
291 }
292
293 static boolean isPrettyPrintingEnabled(Map<String, ?> config) {
294 return config.containsKey(JsonGenerator.PRETTY_PRINTING);
295 }
296}
Note: See TracBrowser for help on using the repository browser.