gson

Gson desearilize list and class, how does erasure work here?

筅森魡賤 提交于 2021-02-11 00:19:50
问题 I intend to write a generic method to convert a json list into it's specific list with class. This is the generic json parser: public class JsonParserUtils { private static Gson gson = new Gson(); public static <T> String toJson(T object) { if (object == null) { return null; } return gson.toJson(object); } public static <T> T fromJson(String json, Class<T> className) { if (StringUtils.isEmpty(json)) { return null; } T object = gson.fromJson(json, className); return object; } public static <T>

java.lang.NoClassDefFoundError: com.google.gson.Gson

拜拜、爱过 提交于 2021-02-10 12:28:47
问题 I am new on Android Development environment so need help from the experts. java.lang.NoClassDefFoundError: com.google.gson.Gson Gson library included in project but when calling Gson from a method inside the project getting the error like ths 回答1: Here are a couple of reasons that this could be happening: a) The library jar file is not being included b) The wrong version of the jar file is being included c) (if you are using maven) Another jar file is pulling in a newer/different version of

Stackoverflow Exception in Gson

半城伤御伤魂 提交于 2021-02-09 02:59:26
问题 I am trying to parse Json string into Java object using Gson library but i encountered StackoverflowException. java.lang.StackOverflowError com.google.gson.internal.$Gson$Types.checkNotPrimitive($Gson$Types.java:431) com.google.gson.internal.$Gson$Types.access$000($Gson$Types.java:42) com.google.gson.internal.$Gson$Types$WildcardTypeImpl.($Gson$Types.java:540) com.google.gson.internal.$Gson$Types.canonicalize($Gson$Types.java:108) com.google.gson.internal.$Gson$Types$WildcardTypeImpl.($Gson

Stackoverflow Exception in Gson

不羁的心 提交于 2021-02-09 02:58:59
问题 I am trying to parse Json string into Java object using Gson library but i encountered StackoverflowException. java.lang.StackOverflowError com.google.gson.internal.$Gson$Types.checkNotPrimitive($Gson$Types.java:431) com.google.gson.internal.$Gson$Types.access$000($Gson$Types.java:42) com.google.gson.internal.$Gson$Types$WildcardTypeImpl.($Gson$Types.java:540) com.google.gson.internal.$Gson$Types.canonicalize($Gson$Types.java:108) com.google.gson.internal.$Gson$Types$WildcardTypeImpl.($Gson

Gson custom serialization

前提是你 提交于 2021-02-08 16:53:48
问题 I wish to have a custom GSON deserializer such that whenever it is deserializing a JSON object (i.e. anything within curly brackets { ... } ), it will look for a $type node and deserialize using its inbuilt deserializing capability to that type. If no $type object is found, it just does what it normal does. So for example, I would want this to work: { "$type": "my.package.CustomMessage" "payload" : { "$type": "my.package.PayloadMessage", "key": "value" } } public class CustomMessage { public

How to add an object as a property of a JsonObject object?

对着背影说爱祢 提交于 2021-02-08 12:19:43
问题 Here is what I am doing: JsonObject jobj = new JsonObject(); jobj.addProperty("name", "great car"); I am hoping to add another property whose value is an object as follows: jobj.addProperty("car", A_CAR_OBJECT); But it appears that GSON does not allow me to do jobj.addProperty("car", A_CAR_OBJECT) . I am going to eventually do the following: String json = new Gson().toJson(jobj); to get the Json string. Is this doable? Am I using GSON the right way? Or should I just use a HashMap to throw all

How to add an object as a property of a JsonObject object?

不问归期 提交于 2021-02-08 12:19:35
问题 Here is what I am doing: JsonObject jobj = new JsonObject(); jobj.addProperty("name", "great car"); I am hoping to add another property whose value is an object as follows: jobj.addProperty("car", A_CAR_OBJECT); But it appears that GSON does not allow me to do jobj.addProperty("car", A_CAR_OBJECT) . I am going to eventually do the following: String json = new Gson().toJson(jobj); to get the Json string. Is this doable? Am I using GSON the right way? Or should I just use a HashMap to throw all

Scala Gson, cannot serialize Option[Blob]

我与影子孤独终老i 提交于 2021-02-08 11:27:29
问题 I managed to extend Gson to handle Scala's Option and Blob types, but I have trouble with the combined Option[java.sql.Blob] . My test says [info] - should serialize an Option - OK [info] - should serialize a Blob - OK [info] - should serialize an Option[Blob] * FAILED * [info] {"x":{"buf":[97,115,100,102],"len":4,"origLen":4}} was not equal to {"x":"asdf"} Can you please tell me what this {"buf": [...]} object is, and possibly why Gson is doing that? My serializer (below) definitely tells

Gson with Scala causes StackOverflow for Enumerations

青春壹個敷衍的年華 提交于 2021-02-08 11:14:11
问题 I have an enum defined in Scala class as follows // define compression types as enumerator object CompressionType extends Enumeration { type CompressionType = Value val None, Gzip, Snappy, Lz4, Zstd = Value } and I have class that I want to Serialize in JSON case class ProducerConfig(batchNumMessages : Int, lingerMs : Int, messageSize : Int, topic: String, compressionType: CompressionType.Value ) That class includes the Enum object. It seems that using GSON to serialize causes StackOverflow

Gson with Scala causes StackOverflow for Enumerations

我怕爱的太早我们不能终老 提交于 2021-02-08 11:12:41
问题 I have an enum defined in Scala class as follows // define compression types as enumerator object CompressionType extends Enumeration { type CompressionType = Value val None, Gzip, Snappy, Lz4, Zstd = Value } and I have class that I want to Serialize in JSON case class ProducerConfig(batchNumMessages : Int, lingerMs : Int, messageSize : Int, topic: String, compressionType: CompressionType.Value ) That class includes the Enum object. It seems that using GSON to serialize causes StackOverflow