Skip to content
Advertisement

Tag: gson

Object not being able to map to POJO class

I am getting a response, which I converted to Pojo class with one field of type Object. Now when I am trying to cast the Object type to another Pojo class its throwing the error : Code : Here, firsClass object when printed gives following result : Now, the error happens in the following line : I am sharing the

Gson Failing to call custom Serializer

I’ve been trying to follow the advice given here to turn off scientific notation on numeric values represented in Json. The problem I’ve got is that my custom Serializer is never called. I’ve tried different variations of the code and have eventually ended up with: If I’ve understood the API correctly then this code use the custom serialisation for all

Connect two HashMap Values

I’ve got this JSON string: String json = “{“countries”:{“2″:”China”,”3″:”Russia “,”4″:”USA”},”capitals”:{“2″:Beijing,”4″:null,”3″:Moscow}}”; I converted string to HashMap, using this: And now my output is: I would like to connect this values by numbers. I want to create two ArrayList like this: A)- [China,Russia,USA] B)- [Beijing,Moscow,null] How can i do it? Answer First, you need to cast map.get(“label”) to LinkedTreeMap<Integer, String>, then create

How to avoid Jackson escaping double quotes in Java?

I am working on a project and wanted to rewrite some code written in Gson to Jackson using ObjectMapper. So I am trying to create a JSON string using some properties as below: Expected result: Actual: So once I create a JSON String using Jackson, it turns out it is escaping double quotes for field names and values and adding

GSON – trim string during deserialization from JSON

I have a JSON string that is parsed using the GSON library into a Map like so: The problem I have is, my JSON attribute values have trailing/leading whitespaces that needs to be trimmed. Ideally, I want this to be done when the data is parsed to the Map using GSON. Is something like this possible? Answer You need to

Abstract class with Gson serialization/deserialization

I am expecting to get from a json a LinkedList<Request>. The pojos look like this: The serialization works fine, it generates the json, but it crashes at deserialization (maybe it’s the abstract class or/and that I am expecting a LinkedList and/or the lombok annotations)? I tryed writting a gson deserializer (quite a standard one found googling), but it doesn’t do

Serializing JSON with class metadata

I’ve used both Gson and Jackson and both of them offer a way to deal with polymorphism. For example, with Jackson you need to declare the class with a @JsonSubTypes and add each and every @JsonSubTypes.Type you have. Similar thing happens with Gson. My question is if there is an option to make it work like with how MongoDB serializes

change the json object value

I have a json object like above and i want to change apiInvokerPublicKey’s value i did not find a method in gson so how can i change it? EDIT: I used addProperty method from gson but it changes whole “onboardingInformation” i just want to change “apiInvokerPublicKey” Answer You can read whole JSON payload as JsonObject and overwrite existing property. After

Advertisement