Skip to content

Instantly share code, notes, and snippets.

@christianbauer
Created April 1, 2017 15:42
Show Gist options
  • Save christianbauer/fa070c7fed49ffcb35e2e8c51a97a0e7 to your computer and use it in GitHub Desktop.
Save christianbauer/fa070c7fed49ffcb35e2e8c51a97a0e7 to your computer and use it in GitHub Desktop.
public class ElementalJsonDeserializer extends JsonDeserializer<JsonValue> {
@Override
protected JsonValue doDeserialize(JsonReader reader, JsonDeserializationContext ctx, JsonDeserializerParameters params) {
if (reader.hasNext()) {
String nextValue = reader.nextValue();
JsonValue value = Json.instance().parse(nextValue);
value = makeCastableToJavaScriptObject(value);
return value;
}
return null;
}
static native JsonValue makeCastableToJavaScriptObject(JsonValue value) /*-{
return Object(value);
}-*/;
}
public class ElementalJsonSerializer extends JsonSerializer<Object> {
@Override
protected void doSerialize(JsonWriter writer, Object value, JsonSerializationContext ctx, JsonSerializerParameters params) {
if (value instanceof JsonValue) {
JsonValue jsonValue = (JsonValue) value;
writer.rawValue(jsonValue.toJson());
} else {
// If elemental gives us a JS primitive, use JS serialization
writer.rawValue(stringify(value));
}
}
final public native String stringify(Object value) /*-{
return $wnd.JSON.stringify(value);
}-*/;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment