| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 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 |
|
| 35 |
|
| 36 |
|
| 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 |
} |