Takes a reader in any state and returns the next value as a JsonElement.
(JsonReader reader)
| 41 | |
| 42 | /** Takes a reader in any state and returns the next value as a JsonElement. */ |
| 43 | public static JsonElement parse(JsonReader reader) throws JsonParseException { |
| 44 | boolean isEmpty = true; |
| 45 | try { |
| 46 | JsonToken unused = reader.peek(); |
| 47 | isEmpty = false; |
| 48 | return JsonElementTypeAdapter.ADAPTER.read(reader); |
| 49 | } catch (EOFException e) { |
| 50 | /* |
| 51 | * For compatibility with JSON 1.5 and earlier, we return a JsonNull for |
| 52 | * empty documents instead of throwing. |
| 53 | */ |
| 54 | if (isEmpty) { |
| 55 | return JsonNull.INSTANCE; |
| 56 | } |
| 57 | // The stream ended prematurely so it is likely a syntax error. |
| 58 | throw new JsonSyntaxException(e); |
| 59 | } catch (MalformedJsonException e) { |
| 60 | throw new JsonSyntaxException(e); |
| 61 | } catch (IOException e) { |
| 62 | throw new JsonIOException(e); |
| 63 | } catch (NumberFormatException e) { |
| 64 | throw new JsonSyntaxException(e); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** Writes the JSON element to the writer, recursively. */ |
| 69 | public static void write(JsonElement element, JsonWriter writer) throws IOException { |