1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.johnzon.core; |
20 | |
|
21 | |
import javax.json.JsonArray; |
22 | |
import javax.json.JsonArrayBuilder; |
23 | |
import javax.json.JsonObject; |
24 | |
import javax.json.JsonObjectBuilder; |
25 | |
import javax.json.JsonReader; |
26 | |
import javax.json.JsonStructure; |
27 | |
import javax.json.stream.JsonParser; |
28 | |
import javax.json.stream.JsonParsingException; |
29 | |
|
30 | |
class JsonReaderImpl implements JsonReader { |
31 | |
private final JsonParser parser; |
32 | 144 | private boolean closed = false; |
33 | |
|
34 | 144 | JsonReaderImpl(final JsonParser parser) { |
35 | 144 | this.parser = parser; |
36 | 144 | } |
37 | |
|
38 | |
@Override |
39 | |
public JsonStructure read() { |
40 | |
|
41 | 144 | checkClosed(); |
42 | |
|
43 | 144 | if (!parser.hasNext()) { |
44 | 0 | throw new IllegalStateException("Nothing to read"); |
45 | |
} |
46 | 144 | switch (parser.next()) { |
47 | |
case START_OBJECT: |
48 | 114 | final JsonObjectBuilder objectBuilder = new JsonObjectBuilderImpl(); |
49 | 114 | parseObject(objectBuilder); |
50 | 55 | if (parser.hasNext()) { |
51 | 0 | throw new JsonParsingException("Expected end of file", parser.getLocation()); |
52 | |
} |
53 | 54 | close(); |
54 | 54 | return objectBuilder.build(); |
55 | |
case START_ARRAY: |
56 | 18 | final JsonArrayBuilder arrayBuilder = new JsonArrayBuilderImpl(); |
57 | 18 | parseArray(arrayBuilder); |
58 | 11 | if (parser.hasNext()) { |
59 | 0 | throw new JsonParsingException("Expected end of file", parser.getLocation()); |
60 | |
} |
61 | 11 | close(); |
62 | 11 | return arrayBuilder.build(); |
63 | |
default: |
64 | 4 | close(); |
65 | 4 | throw new JsonParsingException("Unknown structure: " + parser.next(), parser.getLocation()); |
66 | |
} |
67 | |
|
68 | |
} |
69 | |
|
70 | |
@Override |
71 | |
public JsonObject readObject() { |
72 | 51 | return JsonObject.class.cast(read()); |
73 | |
} |
74 | |
|
75 | |
@Override |
76 | |
public JsonArray readArray() { |
77 | 6 | return JsonArray.class.cast(read()); |
78 | |
} |
79 | |
|
80 | |
@Override |
81 | |
public void close() { |
82 | |
|
83 | 125 | if (!closed) { |
84 | 69 | closed = true; |
85 | 69 | parser.close(); |
86 | |
} |
87 | |
|
88 | 125 | } |
89 | |
|
90 | |
private void parseObject(final JsonObjectBuilder builder) { |
91 | 4577 | String key = null; |
92 | 100019 | while (parser.hasNext()) { |
93 | 100019 | final JsonParser.Event next = parser.next(); |
94 | 99968 | switch (next) { |
95 | |
case KEY_NAME: |
96 | 47741 | key = parser.getString(); |
97 | 47741 | break; |
98 | |
|
99 | |
case VALUE_STRING: |
100 | 18312 | builder.add(key, new JsonStringImpl(parser.getString())); |
101 | 18312 | break; |
102 | |
|
103 | |
case START_OBJECT: |
104 | 1015 | JsonObjectBuilder subObject = null; |
105 | 1015 | parseObject(subObject = new JsonObjectBuilderImpl()); |
106 | 1012 | builder.add(key, subObject); |
107 | 1012 | break; |
108 | |
|
109 | |
case START_ARRAY: |
110 | 2247 | JsonArrayBuilder subArray = null; |
111 | 2247 | parseArray(subArray = new JsonArrayBuilderImpl()); |
112 | 2239 | builder.add(key, subArray); |
113 | 2239 | break; |
114 | |
|
115 | |
case VALUE_NUMBER: |
116 | 16432 | if (parser.isIntegralNumber()) { |
117 | 9149 | builder.add(key, new JsonLongImpl(parser.getLong())); |
118 | |
} else { |
119 | 7283 | builder.add(key, new JsonNumberImpl(parser.getBigDecimal())); |
120 | |
} |
121 | 7283 | break; |
122 | |
|
123 | |
case VALUE_NULL: |
124 | 3035 | builder.addNull(key); |
125 | 3035 | break; |
126 | |
|
127 | |
case VALUE_TRUE: |
128 | 308 | builder.add(key, true); |
129 | 308 | break; |
130 | |
|
131 | |
case VALUE_FALSE: |
132 | 6363 | builder.add(key, false); |
133 | 6363 | break; |
134 | |
|
135 | |
case END_OBJECT: |
136 | 4515 | return; |
137 | |
|
138 | |
case END_ARRAY: |
139 | 0 | throw new JsonParsingException("']', shouldn't occur", parser.getLocation()); |
140 | |
|
141 | |
default: |
142 | 0 | throw new JsonParsingException(next.name() + ", shouldn't occur", parser.getLocation()); |
143 | |
} |
144 | 95442 | } |
145 | 0 | } |
146 | |
|
147 | |
private void parseArray(final JsonArrayBuilder builder) { |
148 | 11041 | while (parser.hasNext()) { |
149 | 11041 | final JsonParser.Event next = parser.next(); |
150 | 11026 | switch (next) { |
151 | |
case VALUE_STRING: |
152 | 4274 | builder.add(new JsonStringImpl(parser.getString())); |
153 | 4274 | break; |
154 | |
|
155 | |
case VALUE_NUMBER: |
156 | 36 | if (parser.isIntegralNumber()) { |
157 | 33 | builder.add(new JsonLongImpl(parser.getLong())); |
158 | |
} else { |
159 | 3 | builder.add(new JsonNumberImpl(parser.getBigDecimal())); |
160 | |
} |
161 | 3 | break; |
162 | |
|
163 | |
case START_OBJECT: |
164 | 3448 | JsonObjectBuilder subObject = null; |
165 | 3448 | parseObject(subObject = new JsonObjectBuilderImpl()); |
166 | 3448 | builder.add(subObject); |
167 | 3448 | break; |
168 | |
|
169 | |
case START_ARRAY: |
170 | 1 | JsonArrayBuilder subArray = null; |
171 | 1 | parseArray(subArray = new JsonArrayBuilderImpl()); |
172 | 1 | builder.add(subArray); |
173 | 1 | break; |
174 | |
|
175 | |
case END_ARRAY: |
176 | 2251 | return; |
177 | |
|
178 | |
case VALUE_NULL: |
179 | 2 | builder.addNull(); |
180 | 2 | break; |
181 | |
|
182 | |
case VALUE_TRUE: |
183 | 1012 | builder.add(true); |
184 | 1012 | break; |
185 | |
|
186 | |
case VALUE_FALSE: |
187 | 2 | builder.add(false); |
188 | 2 | break; |
189 | |
|
190 | |
case KEY_NAME: |
191 | 0 | throw new JsonParsingException("array doesn't have keys", parser.getLocation()); |
192 | |
|
193 | |
case END_OBJECT: |
194 | 0 | throw new JsonParsingException("'}', shouldn't occur", parser.getLocation()); |
195 | |
|
196 | |
default: |
197 | 0 | throw new JsonParsingException(next.name() + ", shouldn't occur", parser.getLocation()); |
198 | |
} |
199 | 8775 | } |
200 | 0 | } |
201 | |
|
202 | |
private void checkClosed() { |
203 | 144 | if (closed) { |
204 | 0 | throw new IllegalStateException("read(), readObject(), readArray() or close() method was already called"); |
205 | |
} |
206 | |
|
207 | 144 | } |
208 | |
} |