Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw UnsupportedOperationException when JsonWriter.jsonValue is not supported #1651

Merged
merged 2 commits into from
Aug 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
}
}
}