Changeset 13231 in josm for trunk/src/org/glassfish/json/JsonGeneratorImpl.java
- Timestamp:
- 2017-12-23T02:40:43+01:00 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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) {
Note:
See TracChangeset
for help on using the changeset viewer.
