| 1 | package net.minecraft.util;
|
| 2 |
|
| 3 | import com.google.gson.JsonElement;
|
| 4 | import com.google.gson.JsonIOException;
|
| 5 | import com.google.gson.JsonParser;
|
| 6 | import com.google.gson.JsonSyntaxException;
|
| 7 | import com.google.gson.Strictness;
|
| 8 | import com.google.gson.stream.JsonReader;
|
| 9 | import com.google.gson.stream.JsonToken;
|
| 10 | import com.google.gson.stream.MalformedJsonException;
|
| 11 | import java.io.IOException;
|
| 12 | import java.io.Reader;
|
| 13 | import java.io.StringReader;
|
| 14 |
|
| 15 | public class StrictJsonParser {
|
| 16 | public static JsonElement parse(final Reader reader) throws JsonIOException, JsonSyntaxException {
|
| 17 | try {
|
| 18 | JsonReader jsonReader = new JsonReader(reader);
|
| 19 | jsonReader.setStrictness(Strictness.STRICT);
|
| 20 | JsonElement element = JsonParser.parseReader(jsonReader);
|
| 21 | if (!element.isJsonNull() && jsonReader.peek() != JsonToken.END_DOCUMENT) {
|
| 22 | throw new JsonSyntaxException("Did not consume the entire document.");
|
| 23 | } else {
|
| 24 | return element;
|
| 25 | }
|
| 26 | } catch (MalformedJsonException | NumberFormatException e) {
|
| 27 | throw new JsonSyntaxException(e);
|
| 28 | } catch (IOException e) {
|
| 29 | throw new JsonIOException(e);
|
| 30 | }
|
| 31 | }
|
| 32 |
|
| 33 | public static JsonElement parse(final String json) throws JsonSyntaxException {
|
| 34 | return parse(new StringReader(json));
|
| 35 | }
|
| 36 | }
|
| 37 |
|