Giter VIP home page Giter VIP logo

Comments (11)

julman99 avatar julman99 commented on July 1, 2024

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.

daw3rd avatar daw3rd commented on July 1, 2024

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.

julman99 avatar julman99 commented on July 1, 2024

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.

daw3rd avatar daw3rd commented on July 1, 2024

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.

daw3rd avatar daw3rd commented on July 1, 2024

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.

julman99 avatar julman99 commented on July 1, 2024

@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.

julman99 avatar julman99 commented on July 1, 2024

@daw3rd can you try version 1.9.0-alpha1 and let me know if it fixes your issue?

Thanks

from gson-fire.

daw3rd avatar daw3rd commented on July 1, 2024

Seems to have done the trick. Thanks.

from gson-fire.

julman99 avatar julman99 commented on July 1, 2024

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.

julman99 avatar julman99 commented on July 1, 2024

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.

daw3rd avatar daw3rd commented on July 1, 2024

Great. Thanks. I've rebuilt with 1.8.3 and all is well. Thanks for the quick fix.

from gson-fire.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.