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 3 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
27 changes: 27 additions & 0 deletions gson/src/main/java/com/google/gson/JsonElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ public boolean isJsonNull() {
return this instanceof JsonNull;
}

/**
* provides check for verifying if this element represents a raw json value or not.
ZacSweers marked this conversation as resolved.
Show resolved Hide resolved
*
* @return true if this element is of type {@link JsonValue}, false otherwise.
* @since 2.8.7
*/
public boolean isJsonValue() {
return this instanceof JsonValue;
}

/**
* convenience method to get this element as a {@link JsonObject}. If the element is of some
* other type, a {@link IllegalStateException} will result. Hence it is best to use this method
Expand Down Expand Up @@ -140,6 +150,23 @@ public JsonNull getAsJsonNull() {
throw new IllegalStateException("Not a JSON Null: " + this);
}

/**
* convenience method to get this element as a {@link JsonValue}. If the element is of some
* other type, a {@link IllegalStateException} will result. Hence it is best to use this method
ZacSweers marked this conversation as resolved.
Show resolved Hide resolved
* after ensuring that this element is of the desired type by calling {@link #isJsonValue()}
* first.
*
* @return get this element as a {@link JsonValue}.
* @throws IllegalStateException if the element is of another type.
* @since 2.8.7
*/
public JsonValue getAsJsonValue() {
if (isJsonValue()) {
return (JsonValue) this;
}
throw new IllegalStateException("Not a JSON Null: " + this);
ZacSweers marked this conversation as resolved.
Show resolved Hide resolved
ZacSweers marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* convenience method to get this element as a boolean value.
*
Expand Down
51 changes: 51 additions & 0 deletions gson/src/main/java/com/google/gson/JsonValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.google.gson;

import com.google.gson.stream.JsonWriter;

/**
* A class representing a raw JSON value. This should be used with
* {@link JsonWriter#jsonValue(String)} and should not be encoded or escaped further.
ZacSweers marked this conversation as resolved.
Show resolved Hide resolved
*
* @author Zac Sweers
* @since 2.8.7
*/
public final class JsonValue extends JsonElement {

private final String jsonValue;

public JsonValue(String jsonValue) {
this.jsonValue = jsonValue;
ZacSweers marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @return the underlying raw JSON value.
*/
public String getJsonValue() {
return jsonValue;
}

/**
* Returns the same value as this is immutable.
* @since 2.8.7
*/
@Override public JsonElement deepCopy() {
return this;
}

@Override public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

JsonValue jsonValue1 = (JsonValue) o;

return jsonValue.equals(jsonValue1.jsonValue);
}

@Override public int hashCode() {
return jsonValue.hashCode();
}

@Override public String toString() {
return "JsonValue{" + "jsonValue='" + jsonValue + '\'' + '}';
ZacSweers marked this conversation as resolved.
Show resolved Hide resolved
ZacSweers marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonValue;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.io.Writer;
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(new JsonValue(value));
return this;
ZacSweers marked this conversation as resolved.
Show resolved Hide resolved
}

@Override public void flush() throws IOException {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,8 @@ public void write(JsonWriter out, Locale value) throws IOException {
}
out.endObject();

} else if (value.isJsonValue()) {
out.jsonValue(value.getAsJsonValue().getJsonValue());
} else {
throw new IllegalArgumentException("Couldn't write " + value.getClass());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ 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("A").jsonValue("1234");
writer.endObject();
assertEquals("{\"A\":1234}", writer.get().toString());
}

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