From f1062448e100c6eddc48137f7e99b853063ec425 Mon Sep 17 00:00:00 2001 From: Marcono1234 Date: Tue, 11 Feb 2020 14:03:51 +0100 Subject: [PATCH] Throw UnsupportedOperationException when JsonWriter.jsonValue is not supported --- .../com/google/gson/internal/bind/JsonTreeWriter.java | 4 ++++ .../main/java/com/google/gson/stream/JsonWriter.java | 5 ++++- .../google/gson/internal/bind/JsonTreeWriterTest.java | 10 ++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/gson/src/main/java/com/google/gson/internal/bind/JsonTreeWriter.java b/gson/src/main/java/com/google/gson/internal/bind/JsonTreeWriter.java index 51dc1f3a34..df60cd634d 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/JsonTreeWriter.java +++ b/gson/src/main/java/com/google/gson/internal/bind/JsonTreeWriter.java @@ -149,6 +149,10 @@ private void put(JsonElement value) { return this; } + @Override public JsonWriter jsonValue(String value) throws IOException { + throw new UnsupportedOperationException(); + } + @Override public JsonWriter nullValue() throws IOException { put(JsonNull.INSTANCE); return this; diff --git a/gson/src/main/java/com/google/gson/stream/JsonWriter.java b/gson/src/main/java/com/google/gson/stream/JsonWriter.java index 84eaa0a7c4..855798b1d3 100644 --- a/gson/src/main/java/com/google/gson/stream/JsonWriter.java +++ b/gson/src/main/java/com/google/gson/stream/JsonWriter.java @@ -421,10 +421,13 @@ public JsonWriter value(String value) throws IOException { /** * Writes {@code value} directly to the writer without quoting or - * escaping. + * escaping. This might not be supported by all implementations, if + * not supported an {@code UnsupportedOperationException} is thrown. * * @param value the literal string value, or null to encode a null literal. * @return this writer. + * @throws UnsupportedOperationException if this writer does not support + * writing raw JSON values. */ public JsonWriter jsonValue(String value) throws IOException { if (value == null) { diff --git a/gson/src/test/java/com/google/gson/internal/bind/JsonTreeWriterTest.java b/gson/src/test/java/com/google/gson/internal/bind/JsonTreeWriterTest.java index cc04dbc324..3f537d3655 100644 --- a/gson/src/test/java/com/google/gson/internal/bind/JsonTreeWriterTest.java +++ b/gson/src/test/java/com/google/gson/internal/bind/JsonTreeWriterTest.java @@ -172,4 +172,14 @@ public void testStrictBoxedNansAndInfinities() throws IOException { } catch (IllegalArgumentException expected) { } } + + public void testJsonValue() throws IOException { + JsonTreeWriter writer = new JsonTreeWriter(); + writer.beginArray(); + try { + writer.jsonValue("test"); + fail(); + } catch (UnsupportedOperationException expected) { + } + } }