Debian Codegraph @

src/org/eclipse/parsson/JsonReaderImpl.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2013, 2023 Oracle and/or its affiliates. All rights reserved.
3 * Copyright (c) 2022 Contributors to the Eclipse Foundation. All rights reserved.
4 *
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v. 2.0, which is available at
7 * http://www.eclipse.org/legal/epl-2.0.
8 *
9 * This Source Code may also be made available under the following Secondary
10 * Licenses when the conditions for such availability set forth in the
11 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
12 * version 2 with the GNU Classpath Exception, which is available at
13 * https://www.gnu.org/software/classpath/license.html.
14 *
15 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
16 */
17
18 package org.eclipse.parsson;
19
20 import java.io.InputStream;
21 import java.io.Reader;
22 import java.nio.charset.Charset;
23
24 import jakarta.json.JsonArray;
25 import jakarta.json.JsonException;
26 import jakarta.json.JsonObject;
27 import jakarta.json.JsonReader;
28 import jakarta.json.JsonStructure;
29 import jakarta.json.JsonValue;
30 import jakarta.json.stream.JsonParser;
31 import jakarta.json.stream.JsonParsingException;
32
33 /**
34 * JsonReader impl using parser and builders.
35 *
36 * @author Jitendra Kotamraju
37 */
38 class JsonReaderImpl implements JsonReader {
39
40 private final JsonParserImpl parser;
41 private boolean readDone;
42
43 JsonReaderImpl(Reader reader, JsonContext jsonContext) {
44 parser = new JsonParserImpl(reader, jsonContext);
45 }
46
47 JsonReaderImpl(InputStream in, JsonContext jsonContext) {
48 parser = new JsonParserImpl(in, jsonContext);
49 }
50
51 JsonReaderImpl(InputStream in, Charset charset, JsonContext jsonContext) {
52 parser = new JsonParserImpl(in, charset, jsonContext);
53 }
54
55 @Override
56 public JsonStructure read() {
57 if (readDone) {
58 throw new IllegalStateException(JsonMessages.READER_READ_ALREADY_CALLED());
59 }
60 readDone = true;
61 if (parser.hasNext()) {
62 try {
63 JsonParser.Event e = parser.next();
64 if (e == JsonParser.Event.START_ARRAY) {
65 return parser.getArray();
66 } else if (e == JsonParser.Event.START_OBJECT) {
67 return parser.getObject();
68 }
69 } catch (IllegalStateException ise) {
70 throw new JsonParsingException(ise.getMessage(), ise, parser.getLastCharLocation());
71 }
72 }
73 throw new JsonException(JsonMessages.INTERNAL_ERROR());
74 }
75
76 @Override
77 public JsonObject readObject() {
78 if (readDone) {
79 throw new IllegalStateException(JsonMessages.READER_READ_ALREADY_CALLED());
80 }
81 readDone = true;
82 if (parser.hasNext()) {
83 try {
84 parser.next();
85 return parser.getObject();
86 } catch (IllegalStateException ise) {
87 throw new JsonParsingException(ise.getMessage(), ise, parser.getLastCharLocation());
88 }
89 }
90 throw new JsonException(JsonMessages.INTERNAL_ERROR());
91 }
92
93 @Override
94 public JsonArray readArray() {
95 if (readDone) {
96 throw new IllegalStateException(JsonMessages.READER_READ_ALREADY_CALLED());
97 }
98 readDone = true;
99 if (parser.hasNext()) {
100 try {
101 parser.next();
102 return parser.getArray();
103 } catch (IllegalStateException ise) {
104 throw new JsonParsingException(ise.getMessage(), ise, parser.getLastCharLocation());
105 }
106 }
107 throw new JsonException(JsonMessages.INTERNAL_ERROR());
108 }
109
110 @Override
111 public JsonValue readValue() {
112 if (readDone) {
113 throw new IllegalStateException(JsonMessages.READER_READ_ALREADY_CALLED());
114 }
115 readDone = true;
116 if (parser.hasNext()) {
117 try {
118 parser.next();
119 return parser.getValue();
120 } catch (IllegalStateException ise) {
121 throw new JsonParsingException(ise.getMessage(), ise, parser.getLastCharLocation());
122 }
123 }
124 throw new JsonException(JsonMessages.INTERNAL_ERROR());
125 }
126
127 @Override
128 public void close() {
129 readDone = true;
130 parser.close();
131 }
132 }