source: josm/trunk/src/org/glassfish/json/JsonReaderImpl.java@ 10584

Last change on this file since 10584 was 6756, checked in by Don-vip, 11 years ago

fix #9590 - replace org.json with GPL-compliant jsonp + remove mention of old world image removed in r1680

File size: 8.2 KB
Line 
1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
5 *
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common Development
8 * and Distribution License("CDDL") (collectively, the "License"). You
9 * may not use this file except in compliance with the License. You can
10 * 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
13 * language governing permissions and limitations under the License.
14 *
15 * When distributing the software, include this License Header Notice in each
16 * file and include the License file at packager/legal/LICENSE.txt.
17 *
18 * GPL Classpath Exception:
19 * Oracle designates this particular file as subject to the "Classpath"
20 * exception as provided by Oracle in the GPL Version 2 section of the License
21 * file that accompanied this code.
22 *
23 * Modifications:
24 * If applicable, add the following below the License Header, with the fields
25 * enclosed by brackets [] replaced by your own identifying information:
26 * "Portions Copyright [year] [name of copyright owner]"
27 *
28 * Contributor(s):
29 * If you wish your version of this file to be governed by only the CDDL or
30 * only the GPL Version 2, indicate your decision by adding "[Contributor]
31 * elects to include this software in this distribution under the [CDDL or GPL
32 * Version 2] license." If you don't indicate a single choice of license, a
33 * recipient has the option to distribute your version of this file under
34 * either the CDDL, the GPL Version 2 or to extend the choice of license to
35 * its licensees as provided above. However, if you add GPL Version 2 code
36 * and therefore, elected the GPL Version 2 license, then the option applies
37 * only if the new code is made subject to such option by the copyright
38 * holder.
39 */
40
41package org.glassfish.json;
42
43import org.glassfish.json.api.BufferPool;
44
45import javax.json.*;
46import javax.json.stream.JsonParser;
47import java.io.InputStream;
48import java.io.Reader;
49import java.math.BigDecimal;
50import java.nio.charset.Charset;
51
52/**
53 * JsonReader impl using parser and builders.
54 *
55 * @author Jitendra Kotamraju
56 */
57class JsonReaderImpl implements JsonReader {
58 private final JsonParserImpl parser;
59 private boolean readDone;
60 private final BufferPool bufferPool;
61
62 JsonReaderImpl(Reader reader, BufferPool bufferPool) {
63 parser = new JsonParserImpl(reader, bufferPool);
64 this.bufferPool = bufferPool;
65 }
66
67 JsonReaderImpl(InputStream in, BufferPool bufferPool) {
68 parser = new JsonParserImpl(in, bufferPool);
69 this.bufferPool = bufferPool;
70 }
71
72 JsonReaderImpl(InputStream in, Charset charset, BufferPool bufferPool) {
73 parser = new JsonParserImpl(in, charset, bufferPool);
74 this.bufferPool = bufferPool;
75 }
76
77 @Override
78 public JsonStructure read() {
79 if (readDone) {
80 throw new IllegalStateException(JsonMessages.READER_READ_ALREADY_CALLED());
81 }
82 readDone = true;
83 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 }
90 }
91 throw new JsonException("Internal Error");
92 }
93
94 @Override
95 public JsonObject readObject() {
96 if (readDone) {
97 throw new IllegalStateException(JsonMessages.READER_READ_ALREADY_CALLED());
98 }
99 readDone = true;
100 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());
106 }
107 }
108 throw new JsonException("Internal Error");
109 }
110
111 @Override
112 public JsonArray readArray() {
113 if (readDone) {
114 throw new IllegalStateException(JsonMessages.READER_READ_ALREADY_CALLED());
115 }
116 readDone = true;
117 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());
123 }
124 }
125 throw new JsonException("Internal Error");
126 }
127
128 @Override
129 public void close() {
130 readDone = true;
131 parser.close();
132 }
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}
Note: See TracBrowser for help on using the repository browser.