Comments (11)
Thanks for reporting this. I will check during the weekend and release a new version if I am able to fix it
from gson-fire.
Hmm, the code above is not quite correct. I'm trying to reproduce with a simple example, but the above is using my other code (i.e. PolyGson class). I'll try and narrow this down.
from gson-fire.
Ok, please make sure you use the latest gson-fire version since I fixed some issues a while ago regarding lenient parsing
from gson-fire.
Yes, I'm using 1.8.2 from the maven repo. Here's is the updated code that seems to require setting preprocessors AND a type selector
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import io.gsonfire.GsonFireBuilder;
import io.gsonfire.PostProcessor;
import io.gsonfire.TypeSelector;
public class GsonFireBug {
private static class GSONAdapter implements PostProcessor<Object>, TypeSelector<Object> {
private static final String CLASSNAME = "$type";
@Override
public void postDeserialize(Object result, JsonElement src, Gson gson) { }
/**
* After serializing, we add the name of the class to the JsonElement if it represent a class.
*/
@Override
public void postSerialize(JsonElement result, Object src, Gson gson) {
// if (result.isJsonObject())
// result.getAsJsonObject().addProperty(CLASSNAME, src.getClass().getName());
}
/**
* If the element is a JsonObject, then expect the {@link #CLASSNAME} field to specify the name of the class to use.
*/
@Override
public Class<? extends Object> getClassForElement(JsonElement readElement) {
// if (readElement.isJsonObject()) {
// String className = readElement.getAsJsonObject().get(CLASSNAME).toString();
// className = className.replaceAll("\"", "");
// try {
// return Class.forName(className);
// } catch (ClassNotFoundException e) {
// throw new IllegalArgumentException("Could not load class " + className, e);
// }
// }
return null; // Else use the default.
}
}
public static Gson getGson(boolean triggerBug) {
GsonFireBuilder builder = new GsonFireBuilder();
GSONAdapter adapter = new GSONAdapter();
if (triggerBug) {
builder.registerPostProcessor(Object.class, adapter);
builder.registerTypeSelector(Object.class, adapter);
}
GsonBuilder gb = builder.createGsonBuilder();
gb.serializeSpecialFloatingPointValues();
Gson gson = gb.create();
return gson;
}
public static class Buggy {
double a = Double.NaN;
}
public static void main(String[] args) {
String json;
Buggy fm = new Buggy();
json = getGson(false).toJson(fm); // Succeeds
System.out.println(json);
json = getGson(true).toJson(fm); // Fails
System.out.println(json);
}
}
from gson-fire.
And my apologies, but here is an updated output and stack trace from the previous post
{"a":NaN}
Exception in thread "main" java.lang.IllegalArgumentException: JSON forbids NaN and infinities: NaN
at com.google.gson.internal.bind.JsonTreeWriter.value(JsonTreeWriter.java:191)
at com.google.gson.internal.bind.TypeAdapters$13.write(TypeAdapters.java:358)
at com.google.gson.internal.bind.TypeAdapters$13.write(TypeAdapters.java:347)
at com.google.gson.TypeAdapter.toJsonTree(TypeAdapter.java:234)
at io.gsonfire.gson.TypeSelectorTypeAdapterFactory$TypeSelectorTypeAdapter.write(TypeSelectorTypeAdapterFactory.java:58)
at io.gsonfire.gson.NullableTypeAdapter.write(NullableTypeAdapter.java:26)
at com.google.gson.TypeAdapter.toJsonTree(TypeAdapter.java:234)
at io.gsonfire.gson.HooksTypeAdapter.write(HooksTypeAdapter.java:40)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243)
at com.google.gson.TypeAdapter.toJsonTree(TypeAdapter.java:234)
at io.gsonfire.gson.TypeSelectorTypeAdapterFactory$TypeSelectorTypeAdapter.write(TypeSelectorTypeAdapterFactory.java:58)
at io.gsonfire.gson.NullableTypeAdapter.write(NullableTypeAdapter.java:26)
at com.google.gson.TypeAdapter.toJsonTree(TypeAdapter.java:234)
at io.gsonfire.gson.HooksTypeAdapter.write(HooksTypeAdapter.java:40)
at com.google.gson.Gson.toJson(Gson.java:669)
at com.google.gson.Gson.toJson(Gson.java:648)
at com.google.gson.Gson.toJson(Gson.java:603)
at com.google.gson.Gson.toJson(Gson.java:583)
at GsonFireBug.main(GsonFireBug.java:72)
from gson-fire.
@daw3rd I was able to find a fix for this, I will publish a beta version tonight since I was not able to fully test over the weekend
from gson-fire.
@daw3rd can you try version 1.9.0-alpha1
and let me know if it fixes your issue?
Thanks
from gson-fire.
Seems to have done the trick. Thanks.
from gson-fire.
Great, I will do more unit testing to ensure there are no regressions because of this change, and then release 1.9.0
Will update here once I do
Thanks for reporting!
from gson-fire.
FYI just released version 1.8.3
that contains this fix.
It is basically the same alpha you are running, I decided to make it a minor release since it only contains that fix
from gson-fire.
Great. Thanks. I've rebuilt with 1.8.3 and all is well. Thanks for the quick fix.
from gson-fire.
Related Issues (20)
- Reuse Exclusion Strategies HOT 11
- Add more powerful wrapping/unwrapping HOT 1
- Reuse Objects HOT 2
- Hooks Exception is not forwarded. HOT 2
- TypeAdapter does not respect `lenient` value HOT 3
- Custom deserialization without deserializer/type adapter? HOT 3
- Custom Deserialization of same field based on it's type
- Preprocessor for Array Insertion HOT 6
- RFC3339 Parser is wrong HOT 2
- Serialize complex map keys to strings and back HOT 2
- Add OSGi Support HOT 10
- Inconsistent licensing information
- Missing Releases on Github HOT 1
- Check for required fields HOT 1
- Unresolved requirement: Import-Package: com.google.gson.internal.bind HOT 2
- No release tags for versions 1.8.4 and 1.8.5?
- How to exclude a field from serialization?
- update gson from 2.8.6 to version com.google.code.gson:gson:2.8.9 HOT 1
- osgi is not more compatible with new version of gson HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gson-fire.