| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package org.eclipse.parsson; |
| 18 |
|
| 19 |
import jakarta.json.*; |
| 20 |
import jakarta.json.stream.JsonLocation; |
| 21 |
import jakarta.json.stream.JsonParser; |
| 22 |
import java.math.BigDecimal; |
| 23 |
import java.util.ArrayDeque; |
| 24 |
import java.util.Deque; |
| 25 |
import java.util.Iterator; |
| 26 |
import java.util.Map; |
| 27 |
import java.util.NoSuchElementException; |
| 28 |
import java.util.function.Function; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
class JsonStructureParser implements JsonParser { |
| 36 |
|
| 37 |
private Scope current; |
| 38 |
private Event state; |
| 39 |
private final Deque<Scope> scopeStack = new ArrayDeque<>(); |
| 40 |
|
| 41 |
JsonStructureParser(JsonArray array) { |
| 42 |
current = new ArrayScope(array); |
| 43 |
} |
| 44 |
|
| 45 |
JsonStructureParser(JsonObject object) { |
| 46 |
current = new ObjectScope(object); |
| 47 |
} |
| 48 |
|
| 49 |
@Override |
| 50 |
public String getString() { |
| 51 |
switch (state) { |
| 52 |
case KEY_NAME: |
| 53 |
return ((ObjectScope)current).key; |
| 54 |
case VALUE_STRING: |
| 55 |
return ((JsonString)current.getJsonValue()).getString(); |
| 56 |
case VALUE_NUMBER: |
| 57 |
return current.getJsonValue().toString(); |
| 58 |
default: |
| 59 |
throw new IllegalStateException(JsonMessages.PARSER_GETSTRING_ERR(state)); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
private <T> T getNumberValue(Function<JsonNumber, T> numberFunction, Function<Event, String> exceptionMessageFunction) { |
| 64 |
if (state == Event.VALUE_NUMBER) { |
| 65 |
return numberFunction.apply((JsonNumber)current.getJsonValue()); |
| 66 |
} |
| 67 |
throw new IllegalStateException(exceptionMessageFunction.apply(state)); |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public boolean isIntegralNumber() { |
| 72 |
return getNumberValue(JsonNumber::isIntegral, JsonMessages::PARSER_ISINTEGRALNUMBER_ERR); |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public int getInt() { |
| 77 |
return getNumberValue(JsonNumber::intValue, JsonMessages::PARSER_GETINT_ERR); |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
public long getLong() { |
| 82 |
return getNumberValue(JsonNumber::longValue, JsonMessages::PARSER_GETLONG_ERR); |
| 83 |
} |
| 84 |
|
| 85 |
@Override |
| 86 |
public BigDecimal getBigDecimal() { |
| 87 |
return getNumberValue(JsonNumber::bigDecimalValue, JsonMessages::PARSER_GETBIGDECIMAL_ERR); |
| 88 |
} |
| 89 |
|
| 90 |
@Override |
| 91 |
public JsonLocation getLocation() { |
| 92 |
return JsonLocationImpl.UNKNOWN; |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
public boolean hasNext() { |
| 97 |
return !((state == Event.END_OBJECT || state == Event.END_ARRAY) && scopeStack.isEmpty()); |
| 98 |
} |
| 99 |
|
| 100 |
@Override |
| 101 |
public Event next() { |
| 102 |
if (!hasNext()) { |
| 103 |
throw new NoSuchElementException(); |
| 104 |
} |
| 105 |
transition(); |
| 106 |
return state; |
| 107 |
} |
| 108 |
|
| 109 |
private void transition() { |
| 110 |
if (state == null) { |
| 111 |
state = current instanceof ArrayScope ? Event.START_ARRAY : Event.START_OBJECT; |
| 112 |
} else { |
| 113 |
if (state == Event.END_OBJECT || state == Event.END_ARRAY) { |
| 114 |
current = scopeStack.pop(); |
| 115 |
} |
| 116 |
if (current instanceof ArrayScope) { |
| 117 |
if (current.hasNext()) { |
| 118 |
current.next(); |
| 119 |
nextStateAndEndOfTheObjectOrArray(); |
| 120 |
} else { |
| 121 |
state = Event.END_ARRAY; |
| 122 |
} |
| 123 |
} else { |
| 124 |
|
| 125 |
if (state == Event.KEY_NAME) { |
| 126 |
nextStateAndEndOfTheObjectOrArray(); |
| 127 |
} else { |
| 128 |
if (current.hasNext()) { |
| 129 |
current.next(); |
| 130 |
state = Event.KEY_NAME; |
| 131 |
} else { |
| 132 |
state = Event.END_OBJECT; |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
private void nextStateAndEndOfTheObjectOrArray() { |
| 140 |
state = getState(current.getJsonValue()); |
| 141 |
if (state == Event.START_ARRAY || state == Event.START_OBJECT) { |
| 142 |
scopeStack.push(current); |
| 143 |
current = Scope.createScope(current.getJsonValue()); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
@Override |
| 148 |
public void close() { |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
@Override |
| 153 |
public void skipObject() { |
| 154 |
if (current instanceof ObjectScope) { |
| 155 |
int depth = 1; |
| 156 |
do { |
| 157 |
if (state == Event.KEY_NAME) { |
| 158 |
state = getState(current.getJsonValue()); |
| 159 |
switch (state) { |
| 160 |
case START_OBJECT: |
| 161 |
depth++; |
| 162 |
break; |
| 163 |
case END_OBJECT: |
| 164 |
depth--; |
| 165 |
break; |
| 166 |
default: |
| 167 |
|
| 168 |
} |
| 169 |
} else { |
| 170 |
if (current.hasNext()) { |
| 171 |
current.next(); |
| 172 |
state = Event.KEY_NAME; |
| 173 |
} else { |
| 174 |
state = Event.END_OBJECT; |
| 175 |
depth--; |
| 176 |
} |
| 177 |
} |
| 178 |
} while (state != Event.END_OBJECT && depth > 0); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
@Override |
| 183 |
public void skipArray() { |
| 184 |
if (current instanceof ArrayScope) { |
| 185 |
int depth = 1; |
| 186 |
do { |
| 187 |
if (current.hasNext()) { |
| 188 |
current.next(); |
| 189 |
state = getState(current.getJsonValue()); |
| 190 |
switch (state) { |
| 191 |
case START_ARRAY: |
| 192 |
depth++; |
| 193 |
break; |
| 194 |
case END_ARRAY: |
| 195 |
depth--; |
| 196 |
break; |
| 197 |
default: |
| 198 |
|
| 199 |
} |
| 200 |
} else { |
| 201 |
state = Event.END_ARRAY; |
| 202 |
depth--; |
| 203 |
} |
| 204 |
} while (!(state == Event.END_ARRAY && depth == 0)); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
private static Event getState(JsonValue value) { |
| 209 |
switch (value.getValueType()) { |
| 210 |
case ARRAY: |
| 211 |
return Event.START_ARRAY; |
| 212 |
case OBJECT: |
| 213 |
return Event.START_OBJECT; |
| 214 |
case STRING: |
| 215 |
return Event.VALUE_STRING; |
| 216 |
case NUMBER: |
| 217 |
return Event.VALUE_NUMBER; |
| 218 |
case TRUE: |
| 219 |
return Event.VALUE_TRUE; |
| 220 |
case FALSE: |
| 221 |
return Event.VALUE_FALSE; |
| 222 |
case NULL: |
| 223 |
return Event.VALUE_NULL; |
| 224 |
default: |
| 225 |
throw new JsonException(JsonMessages.PARSER_STATE_ERR(value.getValueType())); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
private static abstract class Scope implements Iterator { |
| 230 |
abstract JsonValue getJsonValue(); |
| 231 |
|
| 232 |
static Scope createScope(JsonValue value) { |
| 233 |
if (value instanceof JsonArray) { |
| 234 |
return new ArrayScope((JsonArray)value); |
| 235 |
} else if (value instanceof JsonObject) { |
| 236 |
return new ObjectScope((JsonObject)value); |
| 237 |
} |
| 238 |
throw new JsonException(JsonMessages.PARSER_SCOPE_ERR(value)); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
private static class ArrayScope extends Scope { |
| 243 |
private final Iterator<JsonValue> it; |
| 244 |
private JsonValue value; |
| 245 |
|
| 246 |
ArrayScope(JsonArray array) { |
| 247 |
this.it = array.iterator(); |
| 248 |
} |
| 249 |
|
| 250 |
@Override |
| 251 |
public boolean hasNext() { |
| 252 |
return it.hasNext(); |
| 253 |
} |
| 254 |
|
| 255 |
@Override |
| 256 |
public JsonValue next() { |
| 257 |
value = it.next(); |
| 258 |
return value; |
| 259 |
} |
| 260 |
|
| 261 |
@Override |
| 262 |
public void remove() { |
| 263 |
throw new UnsupportedOperationException(); |
| 264 |
} |
| 265 |
|
| 266 |
@Override |
| 267 |
JsonValue getJsonValue() { |
| 268 |
return value; |
| 269 |
} |
| 270 |
|
| 271 |
} |
| 272 |
|
| 273 |
private static class ObjectScope extends Scope { |
| 274 |
private final Iterator<Map.Entry<String, JsonValue>> it; |
| 275 |
private JsonValue value; |
| 276 |
private String key; |
| 277 |
|
| 278 |
ObjectScope(JsonObject object) { |
| 279 |
this.it = object.entrySet().iterator(); |
| 280 |
} |
| 281 |
|
| 282 |
@Override |
| 283 |
public boolean hasNext() { |
| 284 |
return it.hasNext(); |
| 285 |
} |
| 286 |
|
| 287 |
@Override |
| 288 |
public Map.Entry<String, JsonValue> next() { |
| 289 |
Map.Entry<String, JsonValue> next = it.next(); |
| 290 |
this.key = next.getKey(); |
| 291 |
this.value = next.getValue(); |
| 292 |
return next; |
| 293 |
} |
| 294 |
|
| 295 |
@Override |
| 296 |
public void remove() { |
| 297 |
throw new UnsupportedOperationException(); |
| 298 |
} |
| 299 |
|
| 300 |
@Override |
| 301 |
JsonValue getJsonValue() { |
| 302 |
return value; |
| 303 |
} |
| 304 |
|
| 305 |
} |
| 306 |
|
| 307 |
} |