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

Support jsonValue in JsonTreeWriter #1819

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
Expand Down Expand Up @@ -199,6 +200,16 @@ private void put(JsonElement value) {
return this;
}

/**
* {@inheritDoc}
*
* @since 2.8.7
*/
@Override public JsonWriter jsonValue(String value) throws IOException {
put(JsonParser.parseString(value));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As pointed out in the previous review comment, could you please check for null and then performreturn nullValue(); instead; the documentation demands this behavior, currently it would throw a NullPointerException.

return this;
}

@Override public void flush() throws IOException {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,27 @@ public void testObject() throws IOException {
assertEquals("{\"A\":1,\"B\":2}", writer.get().toString());
}

public void testJsonValue() throws IOException {
JsonTreeWriter writer = new JsonTreeWriter();
writer.beginObject();
writer.name("int")
.jsonValue("1234")
.name("double")
.jsonValue("12.34")
.name("string")
.jsonValue("\"hello\"")
.name("null")
.jsonValue("null")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please also call jsonValue(null)? E.g.:

Suggested change
.jsonValue("null")
.jsonValue("null")
.name("javaNull")
.jsonValue(null)

.name("object")
.jsonValue("{\"A\":\"B\"}")
.name("array")
.jsonValue("[\"A\",\"B\"]")
;
writer.endObject();
assertEquals("{\"int\":1234,\"double\":12.34,\"string\":\"hello\",\"null\":null,"
+ "\"object\":{\"A\":\"B\"},\"array\":[\"A\",\"B\"]}", writer.get().toString());
}

public void testNestedObject() throws IOException {
JsonTreeWriter writer = new JsonTreeWriter();
writer.beginObject();
Expand Down