Skip to content
Permalink
Browse files

Ensures deserializing also supports NaN

  • Loading branch information...
julman99 committed Mar 27, 2018
1 parent 252b391 commit e9f338cd5cdbe8272016e2426ec8aaceb0499da7
@@ -6,6 +6,7 @@
import com.google.gson.stream.JsonWriter;
import io.gsonfire.ClassConfig;
import io.gsonfire.TypeSelector;
import io.gsonfire.util.JsonUtils;

import java.io.IOException;
import java.util.Set;
@@ -65,9 +66,10 @@ public T read(JsonReader in) throws IOException {
if(deserialize == null) {
deserialize = superClass;
}

TypeToken typeToken = TypeToken.get(deserialize);
alreadyResolvedTypeTokensRegistry.add(typeToken);
TypeAdapter otherTypeAdapter;
TypeAdapter<T> otherTypeAdapter;
try {
if (deserialize != superClass) {
otherTypeAdapter = gson.getAdapter(typeToken);
@@ -77,7 +79,7 @@ public T read(JsonReader in) throws IOException {
} finally {
alreadyResolvedTypeTokensRegistry.remove(typeToken);
}
return (T) otherTypeAdapter.fromJsonTree(json);
return JsonUtils.fromJsonTree(otherTypeAdapter, in, json);
}
}

@@ -1,7 +1,9 @@
package io.gsonfire.util;

import com.google.gson.*;
import com.google.gson.internal.bind.JsonTreeReader;
import com.google.gson.internal.bind.JsonTreeWriter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;
@@ -44,18 +46,19 @@ public static JsonElement deepCopy(JsonElement from) {
}
}

private static JsonTreeWriter createTreeWriter(JsonWriter optionsFrom) {
public static JsonElement toJsonTree(TypeAdapter typeAdapter, JsonWriter optionsFrom, Object value) throws IOException {
JsonTreeWriter jsonTreeWriter = new JsonTreeWriter();
jsonTreeWriter.setLenient(optionsFrom.isLenient());
jsonTreeWriter.setHtmlSafe(optionsFrom.isHtmlSafe());
jsonTreeWriter.setSerializeNulls(optionsFrom.getSerializeNulls());
return jsonTreeWriter;
}

public static JsonElement toJsonTree(TypeAdapter typeAdapter, JsonWriter jsonWriter, Object value) throws IOException {
JsonTreeWriter jsonTreeWriter = createTreeWriter(jsonWriter);
typeAdapter.write(jsonTreeWriter, value);
return jsonTreeWriter.get();
}

public static <T> T fromJsonTree(TypeAdapter<T> typeAdapter, JsonReader originalReader, JsonElement element) throws IOException {
JsonTreeReader jsonTreeReader = new JsonTreeReader(element);
jsonTreeReader.setLenient(originalReader.isLenient());
return typeAdapter.read(jsonTreeReader);
}

}
@@ -9,46 +9,54 @@
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class NaNTest {

public static Gson getGson(boolean triggerBug) {
public static Gson getGson(boolean allowNan) {
GsonFireBuilder builder = new GsonFireBuilder();
if (triggerBug) {
builder.registerPostProcessor(Buggy.class, new PostProcessor<Buggy>() {
@Override
public void postDeserialize(Buggy result, JsonElement src, Gson gson) {

}

@Override
public void postSerialize(JsonElement result, Buggy src, Gson gson) {

}
});
builder.registerTypeSelector(Object.class, new TypeSelector<Object>() {
@Override
public Class<?> getClassForElement(JsonElement readElement) {
return null;
}
});
}
builder.registerPostProcessor(Buggy.class, new PostProcessor<Buggy>() {
@Override
public void postDeserialize(Buggy result, JsonElement src, Gson gson) {

}

@Override
public void postSerialize(JsonElement result, Buggy src, Gson gson) {

}
});
builder.registerTypeSelector(Object.class, new TypeSelector<Object>() {
@Override
public Class<?> getClassForElement(JsonElement readElement) {
return null;
}
});
GsonBuilder gb = builder.createGsonBuilder();
gb.serializeSpecialFloatingPointValues();
if(allowNan) {
gb.serializeSpecialFloatingPointValues();
}

Gson gson = gb.create();
return gson;
}


@Test
public void testNan() {
public void testNanAllow() {
Buggy fm = new Buggy();
Gson gson = getGson(true);
String json = gson.toJson(fm);
assertEquals("{\"a\":NaN}", json);
Buggy to = gson.fromJson(json, Buggy.class);
assertEquals(fm.a, to.a);
assertTrue(Double.compare(fm.a, to.a) == 0);
}

@Test(expected = java.lang.IllegalArgumentException.class)
public void testNanDisallowSerializing() {
Buggy fm = new Buggy();
Gson gson = getGson(false);
gson.toJson(fm);
}

public static class Buggy {

0 comments on commit e9f338c

Please sign in to comment.
You can’t perform that action at this time.