Created
April 1, 2017 15:42
-
-
Save christianbauer/fa070c7fed49ffcb35e2e8c51a97a0e7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}-*/; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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