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