Ignore:
Timestamp:
2017-12-23T02:40:43+01:00 (8 years ago)
Author:
Don-vip
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/glassfish/json/JsonGeneratorImpl.java

    r6756 r13231  
    22 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
    33 *
    4  * Copyright (c) 2012-2013 Oracle and/or its affiliates. All rights reserved.
     4 * Copyright (c) 2012-2017 Oracle and/or its affiliates. All rights reserved.
    55 *
    66 * The contents of this file are subject to the terms of either the GNU
     
    99 * may not use this file except in compliance with the License.  You can
    1010 * obtain a copy of the License at
    11  * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
    12  * or packager/legal/LICENSE.txt.  See the License for the specific
     11 * https://oss.oracle.com/licenses/CDDL+GPL-1.1
     12 * or LICENSE.txt.  See the License for the specific
    1313 * language governing permissions and limitations under the License.
    1414 *
    1515 * 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.
    1717 *
    1818 * GPL Classpath Exception:
     
    5050import java.math.BigInteger;
    5151import java.nio.charset.Charset;
     52import java.nio.charset.StandardCharsets;
    5253import java.util.ArrayDeque;
    5354import java.util.Deque;
     
    5859 */
    5960class JsonGeneratorImpl implements JsonGenerator {
    60     private static final Charset UTF_8 = Charset.forName("UTF-8");
    6161
    6262    private static final char[] INT_MIN_VALUE_CHARS = "-2147483648".toCharArray();
     
    101101        IN_NONE,
    102102        IN_OBJECT,
     103        IN_FIELD,
    103104        IN_ARRAY
    104105    }
     
    107108    private final Writer writer;
    108109    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<>();
    110111
    111112    // Using own buffering mechanism as JDK's BufferedWriter uses synchronized
     
    122123
    123124    JsonGeneratorImpl(OutputStream out, BufferPool bufferPool) {
    124         this(out, UTF_8, bufferPool);
     125        this(out, StandardCharsets.UTF_8, bufferPool);
    125126    }
    126127
     
    170171        writeComma();
    171172        writeEscapedString(name);
    172         writeChar(':');
     173        writeColon();
    173174        return this;
    174175    }
     
    267268    @Override
    268269    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
    273272        switch (value.getValueType()) {
    274273            case ARRAY:
     
    295294                JsonNumber number = (JsonNumber)value;
    296295                writeValue(number.toString());
     296                popFieldContext();
    297297                break;
    298298            case TRUE:
     
    382382    }
    383383
     384    @Override
    384385    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();
    389387        writeComma();
    390388        writeEscapedString(value);
    391         return this;
    392     }
    393 
    394 
     389        popFieldContext();
     390        return this;
     391    }
     392
     393
     394    @Override
    395395    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();
    400397        writeComma();
    401398        writeInt(value);
     399        popFieldContext();
    402400        return this;
    403401    }
     
    405403    @Override
    406404    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();
    411406        writeValue(String.valueOf(value));
     407        popFieldContext();
    412408        return this;
    413409    }
     
    415411    @Override
    416412    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();
    421414        if (Double.isInfinite(value) || Double.isNaN(value)) {
    422415            throw new NumberFormatException(JsonMessages.GENERATOR_DOUBLE_INFINITE_NAN());
    423416        }
    424417        writeValue(String.valueOf(value));
     418        popFieldContext();
    425419        return this;
    426420    }
     
    428422    @Override
    429423    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();
    434425        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        }
    436436    }
    437437
    438438    @Override
    439439    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();
    444441        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
    448454    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();
    453456        writeComma();
    454457        writeString(value ? "true" : "false");
    455         return this;
    456     }
    457 
     458        popFieldContext();
     459        return this;
     460    }
     461
     462    @Override
    458463    public JsonGenerator writeNull() {
    459         if (currentContext.scope != Scope.IN_ARRAY) {
    460             throw new JsonGenerationException(
    461                     JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope));
    462         }
     464        checkContextForValue();
    463465        writeComma();
    464466        writeString("null");
     467        popFieldContext();
    465468        return this;
    466469    }
     
    474477        writeComma();
    475478        writeEscapedString(name);
    476         writeChar(':');
     479        writeColon();
    477480        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;
    478494    }
    479495
     
    485501        writeChar(currentContext.scope == Scope.IN_ARRAY ? ']' : '}');
    486502        currentContext = stack.pop();
     503        popFieldContext();
    487504        return this;
    488505    }
    489506
    490507    protected void writeComma() {
    491         if (!currentContext.first) {
     508        if (!currentContext.first && currentContext.scope != Scope.IN_FIELD) {
    492509            writeChar(',');
    493510        }
    494511        currentContext.first = false;
     512    }
     513
     514    protected void writeColon() {
     515        writeChar(':');
    495516    }
    496517
     
    505526    }
    506527
     528    @Override
    507529    public void close() {
    508530        if (currentContext.scope != Scope.IN_NONE || currentContext.first) {
Note: See TracChangeset for help on using the changeset viewer.