Skip to content
Advertisement

Tag: gson

How to fix this unchecked assignment warning?

I got Warning:(31, 46) Unchecked assignment: ‘java.lang.Class’ to ‘java.lang.Class<? extends PACKAGE_NAME.Block>’ warning on the line blockRta.registerSubtype(c);, but I can’t figure out how to fix that without supressing it. ReflectionHelper.getClasses is a static method to get all the classes in that package name, and its return type is Class[]. Block is an interface. RuntimeTypeAdapterFactory is a class in gson extra, and

Write java class with nested Map to custom json format using Gson

What Java data structure can be used to serialize to the following Json which have a set of child objects? Example: Writing John and Paul can be done by: Map<String,Person> but i cannot figure out how to nest the Child without having the ‘children’ property. Example: I am not sure it is relevant, but Gson is being used to create

How to deal with “cannot be cast to class” error – GSON JAVA

i am trying to write my list to jtable in abstract model and then its return to me this error. In my opinion it could be cause by list format? name and amount are in wrong place. This is my full error message: Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException: class com.google.gson.internal.LinkedTreeMap cannot be cast to class Model.Medicine (com.google.gson.internal.LinkedTreeMap and Model.Medicine are

Conditionally serialise fields in Gson

I have two classes : Player and Match. Player has two fields, playerId and name while Match contains two Player objects and matchId too. When I serialise Player I do want the id and the name to appear in the JSON, however when I serialise Match I get matchId and Player’s non transient fields and their respective values even though

Parse JSON Data using POJO, Where key name is numeric

I’m trying to parse a special JSON data using Wrapper class, special means a JSON which have numeric keys like below : I know how to parse JSON data using POJO, But in this case java is not accepting the numeric as Keys. Wrapper/POJO Class I don’t want to go with JSON object based parsing. Is Anyone have any idea

Sort JsonArray by variable key using GSON

I am trying to sort a JsonArray in Java using GSON, I would like to sort everything inside that array via a variable key, meaning there is a string somewhere containing something that is the key that the object needs to be sorted by. Key Variable: varkey1 So it should go like like: Answer Well, you could just implement a

Gson dateformat to serialize/deserialize unix-timestamps

I am using Gson to serialize/deserialize my pojos and currently looking for a clean way to tell Gson to parse/output date attributes as unix-timestamps. Here’s my attempt: Comming from PHP where “U” is the dateformat used to serialize/deserialize date as unix-timestamps, when running my attempt code, I am a RuntimeException: Unknown pattern character ‘U’ I am assuming that Gson uses

Can I apply a custom deserializer to within another custom deserializer for GSON

The below is a working code that helps to convert JSON in Object accordingly. If the String is nil, it will be treated as null. There’s 2 custom deserializer i.e. MyOwnStringDeserializer and MyOwnListDeserializer. I am not happy with MyOwnListDeserializer deserializer, as essentially what it is doing is in term of the String comparison to the rule defined in MyOwnStringDeserializer. But

How can I prevent gson from converting integers to doubles

I’ve got integers in my json, and I do not want gson to convert them to doubles. The following does not work: The output is not what I want: Is there a way to have Integers instead of Doubles in my Map? Edit: not all my fields are integer. I’ve modified my example accordingly. I’ve read quite a few examples

Advertisement