Changeset 13231 in josm for trunk/src/org
- Timestamp:
- 2017-12-23T02:40:43+01:00 (7 years ago)
- Location:
- trunk/src/org/glassfish/json
- Files:
-
- 8 added
- 22 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/glassfish/json/BufferPoolImpl.java
r6756 r13231 2 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 3 * 4 * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.4 * Copyright (c) 2013-2017 Oracle and/or its affiliates. All rights reserved. 5 5 * 6 6 * The contents of this file are subject to the terms of either the GNU … … 9 9 * may not use this file except in compliance with the License. You can 10 10 * obtain a copy of the License at 11 * https:// glassfish.dev.java.net/public/CDDL+GPL_1_1.html12 * or packager/legal/LICENSE.txt. See the License for the specific11 * https://oss.oracle.com/licenses/CDDL+GPL-1.1 12 * or LICENSE.txt. See the License for the specific 13 13 * language governing permissions and limitations under the License. 14 14 * 15 15 * When distributing the software, include this License Header Notice in each 16 * file and include the License file at packager/legal/LICENSE.txt.16 * file and include the License file at LICENSE.txt. 17 17 * 18 18 * GPL Classpath Exception: … … 82 82 83 83 // overwrite the queue 84 ConcurrentLinkedQueue<char[]> d = new ConcurrentLinkedQueue< char[]>();85 queue = new WeakReference< ConcurrentLinkedQueue<char[]>>(d);84 ConcurrentLinkedQueue<char[]> d = new ConcurrentLinkedQueue<>(); 85 queue = new WeakReference<>(d); 86 86 87 87 return d; -
trunk/src/org/glassfish/json/JsonArrayBuilderImpl.java
r6756 r13231 2 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 3 * 4 * Copyright (c) 2012-201 3Oracle and/or its affiliates. All rights reserved.4 * Copyright (c) 2012-2017 Oracle and/or its affiliates. All rights reserved. 5 5 * 6 6 * The contents of this file are subject to the terms of either the GNU … … 9 9 * may not use this file except in compliance with the License. You can 10 10 * obtain a copy of the License at 11 * https:// glassfish.dev.java.net/public/CDDL+GPL_1_1.html12 * or packager/legal/LICENSE.txt. See the License for the specific11 * https://oss.oracle.com/licenses/CDDL+GPL-1.1 12 * or LICENSE.txt. See the License for the specific 13 13 * language governing permissions and limitations under the License. 14 14 * 15 15 * When distributing the software, include this License Header Notice in each 16 * file and include the License file at packager/legal/LICENSE.txt.16 * file and include the License file at LICENSE.txt. 17 17 * 18 18 * GPL Classpath Exception: … … 49 49 import java.util.AbstractList; 50 50 import java.util.ArrayList; 51 import java.util.Collection; 51 52 import java.util.Collections; 52 53 import java.util.List; 54 import java.util.Optional; 53 55 54 56 /** 55 * JsonArrayBuilder impl 57 * JsonArrayBuilder implementation 56 58 * 57 59 * @author Jitendra Kotamraju 60 * @author Kin-man Chung 58 61 */ 62 59 63 class JsonArrayBuilderImpl implements JsonArrayBuilder { 60 64 private ArrayList<JsonValue> valueList; … … 65 69 } 66 70 71 JsonArrayBuilderImpl(JsonArray array, BufferPool bufferPool) { 72 this.bufferPool = bufferPool; 73 valueList = new ArrayList<>(); 74 valueList.addAll(array); 75 } 76 77 JsonArrayBuilderImpl(Collection<?> collection, BufferPool bufferPool) { 78 this.bufferPool = bufferPool; 79 valueList = new ArrayList<>(); 80 populate(collection); 81 } 82 83 @Override 67 84 public JsonArrayBuilder add(JsonValue value) { 68 85 validateValue(value); … … 71 88 } 72 89 90 @Override 73 91 public JsonArrayBuilder add(String value) { 74 92 validateValue(value); … … 77 95 } 78 96 97 @Override 79 98 public JsonArrayBuilder add(BigDecimal value) { 80 99 validateValue(value); … … 83 102 } 84 103 104 @Override 85 105 public JsonArrayBuilder add(BigInteger value) { 86 106 validateValue(value); … … 89 109 } 90 110 111 @Override 91 112 public JsonArrayBuilder add(int value) { 92 113 addValueList(JsonNumberImpl.getJsonNumber(value)); … … 94 115 } 95 116 117 @Override 96 118 public JsonArrayBuilder add(long value) { 97 119 addValueList(JsonNumberImpl.getJsonNumber(value)); … … 99 121 } 100 122 123 @Override 101 124 public JsonArrayBuilder add(double value) { 102 125 addValueList(JsonNumberImpl.getJsonNumber(value)); … … 104 127 } 105 128 129 @Override 106 130 public JsonArrayBuilder add(boolean value) { 107 131 addValueList(value ? JsonValue.TRUE : JsonValue.FALSE); … … 109 133 } 110 134 135 @Override 111 136 public JsonArrayBuilder addNull() { 112 137 addValueList(JsonValue.NULL); … … 114 139 } 115 140 141 @Override 116 142 public JsonArrayBuilder add(JsonObjectBuilder builder) { 117 143 if (builder == null) { … … 122 148 } 123 149 150 @Override 124 151 public JsonArrayBuilder add(JsonArrayBuilder builder) { 125 152 if (builder == null) { … … 130 157 } 131 158 159 @Override 160 public JsonArrayBuilder addAll(JsonArrayBuilder builder) { 161 if (builder == null) { 162 throw new NullPointerException(JsonMessages.ARRBUILDER_ARRAY_BUILDER_NULL()); 163 } 164 if (valueList == null) { 165 valueList = new ArrayList<>(); 166 } 167 valueList.addAll(builder.build()); 168 return this; 169 } 170 171 @Override 172 public JsonArrayBuilder add(int index, JsonValue value) { 173 validateValue(value); 174 addValueList(index, value); 175 return this; 176 } 177 178 @Override 179 public JsonArrayBuilder add(int index, String value) { 180 validateValue(value); 181 addValueList(index, new JsonStringImpl(value)); 182 return this; 183 } 184 185 @Override 186 public JsonArrayBuilder add(int index, BigDecimal value) { 187 validateValue(value); 188 addValueList(index, JsonNumberImpl.getJsonNumber(value)); 189 return this; 190 } 191 192 @Override 193 public JsonArrayBuilder add(int index, BigInteger value) { 194 validateValue(value); 195 addValueList(index, JsonNumberImpl.getJsonNumber(value)); 196 return this; 197 } 198 199 @Override 200 public JsonArrayBuilder add(int index, int value) { 201 addValueList(index, JsonNumberImpl.getJsonNumber(value)); 202 return this; 203 } 204 205 @Override 206 public JsonArrayBuilder add(int index, long value) { 207 addValueList(index, JsonNumberImpl.getJsonNumber(value)); 208 return this; 209 } 210 211 @Override 212 public JsonArrayBuilder add(int index, double value) { 213 addValueList(index, JsonNumberImpl.getJsonNumber(value)); 214 return this; 215 } 216 217 @Override 218 public JsonArrayBuilder add(int index, boolean value) { 219 addValueList(index, value ? JsonValue.TRUE : JsonValue.FALSE); 220 return this; 221 } 222 223 @Override 224 public JsonArrayBuilder addNull(int index) { 225 addValueList(index, JsonValue.NULL); 226 return this; 227 } 228 229 @Override 230 public JsonArrayBuilder add(int index, JsonObjectBuilder builder) { 231 if (builder == null) { 232 throw new NullPointerException(JsonMessages.ARRBUILDER_OBJECT_BUILDER_NULL()); 233 } 234 addValueList(index, builder.build()); 235 return this; 236 } 237 238 @Override 239 public JsonArrayBuilder add(int index, JsonArrayBuilder builder) { 240 if (builder == null) { 241 throw new NullPointerException(JsonMessages.ARRBUILDER_OBJECT_BUILDER_NULL()); 242 } 243 addValueList(index, builder.build()); 244 return this; 245 } 246 247 @Override 248 public JsonArrayBuilder set(int index, JsonValue value) { 249 validateValue(value); 250 setValueList(index, value); 251 return this; 252 } 253 254 @Override 255 public JsonArrayBuilder set(int index, String value) { 256 validateValue(value); 257 setValueList(index, new JsonStringImpl(value)); 258 return this; 259 } 260 261 @Override 262 public JsonArrayBuilder set(int index, BigDecimal value) { 263 validateValue(value); 264 setValueList(index, JsonNumberImpl.getJsonNumber(value)); 265 return this; 266 } 267 268 @Override 269 public JsonArrayBuilder set(int index, BigInteger value) { 270 validateValue(value); 271 setValueList(index, JsonNumberImpl.getJsonNumber(value)); 272 return this; 273 } 274 275 @Override 276 public JsonArrayBuilder set(int index, int value) { 277 setValueList(index, JsonNumberImpl.getJsonNumber(value)); 278 return this; 279 } 280 281 @Override 282 public JsonArrayBuilder set(int index, long value) { 283 setValueList(index, JsonNumberImpl.getJsonNumber(value)); 284 return this; 285 } 286 287 @Override 288 public JsonArrayBuilder set(int index, double value) { 289 setValueList(index, JsonNumberImpl.getJsonNumber(value)); 290 return this; 291 } 292 293 @Override 294 public JsonArrayBuilder set(int index, boolean value) { 295 setValueList(index, value ? JsonValue.TRUE : JsonValue.FALSE); 296 return this; 297 } 298 299 @Override 300 public JsonArrayBuilder setNull(int index) { 301 setValueList(index, JsonValue.NULL); 302 return this; 303 } 304 305 @Override 306 public JsonArrayBuilder set(int index, JsonObjectBuilder builder) { 307 if (builder == null) { 308 throw new NullPointerException(JsonMessages.ARRBUILDER_OBJECT_BUILDER_NULL()); 309 } 310 setValueList(index, builder.build()); 311 return this; 312 } 313 314 @Override 315 public JsonArrayBuilder set(int index, JsonArrayBuilder builder) { 316 if (builder == null) { 317 throw new NullPointerException(JsonMessages.ARRBUILDER_OBJECT_BUILDER_NULL()); 318 } 319 setValueList(index, builder.build()); 320 return this; 321 } 322 323 @Override 324 public JsonArrayBuilder remove(int index) { 325 if (valueList == null) { 326 throw new IndexOutOfBoundsException(JsonMessages.ARRBUILDER_VALUELIST_NULL(index, 0)); 327 } 328 valueList.remove(index); 329 return this; 330 } 331 332 @Override 132 333 public JsonArray build() { 133 334 List<JsonValue> snapshot; … … 143 344 } 144 345 346 private void populate(Collection<?> collection) { 347 for (Object value : collection) { 348 if (value != null && value instanceof Optional) { 349 ((Optional<?>) value).ifPresent(v -> 350 this.valueList.add(MapUtil.handle(v, bufferPool))); 351 } else { 352 this.valueList.add(MapUtil.handle(value, bufferPool)); 353 } 354 } 355 } 356 145 357 private void addValueList(JsonValue value) { 146 358 if (valueList == null) { 147 valueList = new ArrayList< JsonValue>();359 valueList = new ArrayList<>(); 148 360 } 149 361 valueList.add(value); 362 } 363 364 private void addValueList(int index, JsonValue value) { 365 if (valueList == null) { 366 valueList = new ArrayList<>(); 367 } 368 valueList.add(index, value); 369 } 370 371 private void setValueList(int index, JsonValue value) { 372 if (valueList == null) { 373 throw new IndexOutOfBoundsException(JsonMessages.ARRBUILDER_VALUELIST_NULL(index, 0)); 374 } 375 valueList.set(index, value); 150 376 } 151 377 … … 263 489 public String toString() { 264 490 StringWriter sw = new StringWriter(); 265 JsonWriter jw = new JsonWriterImpl(sw, bufferPool);266 jw.write(this);267 jw.close();491 try (JsonWriter jw = new JsonWriterImpl(sw, bufferPool)) { 492 jw.write(this); 493 } 268 494 return sw.toString(); 269 495 } 270 } 271 496 497 @Override 498 public JsonArray asJsonArray() { 499 return this; 500 } 501 } 272 502 } 273 503 274 275 -
trunk/src/org/glassfish/json/JsonBuilderFactoryImpl.java
r6756 r13231 2 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 3 * 4 * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.4 * Copyright (c) 2013-2017 Oracle and/or its affiliates. All rights reserved. 5 5 * 6 6 * The contents of this file are subject to the terms of either the GNU … … 9 9 * may not use this file except in compliance with the License. You can 10 10 * obtain a copy of the License at 11 * https:// glassfish.dev.java.net/public/CDDL+GPL_1_1.html12 * or packager/legal/LICENSE.txt. See the License for the specific11 * https://oss.oracle.com/licenses/CDDL+GPL-1.1 12 * or LICENSE.txt. See the License for the specific 13 13 * language governing permissions and limitations under the License. 14 14 * 15 15 * When distributing the software, include this License Header Notice in each 16 * file and include the License file at packager/legal/LICENSE.txt.16 * file and include the License file at LICENSE.txt. 17 17 * 18 18 * GPL Classpath Exception: … … 41 41 package org.glassfish.json; 42 42 43 import java.util.Collection; 43 44 import org.glassfish.json.api.BufferPool; 44 45 46 import javax.json.JsonObject; 47 import javax.json.JsonArray; 45 48 import javax.json.JsonArrayBuilder; 46 49 import javax.json.JsonBuilderFactory; … … 65 68 return new JsonObjectBuilderImpl(bufferPool); 66 69 } 70 71 @Override 72 public JsonObjectBuilder createObjectBuilder(JsonObject object) { 73 return new JsonObjectBuilderImpl(object, bufferPool); 74 } 75 76 @Override 77 public JsonObjectBuilder createObjectBuilder(Map<String, Object> object) { 78 return new JsonObjectBuilderImpl(object, bufferPool); 79 } 67 80 68 81 @Override … … 72 85 73 86 @Override 87 public JsonArrayBuilder createArrayBuilder(JsonArray array) { 88 return new JsonArrayBuilderImpl(array, bufferPool); 89 } 90 91 @Override 92 public JsonArrayBuilder createArrayBuilder(Collection<?> collection) { 93 return new JsonArrayBuilderImpl(collection, bufferPool); 94 } 95 96 @Override 74 97 public Map<String, ?> getConfigInUse() { 75 98 return config; -
trunk/src/org/glassfish/json/JsonGeneratorFactoryImpl.java
r6756 r13231 2 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 3 * 4 * Copyright (c) 2012-201 3Oracle and/or its affiliates. All rights reserved.4 * Copyright (c) 2012-2017 Oracle and/or its affiliates. All rights reserved. 5 5 * 6 6 * The contents of this file are subject to the terms of either the GNU … … 9 9 * may not use this file except in compliance with the License. You can 10 10 * obtain a copy of the License at 11 * https:// glassfish.dev.java.net/public/CDDL+GPL_1_1.html12 * or packager/legal/LICENSE.txt. See the License for the specific11 * https://oss.oracle.com/licenses/CDDL+GPL-1.1 12 * or LICENSE.txt. See the License for the specific 13 13 * language governing permissions and limitations under the License. 14 14 * 15 15 * When distributing the software, include this License Header Notice in each 16 * file and include the License file at packager/legal/LICENSE.txt.16 * file and include the License file at LICENSE.txt. 17 17 * 18 18 * GPL Classpath Exception: -
trunk/src/org/glassfish/json/JsonGeneratorImpl.java
r6756 r13231 2 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 3 * 4 * Copyright (c) 2012-201 3Oracle and/or its affiliates. All rights reserved.4 * Copyright (c) 2012-2017 Oracle and/or its affiliates. All rights reserved. 5 5 * 6 6 * The contents of this file are subject to the terms of either the GNU … … 9 9 * may not use this file except in compliance with the License. You can 10 10 * obtain a copy of the License at 11 * https:// glassfish.dev.java.net/public/CDDL+GPL_1_1.html12 * or packager/legal/LICENSE.txt. See the License for the specific11 * https://oss.oracle.com/licenses/CDDL+GPL-1.1 12 * or LICENSE.txt. See the License for the specific 13 13 * language governing permissions and limitations under the License. 14 14 * 15 15 * When distributing the software, include this License Header Notice in each 16 * file and include the License file at packager/legal/LICENSE.txt.16 * file and include the License file at LICENSE.txt. 17 17 * 18 18 * GPL Classpath Exception: … … 50 50 import java.math.BigInteger; 51 51 import java.nio.charset.Charset; 52 import java.nio.charset.StandardCharsets; 52 53 import java.util.ArrayDeque; 53 54 import java.util.Deque; … … 58 59 */ 59 60 class JsonGeneratorImpl implements JsonGenerator { 60 private static final Charset UTF_8 = Charset.forName("UTF-8");61 61 62 62 private static final char[] INT_MIN_VALUE_CHARS = "-2147483648".toCharArray(); … … 101 101 IN_NONE, 102 102 IN_OBJECT, 103 IN_FIELD, 103 104 IN_ARRAY 104 105 } … … 107 108 private final Writer writer; 108 109 private Context currentContext = new Context(Scope.IN_NONE); 109 private final Deque<Context> stack = new ArrayDeque< Context>();110 private final Deque<Context> stack = new ArrayDeque<>(); 110 111 111 112 // Using own buffering mechanism as JDK's BufferedWriter uses synchronized … … 122 123 123 124 JsonGeneratorImpl(OutputStream out, BufferPool bufferPool) { 124 this(out, UTF_8, bufferPool);125 this(out, StandardCharsets.UTF_8, bufferPool); 125 126 } 126 127 … … 170 171 writeComma(); 171 172 writeEscapedString(name); 172 writeC har(':');173 writeColon(); 173 174 return this; 174 175 } … … 267 268 @Override 268 269 public JsonGenerator write(JsonValue value) { 269 if (currentContext.scope != Scope.IN_ARRAY) { 270 throw new JsonGenerationException( 271 JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope)); 272 } 270 checkContextForValue(); 271 273 272 switch (value.getValueType()) { 274 273 case ARRAY: … … 295 294 JsonNumber number = (JsonNumber)value; 296 295 writeValue(number.toString()); 296 popFieldContext(); 297 297 break; 298 298 case TRUE: … … 382 382 } 383 383 384 @Override 384 385 public JsonGenerator write(String value) { 385 if (currentContext.scope != Scope.IN_ARRAY) { 386 throw new JsonGenerationException( 387 JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope)); 388 } 386 checkContextForValue(); 389 387 writeComma(); 390 388 writeEscapedString(value); 391 return this; 392 } 393 394 389 popFieldContext(); 390 return this; 391 } 392 393 394 @Override 395 395 public JsonGenerator write(int value) { 396 if (currentContext.scope != Scope.IN_ARRAY) { 397 throw new JsonGenerationException( 398 JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope)); 399 } 396 checkContextForValue(); 400 397 writeComma(); 401 398 writeInt(value); 399 popFieldContext(); 402 400 return this; 403 401 } … … 405 403 @Override 406 404 public JsonGenerator write(long value) { 407 if (currentContext.scope != Scope.IN_ARRAY) { 408 throw new JsonGenerationException( 409 JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope)); 410 } 405 checkContextForValue(); 411 406 writeValue(String.valueOf(value)); 407 popFieldContext(); 412 408 return this; 413 409 } … … 415 411 @Override 416 412 public JsonGenerator write(double value) { 417 if (currentContext.scope != Scope.IN_ARRAY) { 418 throw new JsonGenerationException( 419 JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope)); 420 } 413 checkContextForValue(); 421 414 if (Double.isInfinite(value) || Double.isNaN(value)) { 422 415 throw new NumberFormatException(JsonMessages.GENERATOR_DOUBLE_INFINITE_NAN()); 423 416 } 424 417 writeValue(String.valueOf(value)); 418 popFieldContext(); 425 419 return this; 426 420 } … … 428 422 @Override 429 423 public JsonGenerator write(BigInteger value) { 430 if (currentContext.scope != Scope.IN_ARRAY) { 431 throw new JsonGenerationException( 432 JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope)); 433 } 424 checkContextForValue(); 434 425 writeValue(value.toString()); 435 return this; 426 popFieldContext(); 427 return this; 428 } 429 430 private void checkContextForValue() { 431 if ((!currentContext.first && currentContext.scope != Scope.IN_ARRAY && currentContext.scope != Scope.IN_FIELD) 432 || (currentContext.first && currentContext.scope == Scope.IN_OBJECT)) { 433 throw new JsonGenerationException( 434 JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope)); 435 } 436 436 } 437 437 438 438 @Override 439 439 public JsonGenerator write(BigDecimal value) { 440 if (currentContext.scope != Scope.IN_ARRAY) { 441 throw new JsonGenerationException( 442 JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope)); 443 } 440 checkContextForValue(); 444 441 writeValue(value.toString()); 445 return this; 446 } 447 442 popFieldContext(); 443 444 return this; 445 } 446 447 private void popFieldContext() { 448 if (currentContext.scope == Scope.IN_FIELD) { 449 currentContext = stack.pop(); 450 } 451 } 452 453 @Override 448 454 public JsonGenerator write(boolean value) { 449 if (currentContext.scope != Scope.IN_ARRAY) { 450 throw new JsonGenerationException( 451 JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope)); 452 } 455 checkContextForValue(); 453 456 writeComma(); 454 457 writeString(value ? "true" : "false"); 455 return this; 456 } 457 458 popFieldContext(); 459 return this; 460 } 461 462 @Override 458 463 public JsonGenerator writeNull() { 459 if (currentContext.scope != Scope.IN_ARRAY) { 460 throw new JsonGenerationException( 461 JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope)); 462 } 464 checkContextForValue(); 463 465 writeComma(); 464 466 writeString("null"); 467 popFieldContext(); 465 468 return this; 466 469 } … … 474 477 writeComma(); 475 478 writeEscapedString(name); 476 writeC har(':');479 writeColon(); 477 480 writeString(value); 481 } 482 483 @Override 484 public JsonGenerator writeKey(String name) { 485 if (currentContext.scope != Scope.IN_OBJECT) { 486 throw new JsonGenerationException( 487 JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope)); 488 } 489 writeName(name); 490 stack.push(currentContext); 491 currentContext = new Context(Scope.IN_FIELD); 492 currentContext.first = false; 493 return this; 478 494 } 479 495 … … 485 501 writeChar(currentContext.scope == Scope.IN_ARRAY ? ']' : '}'); 486 502 currentContext = stack.pop(); 503 popFieldContext(); 487 504 return this; 488 505 } 489 506 490 507 protected void writeComma() { 491 if (!currentContext.first ) {508 if (!currentContext.first && currentContext.scope != Scope.IN_FIELD) { 492 509 writeChar(','); 493 510 } 494 511 currentContext.first = false; 512 } 513 514 protected void writeColon() { 515 writeChar(':'); 495 516 } 496 517 … … 505 526 } 506 527 528 @Override 507 529 public void close() { 508 530 if (currentContext.scope != Scope.IN_NONE || currentContext.first) { -
trunk/src/org/glassfish/json/JsonLocationImpl.java
r6756 r13231 2 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 3 * 4 * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.4 * Copyright (c) 2013-2017 Oracle and/or its affiliates. All rights reserved. 5 5 * 6 6 * The contents of this file are subject to the terms of either the GNU … … 9 9 * may not use this file except in compliance with the License. You can 10 10 * obtain a copy of the License at 11 * https:// glassfish.dev.java.net/public/CDDL+GPL_1_1.html12 * or packager/legal/LICENSE.txt. See the License for the specific11 * https://oss.oracle.com/licenses/CDDL+GPL-1.1 12 * or LICENSE.txt. See the License for the specific 13 13 * language governing permissions and limitations under the License. 14 14 * 15 15 * When distributing the software, include this License Header Notice in each 16 * file and include the License file at packager/legal/LICENSE.txt.16 * file and include the License file at LICENSE.txt. 17 17 * 18 18 * GPL Classpath Exception: … … 74 74 } 75 75 76 @Override 76 77 public String toString() { 77 78 return "(line no="+lineNo+", column no="+columnNo+", offset="+ offset +")"; -
trunk/src/org/glassfish/json/JsonMessages.java
r6756 r13231 2 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 3 * 4 * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.4 * Copyright (c) 2013-2017 Oracle and/or its affiliates. All rights reserved. 5 5 * 6 6 * The contents of this file are subject to the terms of either the GNU … … 9 9 * may not use this file except in compliance with the License. You can 10 10 * obtain a copy of the License at 11 * https:// glassfish.dev.java.net/public/CDDL+GPL_1_1.html12 * or packager/legal/LICENSE.txt. See the License for the specific11 * https://oss.oracle.com/licenses/CDDL+GPL-1.1 12 * or LICENSE.txt. See the License for the specific 13 13 * language governing permissions and limitations under the License. 14 14 * 15 15 * When distributing the software, include this License Header Notice in each 16 * file and include the License file at packager/legal/LICENSE.txt.16 * file and include the License file at LICENSE.txt. 17 17 * 18 18 * GPL Classpath Exception: … … 46 46 import java.text.MessageFormat; 47 47 import java.util.ResourceBundle; 48 import javax.json.JsonObject; 49 import javax.json.JsonValue; 48 50 49 51 /** … … 56 58 ResourceBundle.getBundle("org.glassfish.json.messages"); 57 59 60 // global/shared messages 61 static String INTERNAL_ERROR() { 62 return localize("internal.error"); 63 } 64 58 65 // tokenizer messages 59 66 static String TOKENIZER_UNEXPECTED_CHAR(int unexpected, JsonLocation location) { … … 91 98 } 92 99 100 static String PARSER_GETARRAY_ERR(JsonParser.Event event) { 101 return localize("parser.getArray.err", event); 102 } 103 104 static String PARSER_GETOBJECT_ERR(JsonParser.Event event) { 105 return localize("parser.getObject.err", event); 106 } 107 108 static String PARSER_GETVALUE_ERR(JsonParser.Event event) { 109 return localize("parser.getValue.err", event); 110 } 111 112 static String PARSER_GETVALUESTREAM_ERR() { 113 return localize("parser.getValueStream.err"); 114 } 115 93 116 static String PARSER_EXPECTED_EOF(JsonTokenizer.JsonToken token) { 94 117 return localize("parser.expected.eof", token); … … 103 126 } 104 127 128 static String PARSER_STATE_ERR(JsonValue.ValueType type) { 129 return localize("parser.state.err", type); 130 } 131 132 static String PARSER_SCOPE_ERR(JsonValue value) { 133 return localize("parser.scope.err", value); 134 } 135 136 static String PARSER_INPUT_ENC_DETECT_FAILED() { 137 return localize("parser.input.enc.detect.failed"); 138 } 139 140 static String PARSER_INPUT_ENC_DETECT_IOERR() { 141 return localize("parser.input.enc.detect.ioerr"); 142 } 105 143 106 144 // generator messages … … 140 178 } 141 179 142 143 180 // reader messages 144 181 static String READER_READ_ALREADY_CALLED() { … … 146 183 } 147 184 148 static String READER_EXPECTED_ARRAY_GOT_OBJECT() {149 return localize("reader.expected.array.got.object");150 }151 152 static String READER_EXPECTED_OBJECT_GOT_ARRAY() {153 return localize("reader.expected.object.got.array");154 }155 156 185 157 186 // obj builder messages … … 184 213 static String ARRBUILDER_ARRAY_BUILDER_NULL() { 185 214 return localize("arrbuilder.array.builder.null"); 215 } 216 217 static String ARRBUILDER_VALUELIST_NULL(int index, int size) { 218 return localize("arrbuilder.valuelist.null", index, size); 219 } 220 221 // json pointer messages 222 static String POINTER_FORMAT_INVALID() { 223 return localize("pointer.format.invalid"); 224 } 225 226 static String POINTER_MAPPING_MISSING(JsonObject object, String key) { 227 return localize("pointer.mapping.missing", object, key); 228 } 229 230 static String POINTER_REFERENCE_INVALID(JsonValue.ValueType type) { 231 return localize("pointer.reference.invalid", type.name()); 232 } 233 234 static String POINTER_ARRAY_INDEX_ERR(String token) { 235 return localize("pointer.array.index.err", token); 236 } 237 238 static String POINTER_ARRAY_INDEX_ILLEGAL(String token) { 239 return localize("pointer.array.index.illegal", token); 240 } 241 242 // nodereference messages 243 static String NODEREF_VALUE_ADD_ERR() { 244 return localize("noderef.value.add.err"); 245 } 246 247 static String NODEREF_VALUE_CANNOT_REMOVE() { 248 return localize("noderef.value.cannot.remove"); 249 } 250 251 static String NODEREF_OBJECT_MISSING(String key) { 252 return localize("noderef.object.missing", key); 253 } 254 255 static String NODEREF_ARRAY_INDEX_ERR(int index, int size) { 256 return localize("noderef.array.index.err", index, size); 257 } 258 259 // json patch messages 260 static String PATCH_MUST_BE_ARRAY() { 261 return localize("patch.must.be.array"); 262 } 263 264 static String PATCH_MOVE_PROPER_PREFIX(String from, String path) { 265 return localize("patch.move.proper.prefix", from, path); 266 } 267 268 static String PATCH_MOVE_TARGET_NULL(String from) { 269 return localize("patch.move.target.null", from); 270 } 271 272 static String PATCH_TEST_FAILED(String path, String value) { 273 return localize("patch.test.failed", path, value); 274 } 275 276 static String PATCH_ILLEGAL_OPERATION(String operation) { 277 return localize("patch.illegal.operation", operation); 278 } 279 280 static String PATCH_MEMBER_MISSING(String operation, String member) { 281 return localize("patch.member.missing", operation, member); 186 282 } 187 283 -
trunk/src/org/glassfish/json/JsonNumberImpl.java
r6756 r13231 2 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 3 * 4 * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.4 * Copyright (c) 2013-2017 Oracle and/or its affiliates. All rights reserved. 5 5 * 6 6 * The contents of this file are subject to the terms of either the GNU … … 9 9 * may not use this file except in compliance with the License. You can 10 10 * obtain a copy of the License at 11 * https:// glassfish.dev.java.net/public/CDDL+GPL_1_1.html12 * or packager/legal/LICENSE.txt. See the License for the specific11 * https://oss.oracle.com/licenses/CDDL+GPL-1.1 12 * or LICENSE.txt. See the License for the specific 13 13 * language governing permissions and limitations under the License. 14 14 * 15 15 * When distributing the software, include this License Header Notice in each 16 * file and include the License file at packager/legal/LICENSE.txt.16 * file and include the License file at LICENSE.txt. 17 17 * 18 18 * GPL Classpath Exception: … … 126 126 127 127 @Override 128 public Number numberValue() { 129 return num; 130 } 131 132 @Override 128 133 public String toString() { 129 134 return Integer.toString(num); … … 143 148 public boolean isIntegral() { 144 149 return true; 150 } 151 152 @Override 153 public int intValue() { 154 return (int) num; 155 } 156 157 @Override 158 public int intValueExact() { 159 return Math.toIntExact(num); 145 160 } 146 161 … … 172 187 173 188 @Override 189 public Number numberValue() { 190 return num; 191 } 192 193 @Override 174 194 public String toString() { 175 195 return Long.toString(num); … … 191 211 } 192 212 213 @Override 214 public Number numberValue() { 215 return bigDecimalValue(); 216 } 217 193 218 } 194 219 … … 245 270 @Override 246 271 public boolean equals(Object obj) { 272 if (this == obj){ 273 return true; 274 } 247 275 if (!(obj instanceof JsonNumber)) { 248 276 return false; -
trunk/src/org/glassfish/json/JsonObjectBuilderImpl.java
r6756 r13231 2 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 3 * 4 * Copyright (c) 2012-201 3Oracle and/or its affiliates. All rights reserved.4 * Copyright (c) 2012-2017 Oracle and/or its affiliates. All rights reserved. 5 5 * 6 6 * The contents of this file are subject to the terms of either the GNU … … 9 9 * may not use this file except in compliance with the License. You can 10 10 * obtain a copy of the License at 11 * https:// glassfish.dev.java.net/public/CDDL+GPL_1_1.html12 * or packager/legal/LICENSE.txt. See the License for the specific11 * https://oss.oracle.com/licenses/CDDL+GPL-1.1 12 * or LICENSE.txt. See the License for the specific 13 13 * language governing permissions and limitations under the License. 14 14 * 15 15 * When distributing the software, include this License Header Notice in each 16 * file and include the License file at packager/legal/LICENSE.txt.16 * file and include the License file at LICENSE.txt. 17 17 * 18 18 * GPL Classpath Exception: … … 51 51 52 52 /** 53 * JsonObjectBuilder impl 53 * JsonObjectBuilder implementation 54 54 * 55 55 * @author Jitendra Kotamraju 56 * @author Kin-man Chung 56 57 */ 57 58 class JsonObjectBuilderImpl implements JsonObjectBuilder { 59 58 60 private Map<String, JsonValue> valueMap; 59 61 private final BufferPool bufferPool; … … 63 65 } 64 66 67 JsonObjectBuilderImpl(JsonObject object, BufferPool bufferPool) { 68 this.bufferPool = bufferPool; 69 valueMap = new LinkedHashMap<>(); 70 valueMap.putAll(object); 71 } 72 73 JsonObjectBuilderImpl(Map<String, Object> map, BufferPool bufferPool) { 74 this.bufferPool = bufferPool; 75 valueMap = new LinkedHashMap<>(); 76 populate(map); 77 } 78 79 @Override 65 80 public JsonObjectBuilder add(String name, JsonValue value) { 66 81 validateName(name); … … 70 85 } 71 86 87 @Override 72 88 public JsonObjectBuilder add(String name, String value) { 73 89 validateName(name); … … 77 93 } 78 94 95 @Override 79 96 public JsonObjectBuilder add(String name, BigInteger value) { 80 97 validateName(name); … … 84 101 } 85 102 103 @Override 86 104 public JsonObjectBuilder add(String name, BigDecimal value) { 87 105 validateName(name); … … 91 109 } 92 110 111 @Override 93 112 public JsonObjectBuilder add(String name, int value) { 94 113 validateName(name); … … 97 116 } 98 117 118 @Override 99 119 public JsonObjectBuilder add(String name, long value) { 100 120 validateName(name); … … 103 123 } 104 124 125 @Override 105 126 public JsonObjectBuilder add(String name, double value) { 106 127 validateName(name); … … 109 130 } 110 131 132 @Override 111 133 public JsonObjectBuilder add(String name, boolean value) { 112 134 validateName(name); … … 115 137 } 116 138 139 @Override 117 140 public JsonObjectBuilder addNull(String name) { 118 141 validateName(name); … … 121 144 } 122 145 146 @Override 123 147 public JsonObjectBuilder add(String name, JsonObjectBuilder builder) { 124 148 validateName(name); … … 130 154 } 131 155 156 @Override 132 157 public JsonObjectBuilder add(String name, JsonArrayBuilder builder) { 133 158 validateName(name); … … 139 164 } 140 165 166 @Override 167 public JsonObjectBuilder addAll(JsonObjectBuilder builder) { 168 if (builder == null) { 169 throw new NullPointerException(JsonMessages.OBJBUILDER_OBJECT_BUILDER_NULL()); 170 } 171 if (valueMap == null) { 172 this.valueMap = new LinkedHashMap<>(); 173 } 174 this.valueMap.putAll(builder.build()); 175 return this; 176 } 177 178 @Override 179 public JsonObjectBuilder remove(String name) { 180 validateName(name); 181 this.valueMap.remove(name); 182 return this; 183 } 184 185 @Override 141 186 public JsonObject build() { 142 187 Map<String, JsonValue> snapshot = (valueMap == null) … … 147 192 } 148 193 194 private void populate(Map<String, Object> map) { 195 final Set<String> fields = map.keySet(); 196 for (String field : fields) { 197 Object value = map.get(field); 198 if (value != null && value instanceof Optional) { 199 ((Optional<?>) value).ifPresent(v -> 200 this.valueMap.put(field, MapUtil.handle(v, bufferPool))); 201 } else { 202 this.valueMap.put(field, MapUtil.handle(value, bufferPool)); 203 } 204 } 205 } 206 149 207 private void putValueMap(String name, JsonValue value) { 150 208 if (valueMap == null) { 151 this.valueMap = new LinkedHashMap< String, JsonValue>();209 this.valueMap = new LinkedHashMap<>(); 152 210 } 153 211 valueMap.put(name, value); … … 264 322 public String toString() { 265 323 StringWriter sw = new StringWriter(); 266 JsonWriter jw = new JsonWriterImpl(sw, bufferPool);267 jw.write(this);268 jw.close();324 try (JsonWriter jw = new JsonWriterImpl(sw, bufferPool)) { 325 jw.write(this); 326 } 269 327 return sw.toString(); 270 328 } 329 330 @Override 331 public JsonObject asJsonObject() { 332 return this; 333 } 334 335 @Override 336 public int size() { 337 return valueMap.size(); 338 } 339 340 @Override 341 public JsonValue get(Object key) { 342 return valueMap.get(key); 343 } 344 345 @Override 346 public boolean containsKey(Object key) { 347 return valueMap.containsKey(key); 348 } 271 349 } 272 350 -
trunk/src/org/glassfish/json/JsonParserFactoryImpl.java
r6756 r13231 2 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 3 * 4 * Copyright (c) 2012-201 3Oracle and/or its affiliates. All rights reserved.4 * Copyright (c) 2012-2017 Oracle and/or its affiliates. All rights reserved. 5 5 * 6 6 * The contents of this file are subject to the terms of either the GNU … … 9 9 * may not use this file except in compliance with the License. You can 10 10 * obtain a copy of the License at 11 * https:// glassfish.dev.java.net/public/CDDL+GPL_1_1.html12 * or packager/legal/LICENSE.txt. See the License for the specific11 * https://oss.oracle.com/licenses/CDDL+GPL-1.1 12 * or LICENSE.txt. See the License for the specific 13 13 * language governing permissions and limitations under the License. 14 14 * 15 15 * When distributing the software, include this License Header Notice in each 16 * file and include the License file at packager/legal/LICENSE.txt.16 * file and include the License file at LICENSE.txt. 17 17 * 18 18 * GPL Classpath Exception: -
trunk/src/org/glassfish/json/JsonParserImpl.java
r6756 r13231 2 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 3 * 4 * Copyright (c) 2012-201 3Oracle and/or its affiliates. All rights reserved.4 * Copyright (c) 2012-2017 Oracle and/or its affiliates. All rights reserved. 5 5 * 6 6 * The contents of this file are subject to the terms of either the GNU … … 9 9 * may not use this file except in compliance with the License. You can 10 10 * obtain a copy of the License at 11 * https:// glassfish.dev.java.net/public/CDDL+GPL_1_1.html12 * or packager/legal/LICENSE.txt. See the License for the specific11 * https://oss.oracle.com/licenses/CDDL+GPL-1.1 12 * or LICENSE.txt. See the License for the specific 13 13 * language governing permissions and limitations under the License. 14 14 * 15 15 * When distributing the software, include this License Header Notice in each 16 * file and include the License file at packager/legal/LICENSE.txt.16 * file and include the License file at LICENSE.txt. 17 17 * 18 18 * GPL Classpath Exception: … … 41 41 package org.glassfish.json; 42 42 43 import javax.json.*; 43 import java.io.IOException; 44 import java.io.InputStream; 45 import java.io.InputStreamReader; 46 import java.io.Reader; 47 import java.math.BigDecimal; 48 import java.nio.charset.Charset; 49 import java.util.AbstractMap; 50 import java.util.Map; 51 import java.util.NoSuchElementException; 52 import java.util.Spliterator; 53 import java.util.Spliterators; 54 import java.util.function.Consumer; 55 import java.util.stream.Stream; 56 import java.util.stream.StreamSupport; 57 58 import javax.json.JsonArray; 59 import javax.json.JsonArrayBuilder; 60 import javax.json.JsonException; 61 import javax.json.JsonObject; 62 import javax.json.JsonObjectBuilder; 63 import javax.json.JsonValue; 44 64 import javax.json.stream.JsonLocation; 45 65 import javax.json.stream.JsonParser; 66 import javax.json.stream.JsonParser.Event; 46 67 import javax.json.stream.JsonParsingException; 47 import java.io.*;48 import java.math.BigDecimal;49 import java.nio.charset.Charset;50 import java.util.*;51 68 52 69 import org.glassfish.json.JsonTokenizer.JsonToken; … … 58 75 * 59 76 * @author Jitendra Kotamraju 77 * @author Kin-man Chung 60 78 */ 61 79 public class JsonParserImpl implements JsonParser { 62 80 81 private final BufferPool bufferPool; 63 82 private Context currentContext = new NoneContext(); 64 83 private Event currentEvent; 65 84 66 85 private final Stack stack = new Stack(); 67 private final StateIterator stateIterator;68 86 private final JsonTokenizer tokenizer; 69 87 70 88 public JsonParserImpl(Reader reader, BufferPool bufferPool) { 89 this.bufferPool = bufferPool; 71 90 tokenizer = new JsonTokenizer(reader, bufferPool); 72 stateIterator = new StateIterator();73 91 } 74 92 75 93 public JsonParserImpl(InputStream in, BufferPool bufferPool) { 94 this.bufferPool = bufferPool; 76 95 UnicodeDetectingInputStream uin = new UnicodeDetectingInputStream(in); 77 96 tokenizer = new JsonTokenizer(new InputStreamReader(uin, uin.getCharset()), bufferPool); 78 stateIterator = new StateIterator();79 97 } 80 98 81 99 public JsonParserImpl(InputStream in, Charset encoding, BufferPool bufferPool) { 100 this.bufferPool = bufferPool; 82 101 tokenizer = new JsonTokenizer(new InputStreamReader(in, encoding), bufferPool); 83 stateIterator = new StateIterator();84 } 85 102 } 103 104 @Override 86 105 public String getString() { 87 106 if (currentEvent == Event.KEY_NAME || currentEvent == Event.VALUE_STRING … … 115 134 } 116 135 136 boolean isDefinitelyLong() { 137 return tokenizer.isDefinitelyLong(); 138 } 139 117 140 @Override 118 141 public long getLong() { … … 121 144 JsonMessages.PARSER_GETLONG_ERR(currentEvent)); 122 145 } 123 return tokenizer.get BigDecimal().longValue();146 return tokenizer.getLong(); 124 147 } 125 148 … … 134 157 135 158 @Override 159 public JsonArray getArray() { 160 if (currentEvent != Event.START_ARRAY) { 161 throw new IllegalStateException( 162 JsonMessages.PARSER_GETARRAY_ERR(currentEvent)); 163 } 164 return getArray(new JsonArrayBuilderImpl(bufferPool)); 165 } 166 167 @Override 168 public JsonObject getObject() { 169 if (currentEvent != Event.START_OBJECT) { 170 throw new IllegalStateException( 171 JsonMessages.PARSER_GETOBJECT_ERR(currentEvent)); 172 } 173 return getObject(new JsonObjectBuilderImpl(bufferPool)); 174 } 175 176 @Override 177 public JsonValue getValue() { 178 switch (currentEvent) { 179 case START_ARRAY: 180 return getArray(new JsonArrayBuilderImpl(bufferPool)); 181 case START_OBJECT: 182 return getObject(new JsonObjectBuilderImpl(bufferPool)); 183 case KEY_NAME: 184 case VALUE_STRING: 185 return new JsonStringImpl(getString()); 186 case VALUE_NUMBER: 187 if (isDefinitelyInt()) { 188 return JsonNumberImpl.getJsonNumber(getInt()); 189 } else if (isDefinitelyLong()) { 190 return JsonNumberImpl.getJsonNumber(getLong()); 191 } 192 return JsonNumberImpl.getJsonNumber(getBigDecimal()); 193 case VALUE_TRUE: 194 return JsonValue.TRUE; 195 case VALUE_FALSE: 196 return JsonValue.FALSE; 197 case VALUE_NULL: 198 return JsonValue.NULL; 199 case END_ARRAY: 200 case END_OBJECT: 201 default: 202 throw new IllegalStateException(JsonMessages.PARSER_GETVALUE_ERR(currentEvent)); 203 } 204 } 205 206 @Override 207 public Stream<JsonValue> getArrayStream() { 208 if (currentEvent != Event.START_ARRAY) { 209 throw new IllegalStateException( 210 JsonMessages.PARSER_GETARRAY_ERR(currentEvent)); 211 } 212 Spliterator<JsonValue> spliterator = 213 new Spliterators.AbstractSpliterator<JsonValue>(Long.MAX_VALUE, Spliterator.ORDERED) { 214 @Override 215 public Spliterator<JsonValue> trySplit() { 216 return null; 217 } 218 @Override 219 public boolean tryAdvance(Consumer<? super JsonValue> action) { 220 if (action == null) { 221 throw new NullPointerException(); 222 } 223 if (! hasNext()) { 224 return false; 225 } 226 if (next() == JsonParser.Event.END_ARRAY) { 227 return false; 228 } 229 action.accept(getValue()); 230 return true; 231 } 232 }; 233 return StreamSupport.stream(spliterator, false); 234 } 235 236 @Override 237 public Stream<Map.Entry<String, JsonValue>> getObjectStream() { 238 if (currentEvent != Event.START_OBJECT) { 239 throw new IllegalStateException( 240 JsonMessages.PARSER_GETOBJECT_ERR(currentEvent)); 241 } 242 Spliterator<Map.Entry<String, JsonValue>> spliterator = 243 new Spliterators.AbstractSpliterator<Map.Entry<String, JsonValue>>(Long.MAX_VALUE, Spliterator.ORDERED) { 244 @Override 245 public Spliterator<Map.Entry<String,JsonValue>> trySplit() { 246 return null; 247 } 248 @Override 249 public boolean tryAdvance(Consumer<? super Map.Entry<String, JsonValue>> action) { 250 if (action == null) { 251 throw new NullPointerException(); 252 } 253 if (! hasNext()) { 254 return false; 255 } 256 JsonParser.Event e = next(); 257 if (e == JsonParser.Event.END_OBJECT) { 258 return false; 259 } 260 if (e != JsonParser.Event.KEY_NAME) { 261 throw new JsonException(JsonMessages.INTERNAL_ERROR()); 262 } 263 String key = getString(); 264 if (! hasNext()) { 265 throw new JsonException(JsonMessages.INTERNAL_ERROR()); 266 } 267 next(); 268 JsonValue value = getValue(); 269 action.accept(new AbstractMap.SimpleImmutableEntry<>(key, value)); 270 return true; 271 } 272 }; 273 return StreamSupport.stream(spliterator, false); 274 } 275 276 @Override 277 public Stream<JsonValue> getValueStream() { 278 if (! (currentContext instanceof NoneContext)) { 279 throw new IllegalStateException( 280 JsonMessages.PARSER_GETVALUESTREAM_ERR()); 281 } 282 Spliterator<JsonValue> spliterator = 283 new Spliterators.AbstractSpliterator<JsonValue>(Long.MAX_VALUE, Spliterator.ORDERED) { 284 @Override 285 public Spliterator<JsonValue> trySplit() { 286 return null; 287 } 288 @Override 289 public boolean tryAdvance(Consumer<? super JsonValue> action) { 290 if (action == null) { 291 throw new NullPointerException(); 292 } 293 if (! hasNext()) { 294 return false; 295 } 296 next(); 297 action.accept(getValue()); 298 return true; 299 } 300 }; 301 return StreamSupport.stream(spliterator, false); 302 } 303 304 @Override 305 public void skipArray() { 306 if (currentEvent == Event.START_ARRAY) { 307 currentContext.skip(); 308 currentContext = stack.pop(); 309 } 310 } 311 312 @Override 313 public void skipObject() { 314 if (currentEvent == Event.START_OBJECT) { 315 currentContext.skip(); 316 currentContext = stack.pop(); 317 } 318 } 319 320 private JsonArray getArray(JsonArrayBuilder builder) { 321 while(hasNext()) { 322 JsonParser.Event e = next(); 323 if (e == JsonParser.Event.END_ARRAY) { 324 return builder.build(); 325 } 326 builder.add(getValue()); 327 } 328 throw parsingException(JsonToken.EOF, "[CURLYOPEN, SQUAREOPEN, STRING, NUMBER, TRUE, FALSE, NULL, SQUARECLOSE]"); 329 } 330 331 private JsonObject getObject(JsonObjectBuilder builder) { 332 while(hasNext()) { 333 JsonParser.Event e = next(); 334 if (e == JsonParser.Event.END_OBJECT) { 335 return builder.build(); 336 } 337 String key = getString(); 338 next(); 339 builder.add(key, getValue()); 340 } 341 throw parsingException(JsonToken.EOF, "[STRING, CURLYCLOSE]"); 342 } 343 344 @Override 136 345 public JsonLocation getLocation() { 137 346 return tokenizer.getLocation(); … … 142 351 } 143 352 353 @Override 144 354 public boolean hasNext() { 145 return stateIterator.hasNext(); 146 } 147 355 return tokenizer.hasNextToken(); 356 } 357 358 @Override 148 359 public Event next() { 149 return stateIterator.next(); 150 } 151 152 private class StateIterator implements Iterator<JsonParser.Event> { 153 154 @Override 155 public boolean hasNext() { 156 if (stack.isEmpty() && (currentEvent == Event.END_ARRAY || currentEvent == Event.END_OBJECT)) { 157 JsonToken token = tokenizer.nextToken(); 158 if (token != JsonToken.EOF) { 159 throw new JsonParsingException(JsonMessages.PARSER_EXPECTED_EOF(token), 160 getLastCharLocation()); 161 } 162 return false; 163 } 164 return true; 165 } 166 167 @Override 168 public JsonParser.Event next() { 169 if (!hasNext()) { 170 throw new NoSuchElementException(); 171 } 172 return currentEvent = currentContext.getNextEvent(); 173 } 174 175 @Override 176 public void remove() { 177 throw new UnsupportedOperationException(); 178 } 179 } 180 360 if (!hasNext()) { 361 throw new NoSuchElementException(); 362 } 363 return currentEvent = currentContext.getNextEvent(); 364 } 365 366 @Override 181 367 public void close() { 182 368 try { … … 206 392 } 207 393 394 private Context peek() { 395 return head; 396 } 397 208 398 private boolean isEmpty() { 209 399 return head == null; … … 214 404 Context next; 215 405 abstract Event getNextEvent(); 406 abstract void skip(); 216 407 } 217 408 … … 219 410 @Override 220 411 public Event getNextEvent() { 221 // Handle 1. { 2. [412 // Handle 1. { 2. [ 3. value 222 413 JsonToken token = tokenizer.nextToken(); 223 414 if (token == JsonToken.CURLYOPEN) { … … 229 420 currentContext = new ArrayContext(); 230 421 return Event.START_ARRAY; 231 } 232 throw parsingException(token, "[CURLYOPEN, SQUAREOPEN]"); 422 } else if (token.isValue()) { 423 return token.getEvent(); 424 } 425 throw parsingException(token, "[CURLYOPEN, SQUAREOPEN, STRING, NUMBER, TRUE, FALSE, NULL]"); 426 } 427 428 @Override 429 void skip() { 430 // no-op 233 431 } 234 432 } … … 293 491 } 294 492 493 @Override 494 void skip() { 495 JsonToken token; 496 int depth = 1; 497 do { 498 token = tokenizer.nextToken(); 499 switch (token) { 500 case CURLYCLOSE: 501 depth--; 502 break; 503 case CURLYOPEN: 504 depth++; 505 break; 506 } 507 } while (!(token == JsonToken.CURLYCLOSE && depth == 0)); 508 } 509 295 510 } 296 511 … … 328 543 } 329 544 545 @Override 546 void skip() { 547 JsonToken token; 548 int depth = 1; 549 do { 550 token = tokenizer.nextToken(); 551 switch (token) { 552 case SQUARECLOSE: 553 depth--; 554 break; 555 case SQUAREOPEN: 556 depth++; 557 break; 558 } 559 } while (!(token == JsonToken.SQUARECLOSE && depth == 0)); 560 } 330 561 } 331 562 -
trunk/src/org/glassfish/json/JsonPrettyGeneratorImpl.java
r6756 r13231 2 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 3 * 4 * Copyright (c) 2012-201 3Oracle and/or its affiliates. All rights reserved.4 * Copyright (c) 2012-2017 Oracle and/or its affiliates. All rights reserved. 5 5 * 6 6 * The contents of this file are subject to the terms of either the GNU … … 9 9 * may not use this file except in compliance with the License. You can 10 10 * obtain a copy of the License at 11 * https:// glassfish.dev.java.net/public/CDDL+GPL_1_1.html12 * or packager/legal/LICENSE.txt. See the License for the specific11 * https://oss.oracle.com/licenses/CDDL+GPL-1.1 12 * or LICENSE.txt. See the License for the specific 13 13 * language governing permissions and limitations under the License. 14 14 * 15 15 * When distributing the software, include this License Header Notice in each 16 * file and include the License file at packager/legal/LICENSE.txt.16 * file and include the License file at LICENSE.txt. 17 17 * 18 18 * GPL Classpath Exception: … … 117 117 } 118 118 119 @Override 120 protected void writeColon() { 121 super.writeColon(); 122 writeChar(' '); 123 } 124 119 125 private void writeNewLine() { 120 126 writeChar('\n'); -
trunk/src/org/glassfish/json/JsonProviderImpl.java
r6756 r13231 2 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 3 * 4 * Copyright (c) 2012-201 3Oracle and/or its affiliates. All rights reserved.4 * Copyright (c) 2012-2017 Oracle and/or its affiliates. All rights reserved. 5 5 * 6 6 * The contents of this file are subject to the terms of either the GNU … … 9 9 * may not use this file except in compliance with the License. You can 10 10 * obtain a copy of the License at 11 * https:// glassfish.dev.java.net/public/CDDL+GPL_1_1.html12 * or packager/legal/LICENSE.txt. See the License for the specific11 * https://oss.oracle.com/licenses/CDDL+GPL-1.1 12 * or LICENSE.txt. See the License for the specific 13 13 * language governing permissions and limitations under the License. 14 14 * 15 15 * When distributing the software, include this License Header Notice in each 16 * file and include the License file at packager/legal/LICENSE.txt.16 * file and include the License file at LICENSE.txt. 17 17 * 18 18 * GPL Classpath Exception: … … 53 53 import java.io.Reader; 54 54 import java.io.Writer; 55 import java.util.Collection; 55 56 import java.util.Collections; 56 57 import java.util.HashMap; 57 58 import java.util.Map; 59 import java.math.BigDecimal; 60 import java.math.BigInteger; 58 61 59 62 /** 60 63 * @author Jitendra Kotamraju 64 * @author Kin-man Chung 65 * @author Alex Soto 61 66 */ 62 67 public class JsonProviderImpl extends JsonProvider { … … 106 111 pool = bufferPool; 107 112 } else { 108 providerConfig = new HashMap< String, Object>();113 providerConfig = new HashMap<>(); 109 114 if (prettyPrinting=JsonProviderImpl.isPrettyPrintingEnabled(config)) { 110 115 providerConfig.put(JsonGenerator.PRETTY_PRINTING, true); … … 152 157 pool = bufferPool; 153 158 } else { 154 providerConfig = new HashMap< String, Object>();159 providerConfig = new HashMap<>(); 155 160 if (prettyPrinting=JsonProviderImpl.isPrettyPrintingEnabled(config)) { 156 161 providerConfig.put(JsonGenerator.PRETTY_PRINTING, true); … … 185 190 186 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 187 202 public JsonArrayBuilder createArrayBuilder() { 188 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); 189 279 } 190 280 -
trunk/src/org/glassfish/json/JsonReaderFactoryImpl.java
r6756 r13231 2 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 3 * 4 * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.4 * Copyright (c) 2013-2017 Oracle and/or its affiliates. All rights reserved. 5 5 * 6 6 * The contents of this file are subject to the terms of either the GNU … … 9 9 * may not use this file except in compliance with the License. You can 10 10 * obtain a copy of the License at 11 * https:// glassfish.dev.java.net/public/CDDL+GPL_1_1.html12 * or packager/legal/LICENSE.txt. See the License for the specific11 * https://oss.oracle.com/licenses/CDDL+GPL-1.1 12 * or LICENSE.txt. See the License for the specific 13 13 * language governing permissions and limitations under the License. 14 14 * 15 15 * When distributing the software, include this License Header Notice in each 16 * file and include the License file at packager/legal/LICENSE.txt.16 * file and include the License file at LICENSE.txt. 17 17 * 18 18 * GPL Classpath Exception: -
trunk/src/org/glassfish/json/JsonReaderImpl.java
r6756 r13231 2 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 3 * 4 * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.4 * Copyright (c) 2013-2017 Oracle and/or its affiliates. All rights reserved. 5 5 * 6 6 * The contents of this file are subject to the terms of either the GNU … … 9 9 * may not use this file except in compliance with the License. You can 10 10 * obtain a copy of the License at 11 * https:// glassfish.dev.java.net/public/CDDL+GPL_1_1.html12 * or packager/legal/LICENSE.txt. See the License for the specific11 * https://oss.oracle.com/licenses/CDDL+GPL-1.1 12 * or LICENSE.txt. See the License for the specific 13 13 * language governing permissions and limitations under the License. 14 14 * 15 15 * When distributing the software, include this License Header Notice in each 16 * file and include the License file at packager/legal/LICENSE.txt.16 * file and include the License file at LICENSE.txt. 17 17 * 18 18 * GPL Classpath Exception: … … 43 43 import org.glassfish.json.api.BufferPool; 44 44 45 import javax.json.*;46 import javax.json.stream.JsonParser;47 45 import java.io.InputStream; 48 46 import java.io.Reader; 49 import java.math.BigDecimal;50 47 import java.nio.charset.Charset; 48 import javax.json.JsonArray; 49 import javax.json.JsonException; 50 import javax.json.JsonObject; 51 import javax.json.JsonReader; 52 import javax.json.JsonStructure; 53 import javax.json.JsonValue; 54 import javax.json.stream.JsonParser; 55 import javax.json.stream.JsonParsingException; 51 56 52 57 /** … … 82 87 readDone = true; 83 88 if (parser.hasNext()) { 84 JsonParser.Event e = parser.next(); 85 if (e == JsonParser.Event.START_ARRAY) { 86 return readArray(new JsonArrayBuilderImpl(bufferPool)); 87 } else if (e == JsonParser.Event.START_OBJECT) { 88 return readObject(new JsonObjectBuilderImpl(bufferPool)); 89 try { 90 JsonParser.Event e = parser.next(); 91 if (e == JsonParser.Event.START_ARRAY) { 92 return parser.getArray(); 93 } else if (e == JsonParser.Event.START_OBJECT) { 94 return parser.getObject(); 95 } 96 } catch (IllegalStateException ise) { 97 throw new JsonParsingException(ise.getMessage(), ise, parser.getLastCharLocation()); 89 98 } 90 99 } 91 throw new JsonException( "Internal Error");100 throw new JsonException(JsonMessages.INTERNAL_ERROR()); 92 101 } 93 102 … … 99 108 readDone = true; 100 109 if (parser.hasNext()) { 101 JsonParser.Event e = parser.next();102 if (e == JsonParser.Event.START_OBJECT) {103 return readObject(new JsonObjectBuilderImpl(bufferPool));104 } else if (e == JsonParser.Event.START_ARRAY) {105 throw new Json Exception(JsonMessages.READER_EXPECTED_OBJECT_GOT_ARRAY());110 try { 111 parser.next(); 112 return parser.getObject(); 113 } catch (IllegalStateException ise) { 114 throw new JsonParsingException(ise.getMessage(), ise, parser.getLastCharLocation()); 106 115 } 107 116 } 108 throw new JsonException( "Internal Error");117 throw new JsonException(JsonMessages.INTERNAL_ERROR()); 109 118 } 110 119 … … 116 125 readDone = true; 117 126 if (parser.hasNext()) { 118 JsonParser.Event e = parser.next();119 if (e == JsonParser.Event.START_ARRAY) {120 return readArray(new JsonArrayBuilderImpl(bufferPool));121 } else if (e == JsonParser.Event.START_OBJECT) {122 throw new Json Exception(JsonMessages.READER_EXPECTED_ARRAY_GOT_OBJECT());127 try { 128 parser.next(); 129 return parser.getArray(); 130 } catch (IllegalStateException ise) { 131 throw new JsonParsingException(ise.getMessage(), ise, parser.getLastCharLocation()); 123 132 } 124 133 } 125 throw new JsonException("Internal Error"); 134 throw new JsonException(JsonMessages.INTERNAL_ERROR()); 135 } 136 137 @Override 138 public JsonValue readValue() { 139 if (readDone) { 140 throw new IllegalStateException(JsonMessages.READER_READ_ALREADY_CALLED()); 141 } 142 readDone = true; 143 if (parser.hasNext()) { 144 try { 145 parser.next(); 146 return parser.getValue(); 147 } catch (IllegalStateException ise) { 148 throw new JsonParsingException(ise.getMessage(), ise, parser.getLastCharLocation()); 149 } 150 } 151 throw new JsonException(JsonMessages.INTERNAL_ERROR()); 126 152 } 127 153 … … 131 157 parser.close(); 132 158 } 133 134 private JsonArray readArray(JsonArrayBuilder builder) {135 while(parser.hasNext()) {136 JsonParser.Event e = parser.next();137 switch (e) {138 case START_ARRAY:139 JsonArray array = readArray(new JsonArrayBuilderImpl(bufferPool));140 builder.add(array);141 break;142 case START_OBJECT:143 JsonObject object = readObject(new JsonObjectBuilderImpl(bufferPool));144 builder.add(object);145 break;146 case VALUE_STRING:147 builder.add(parser.getString());148 break;149 case VALUE_NUMBER:150 if (parser.isDefinitelyInt()) {151 builder.add(parser.getInt());152 } else {153 builder.add(parser.getBigDecimal());154 }155 break;156 case VALUE_TRUE:157 builder.add(JsonValue.TRUE);158 break;159 case VALUE_FALSE:160 builder.add(JsonValue.FALSE);161 break;162 case VALUE_NULL:163 builder.addNull();164 break;165 case END_ARRAY:166 return builder.build();167 default:168 throw new JsonException("Internal Error");169 }170 }171 throw new JsonException("Internal Error");172 }173 174 private JsonObject readObject(JsonObjectBuilder builder) {175 String key = null;176 while(parser.hasNext()) {177 JsonParser.Event e = parser .next();178 switch (e) {179 case START_ARRAY:180 JsonArray array = readArray(new JsonArrayBuilderImpl(bufferPool));181 builder.add(key, array);182 break;183 case START_OBJECT:184 JsonObject object = readObject(new JsonObjectBuilderImpl(bufferPool));185 builder.add(key, object);186 break;187 case KEY_NAME:188 key = parser.getString();189 break;190 case VALUE_STRING:191 builder.add(key, parser.getString());192 break;193 case VALUE_NUMBER:194 if (parser.isDefinitelyInt()) {195 builder.add(key, parser.getInt());196 } else {197 builder.add(key, parser.getBigDecimal());198 }199 break;200 case VALUE_TRUE:201 builder.add(key, JsonValue.TRUE);202 break;203 case VALUE_FALSE:204 builder.add(key, JsonValue.FALSE);205 break;206 case VALUE_NULL:207 builder.addNull(key);208 break;209 case END_OBJECT:210 return builder.build();211 default:212 throw new JsonException("Internal Error");213 }214 }215 throw new JsonException("Internal Error");216 }217 218 159 } -
trunk/src/org/glassfish/json/JsonStringImpl.java
r6756 r13231 2 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 3 * 4 * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.4 * Copyright (c) 2013-2017 Oracle and/or its affiliates. All rights reserved. 5 5 * 6 6 * The contents of this file are subject to the terms of either the GNU … … 9 9 * may not use this file except in compliance with the License. You can 10 10 * obtain a copy of the License at 11 * https:// glassfish.dev.java.net/public/CDDL+GPL_1_1.html12 * or packager/legal/LICENSE.txt. See the License for the specific11 * https://oss.oracle.com/licenses/CDDL+GPL-1.1 12 * or LICENSE.txt. See the License for the specific 13 13 * language governing permissions and limitations under the License. 14 14 * 15 15 * When distributing the software, include this License Header Notice in each 16 * file and include the License file at packager/legal/LICENSE.txt.16 * file and include the License file at LICENSE.txt. 17 17 * 18 18 * GPL Classpath Exception: … … 78 78 @Override 79 79 public boolean equals(Object obj) { 80 if (this == obj){ 81 return true; 82 } 80 83 if (!(obj instanceof JsonString)) { 81 84 return false; -
trunk/src/org/glassfish/json/JsonStructureParser.java
r6756 r13231 2 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 3 * 4 * Copyright (c) 2012-201 3Oracle and/or its affiliates. All rights reserved.4 * Copyright (c) 2012-2017 Oracle and/or its affiliates. All rights reserved. 5 5 * 6 6 * The contents of this file are subject to the terms of either the GNU … … 9 9 * may not use this file except in compliance with the License. You can 10 10 * obtain a copy of the License at 11 * https:// glassfish.dev.java.net/public/CDDL+GPL_1_1.html12 * or packager/legal/LICENSE.txt. See the License for the specific11 * https://oss.oracle.com/licenses/CDDL+GPL-1.1 12 * or LICENSE.txt. See the License for the specific 13 13 * language governing permissions and limitations under the License. 14 14 * 15 15 * When distributing the software, include this License Header Notice in each 16 * file and include the License file at packager/legal/LICENSE.txt.16 * file and include the License file at LICENSE.txt. 17 17 * 18 18 * GPL Classpath Exception: … … 45 45 import javax.json.stream.JsonParser; 46 46 import java.math.BigDecimal; 47 import java.util.*; 47 import java.util.ArrayDeque; 48 import java.util.Deque; 49 import java.util.Iterator; 50 import java.util.Map; 51 import java.util.NoSuchElementException; 48 52 49 53 /** … … 56 60 private Scope current; 57 61 private Event state; 58 private final Deque<Scope> scopeStack = new ArrayDeque< Scope>();62 private final Deque<Scope> scopeStack = new ArrayDeque<>(); 59 63 60 64 JsonStructureParser(JsonArray array) { … … 68 72 @Override 69 73 public String getString() { 70 if (state == Event.KEY_NAME) { 71 return ((ObjectScope)current).key; 72 } else if (state == Event.VALUE_STRING) { 73 return ((JsonString)current.getJsonValue()).getString(); 74 } 75 throw new IllegalStateException(JsonMessages.PARSER_GETSTRING_ERR(state)); 74 switch (state) { 75 case KEY_NAME: 76 return ((ObjectScope)current).key; 77 case VALUE_STRING: 78 return ((JsonString)current.getJsonValue()).getString(); 79 case VALUE_NUMBER: 80 return ((JsonNumber)current.getJsonValue()).toString(); 81 default: 82 throw new IllegalStateException(JsonMessages.PARSER_GETSTRING_ERR(state)); 83 } 76 84 } 77 85 … … 168 176 public void close() { 169 177 // no-op 178 } 179 180 @Override 181 public void skipObject() { 182 if (current instanceof ObjectScope) { 183 int depth = 1; 184 do { 185 if (state == Event.KEY_NAME) { 186 state = getState(current.getJsonValue()); 187 switch (state) { 188 case START_OBJECT: 189 depth++; 190 break; 191 case END_OBJECT: 192 depth--; 193 break; 194 default: 195 //no-op 196 } 197 } else { 198 if (current.hasNext()) { 199 current.next(); 200 state = Event.KEY_NAME; 201 } else { 202 state = Event.END_OBJECT; 203 depth--; 204 } 205 } 206 } while (state != Event.END_OBJECT && depth > 0); 207 } 208 } 209 210 @Override 211 public void skipArray() { 212 if (current instanceof ArrayScope) { 213 int depth = 1; 214 do { 215 if (current.hasNext()) { 216 current.next(); 217 state = getState(current.getJsonValue()); 218 switch (state) { 219 case START_ARRAY: 220 depth++; 221 break; 222 case END_ARRAY: 223 depth--; 224 break; 225 default: 226 //no-op 227 } 228 } else { 229 state = Event.END_ARRAY; 230 depth--; 231 } 232 } while (!(state == Event.END_ARRAY && depth == 0)); 233 } 170 234 } 171 235 … … 187 251 return Event.VALUE_NULL; 188 252 default: 189 throw new JsonException( "Unknown value type="+value.getValueType());253 throw new JsonException(JsonMessages.PARSER_STATE_ERR(value.getValueType())); 190 254 } 191 255 } … … 200 264 return new ObjectScope((JsonObject)value); 201 265 } 202 throw new JsonException( "Cannot be called for value="+value);266 throw new JsonException(JsonMessages.PARSER_SCOPE_ERR(value)); 203 267 } 204 268 } -
trunk/src/org/glassfish/json/JsonTokenizer.java
r6756 r13231 2 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 3 * 4 * Copyright (c) 2012-201 3Oracle and/or its affiliates. All rights reserved.4 * Copyright (c) 2012-2017 Oracle and/or its affiliates. All rights reserved. 5 5 * 6 6 * The contents of this file are subject to the terms of either the GNU … … 9 9 * may not use this file except in compliance with the License. You can 10 10 * obtain a copy of the License at 11 * https:// glassfish.dev.java.net/public/CDDL+GPL_1_1.html12 * or packager/legal/LICENSE.txt. See the License for the specific11 * https://oss.oracle.com/licenses/CDDL+GPL-1.1 12 * or LICENSE.txt. See the License for the specific 13 13 * language governing permissions and limitations under the License. 14 14 * 15 15 * When distributing the software, include this License Header Notice in each 16 * file and include the License file at packager/legal/LICENSE.txt.16 * file and include the License file at LICENSE.txt. 17 17 * 18 18 * GPL Classpath Exception: … … 293 293 } 294 294 } 295 readBegin--; 296 storeEnd = readBegin; 295 if (ch != -1) { 296 // Only reset readBegin if eof has not been reached 297 readBegin--; 298 storeEnd = readBegin; 299 } 297 300 } 298 301 … … 417 420 } 418 421 422 boolean hasNextToken() { 423 reset(); 424 int ch = peek(); 425 426 // whitespace 427 while (ch == 0x20 || ch == 0x09 || ch == 0x0a || ch == 0x0d) { 428 if (ch == '\r') { 429 ++lineNo; 430 ++readBegin; 431 ch = peek(); 432 if (ch == '\n') { 433 lastLineOffset = bufferOffset+readBegin+1; 434 } else { 435 lastLineOffset = bufferOffset+readBegin; 436 continue; 437 } 438 } else if (ch == '\n') { 439 ++lineNo; 440 lastLineOffset = bufferOffset+readBegin+1; 441 } 442 ++readBegin; 443 ch = peek(); 444 } 445 return ch != -1; 446 } 447 448 private int peek() { 449 try { 450 if (readBegin == readEnd) { // need to fill the buffer 451 int len = fillBuf(); 452 if (len == -1) { 453 return -1; 454 } 455 assert len != 0; 456 readBegin = storeEnd; 457 readEnd = readBegin+len; 458 } 459 return buf[readBegin]; 460 } catch (IOException ioe) { 461 throw new JsonException(JsonMessages.TOKENIZER_IO_ERR(), ioe); 462 } 463 } 464 419 465 // Gives the location of the last char. Used for 420 466 // JsonParsingException.getLocation … … 499 545 // no need to create BigDecimal for common integer values (1-9 digits) 500 546 int storeLen = storeEnd-storeBegin; 501 if (!fracOrExp && (storeLen <= 9 || (minus && storeLen == 10))) {547 if (!fracOrExp && (storeLen <= 9 || (minus && storeLen <= 10))) { 502 548 int num = 0; 503 549 int i = minus ? 1 : 0; … … 510 556 } 511 557 } 558 559 long getLong() { 560 // no need to create BigDecimal for common integer values (1-18 digits) 561 int storeLen = storeEnd-storeBegin; 562 if (!fracOrExp && (storeLen <= 18 || (minus && storeLen <= 19))) { 563 long num = 0; 564 int i = minus ? 1 : 0; 565 for(; i < storeLen; i++) { 566 num = num * 10 + (buf[storeBegin+i] - '0'); 567 } 568 return minus ? -num : num; 569 } else { 570 return getBigDecimal().longValue(); 571 } 572 } 512 573 513 574 // returns true for common integer values (1-9 digits). … … 515 576 boolean isDefinitelyInt() { 516 577 int storeLen = storeEnd-storeBegin; 517 return !fracOrExp && (storeLen <= 9 || (minus && storeLen == 10)); 578 return !fracOrExp && (storeLen <= 9 || (minus && storeLen <= 10)); 579 } 580 581 // returns true for common long values (1-18 digits). 582 // So there are cases it will return false even though the number is long 583 boolean isDefinitelyLong() { 584 int storeLen = storeEnd-storeBegin; 585 return !fracOrExp && (storeLen <= 18 || (minus && storeLen <= 19)); 518 586 } 519 587 -
trunk/src/org/glassfish/json/JsonWriterFactoryImpl.java
r6756 r13231 2 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 3 * 4 * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.4 * Copyright (c) 2013-2017 Oracle and/or its affiliates. All rights reserved. 5 5 * 6 6 * The contents of this file are subject to the terms of either the GNU … … 9 9 * may not use this file except in compliance with the License. You can 10 10 * obtain a copy of the License at 11 * https:// glassfish.dev.java.net/public/CDDL+GPL_1_1.html12 * or packager/legal/LICENSE.txt. See the License for the specific11 * https://oss.oracle.com/licenses/CDDL+GPL-1.1 12 * or LICENSE.txt. See the License for the specific 13 13 * language governing permissions and limitations under the License. 14 14 * 15 15 * When distributing the software, include this License Header Notice in each 16 * file and include the License file at packager/legal/LICENSE.txt.16 * file and include the License file at LICENSE.txt. 17 17 * 18 18 * GPL Classpath Exception: -
trunk/src/org/glassfish/json/JsonWriterImpl.java
r6756 r13231 2 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 3 * 4 * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.4 * Copyright (c) 2013-2017 Oracle and/or its affiliates. All rights reserved. 5 5 * 6 6 * The contents of this file are subject to the terms of either the GNU … … 9 9 * may not use this file except in compliance with the License. You can 10 10 * obtain a copy of the License at 11 * https:// glassfish.dev.java.net/public/CDDL+GPL_1_1.html12 * or packager/legal/LICENSE.txt. See the License for the specific11 * https://oss.oracle.com/licenses/CDDL+GPL-1.1 12 * or LICENSE.txt. See the License for the specific 13 13 * language governing permissions and limitations under the License. 14 14 * 15 15 * When distributing the software, include this License Header Notice in each 16 * file and include the License file at packager/legal/LICENSE.txt.16 * file and include the License file at LICENSE.txt. 17 17 * 18 18 * GPL Classpath Exception: … … 49 49 import java.io.Writer; 50 50 import java.nio.charset.Charset; 51 import java.nio.charset.StandardCharsets; 51 52 import java.util.Map; 52 53 … … 57 58 */ 58 59 class JsonWriterImpl implements JsonWriter { 59 private static final Charset UTF_8 = Charset.forName("UTF-8");60 60 61 61 private final JsonGeneratorImpl generator; … … 75 75 76 76 JsonWriterImpl(OutputStream out, BufferPool bufferPool) { 77 this(out, UTF_8, false, bufferPool);77 this(out, StandardCharsets.UTF_8, false, bufferPool); 78 78 } 79 79 80 80 JsonWriterImpl(OutputStream out, boolean prettyPrinting, BufferPool bufferPool) { 81 this(out, UTF_8, prettyPrinting, bufferPool);81 this(out, StandardCharsets.UTF_8, prettyPrinting, bufferPool); 82 82 } 83 83 … … 146 146 147 147 @Override 148 public void write(JsonValue value) { 149 switch (value.getValueType()) { 150 case OBJECT: 151 writeObject((JsonObject) value); 152 return; 153 case ARRAY: 154 writeArray((JsonArray) value); 155 return; 156 default: 157 if (writeDone) { 158 throw new IllegalStateException(JsonMessages.WRITER_WRITE_ALREADY_CALLED()); 159 } 160 writeDone = true; 161 generator.write(value); 162 generator.flushBuffer(); 163 if (os != null) { 164 generator.flush(); 165 } 166 } 167 } 168 169 @Override 148 170 public void close() { 149 171 writeDone = true; -
trunk/src/org/glassfish/json/UnicodeDetectingInputStream.java
r6756 r13231 2 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 3 * 4 * Copyright (c) 2012-201 3Oracle and/or its affiliates. All rights reserved.4 * Copyright (c) 2012-2017 Oracle and/or its affiliates. All rights reserved. 5 5 * 6 6 * The contents of this file are subject to the terms of either the GNU … … 9 9 * may not use this file except in compliance with the License. You can 10 10 * obtain a copy of the License at 11 * https:// glassfish.dev.java.net/public/CDDL+GPL_1_1.html12 * or packager/legal/LICENSE.txt. See the License for the specific11 * https://oss.oracle.com/licenses/CDDL+GPL-1.1 12 * or LICENSE.txt. See the License for the specific 13 13 * language governing permissions and limitations under the License. 14 14 * 15 15 * When distributing the software, include this License Header Notice in each 16 * file and include the License file at packager/legal/LICENSE.txt.16 * file and include the License file at LICENSE.txt. 17 17 * 18 18 * GPL Classpath Exception: … … 46 46 import java.io.InputStream; 47 47 import java.nio.charset.Charset; 48 import java.nio.charset.StandardCharsets; 48 49 49 50 /** … … 54 55 */ 55 56 class UnicodeDetectingInputStream extends FilterInputStream { 56 private static final Charset UTF_8 = Charset.forName("UTF-8"); 57 private static final Charset UTF_16BE = Charset.forName("UTF-16BE"); 58 private static final Charset UTF_16LE = Charset.forName("UTF-16LE"); 57 59 58 private static final Charset UTF_32LE = Charset.forName("UTF-32LE"); 60 59 private static final Charset UTF_32BE = Charset.forName("UTF-32BE"); … … 122 121 buf[3] = (byte)b4; 123 122 } catch (IOException ioe) { 124 throw new JsonException( "I/O error while auto-detecting the encoding of stream", ioe);123 throw new JsonException(JsonMessages.PARSER_INPUT_ENC_DETECT_IOERR(), ioe); 125 124 } 126 125 } … … 129 128 fillBuf(); 130 129 if (bufLen < 2) { 131 throw new JsonException( "Cannot auto-detect encoding, not enough chars");130 throw new JsonException(JsonMessages.PARSER_INPUT_ENC_DETECT_FAILED()); 132 131 } else if (bufLen == 4) { 133 132 // Use BOM to detect encoding … … 140 139 } else if (buf[0] == FE && buf[1] == FF) { 141 140 curIndex = 2; 142 return UTF_16BE;141 return StandardCharsets.UTF_16BE; 143 142 } else if (buf[0] == FF && buf[1] == FE) { 144 143 curIndex = 2; 145 return UTF_16LE;144 return StandardCharsets.UTF_16LE; 146 145 } else if (buf[0] == EF && buf[1] == BB && buf[2] == BF) { 147 146 curIndex = 3; 148 return UTF_8;147 return StandardCharsets.UTF_8; 149 148 } 150 149 // No BOM, just use JSON RFC's encoding algo to auto-detect … … 152 151 return UTF_32BE; 153 152 } else if (buf[0] == NUL && buf[2] == NUL) { 154 return UTF_16BE;153 return StandardCharsets.UTF_16BE; 155 154 } else if (buf[1] == NUL && buf[2] == NUL && buf[3] == NUL) { 156 155 return UTF_32LE; 157 156 } else if (buf[1] == NUL && buf[3] == NUL) { 158 return UTF_16LE;157 return StandardCharsets.UTF_16LE; 159 158 } 160 159 } 161 return UTF_8;160 return StandardCharsets.UTF_8; 162 161 } 163 162 -
trunk/src/org/glassfish/json/api/BufferPool.java
r6756 r13231 2 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 3 * 4 * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.4 * Copyright (c) 2013-2017 Oracle and/or its affiliates. All rights reserved. 5 5 * 6 6 * The contents of this file are subject to the terms of either the GNU … … 9 9 * may not use this file except in compliance with the License. You can 10 10 * obtain a copy of the License at 11 * https:// glassfish.dev.java.net/public/CDDL+GPL_1_1.html12 * or packager/legal/LICENSE.txt. See the License for the specific11 * https://oss.oracle.com/licenses/CDDL+GPL-1.1 12 * or LICENSE.txt. See the License for the specific 13 13 * language governing permissions and limitations under the License. 14 14 * 15 15 * When distributing the software, include this License Header Notice in each 16 * file and include the License file at packager/legal/LICENSE.txt.16 * file and include the License file at LICENSE.txt. 17 17 * 18 18 * GPL Classpath Exception: … … 61 61 /** 62 62 * Returns an object back to the pool. 63 * 64 * @param buf object to return back to the pool 63 65 */ 64 66 void recycle(char[] buf);
Note:
See TracChangeset
for help on using the changeset viewer.