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/JsonReaderImpl.java

    r6756 r13231  
    22 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
    33 *
    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.
    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:
     
    4343import org.glassfish.json.api.BufferPool;
    4444
    45 import javax.json.*;
    46 import javax.json.stream.JsonParser;
    4745import java.io.InputStream;
    4846import java.io.Reader;
    49 import java.math.BigDecimal;
    5047import java.nio.charset.Charset;
     48import javax.json.JsonArray;
     49import javax.json.JsonException;
     50import javax.json.JsonObject;
     51import javax.json.JsonReader;
     52import javax.json.JsonStructure;
     53import javax.json.JsonValue;
     54import javax.json.stream.JsonParser;
     55import javax.json.stream.JsonParsingException;
    5156
    5257/**
     
    8287        readDone = true;
    8388        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());
    8998            }
    9099        }
    91         throw new JsonException("Internal Error");
     100        throw new JsonException(JsonMessages.INTERNAL_ERROR());
    92101    }
    93102
     
    99108        readDone = true;
    100109        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 JsonException(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());
    106115            }
    107116        }
    108         throw new JsonException("Internal Error");
     117        throw new JsonException(JsonMessages.INTERNAL_ERROR());
    109118    }
    110119
     
    116125        readDone = true;
    117126        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 JsonException(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());
    123132            }
    124133        }
    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());
    126152    }
    127153
     
    131157        parser.close();
    132158    }
    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 
    218159}
Note: See TracChangeset for help on using the changeset viewer.