Skip to content

Commit

Permalink
Throw UnsupportedOperationException when JsonWriter.jsonValue is not …
Browse files Browse the repository at this point in the history
…supported (#1651)
  • Loading branch information
Marcono1234 authored Aug 21, 2022
1 parent b84b221 commit 26be941
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,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;
Expand Down
5 changes: 4 additions & 1 deletion gson/src/main/java/com/google/gson/stream/JsonWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -426,10 +426,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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,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) {
}
}
}

0 comments on commit 26be941

Please sign in to comment.