Questions tagged [gson]

Gson is Google's open-source library for serializing and deserializing Java objects to/from JSON.

Gson is Google's open-source library for serializing and deserializing Java objects to/from JSON.

The Gson project is hosted at and available for download from https://github.com/google/gson and licensed using the Apache License 2.0

Latest release: 2.8.6 released on Oct 4, 2019

Project Links

  • Gson API: Javadocs for the current Gson release
  • Gson guide: This guide contains examples on how to use Gson in your code.
  • Gson Roadmap: Details on upcoming releases

Alternatives

Alternative Java-to-JSON binding solutions with similar APIs include FastJSON, Jackson, and svenson. Yet more Java-to-JSON libraries are listed at json.org.

Performance

The latest performance benchmarks for these and other JSON serialization and deserialization solutions for Java are available at https://github.com/eishay/jvm-serializers/wiki.

9768 questions
3
votes
2 answers

JAVA,GSON :- add multiple data in json object using GSON

I am trying to add multiple data in single JSON object but its getting overwritten. I looked at some of the stackoverflow question but i couldn't find any answers .(maybe i dont know how to search in google) Gson turn an array of data objects into…
mihir6692
  • 177
  • 1
  • 4
  • 19
3
votes
4 answers

How to deserialize json string into object

{ "LocalLocationId [id=1]":{ "type":"folderlocation", "id":{ "type":"locallocationid", "id":1 }, "parentId":{ "type":"locallocationid", "id":0 }, "name":"Test", …
Kedar Javalkar
  • 343
  • 1
  • 5
  • 22
3
votes
1 answer

Gson's .toJson method replaces < with \u003c

I am trying to convert a Java object having values in one variable as a string of xml . Eg: After using String json = new Gson().toJson(testVO) It replaces "<" with \u003c. I want to avoid this as I will then display the data…
V Joe
  • 271
  • 1
  • 5
  • 23
3
votes
1 answer

Retrofit and Proguard error

I am getting the following exception when using retrofit with proguard (note, without Proguard everything works fine): java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.path.to.my.model at…
Daiwik Daarun
  • 3,804
  • 7
  • 33
  • 60
3
votes
1 answer

Can I make Gson.fromJson() use String.intern or use a StringPool for heavily used Strings

I am using Gson to deserialize a large number of JSON documents and performance analysis shows that garbage collection is straining to keep up and suspension time is very high due to that. I suspect that Gson creates a large number of strings for…
centic
  • 15,565
  • 9
  • 68
  • 125
3
votes
1 answer

In GSON, how to work with JSON attributes containing space?

Since GSON sets the object(pojo)'s fields using reflection, what if the json attributes contain spaces? Could we solve it without telling the server? Example : "node": { "Description": "Traffic Accident", "Date of Event": "02…
Zin Win Htet
  • 2,448
  • 4
  • 32
  • 54
3
votes
1 answer

Gson TypeAdapter's write method is still called even if the field is set not to be serialized using Expose

The Problem @JsonAdapter(WatusiTypeAdapter.class) @Expose(serialize = false, deserialize = true) private Watusi watusi; If a TypeAdapter is present, the Expose annotation seems to be ignored. The write method of the WatusiTypeAdapter is still…
Gregory Higley
  • 15,923
  • 9
  • 67
  • 96
3
votes
1 answer

ClassCastException Object Inheritance with Retrofit

I'm migrating to Retrofit from Asynctask + Loader for our core networking. It was going well till I encountered ClassCastExceptions where they previously didn't occur: ImagePojo imagePojo = (ImagePojo) mediaPojo; // Error: cannot cast MediaPojo to…
kip2
  • 6,473
  • 4
  • 55
  • 72
3
votes
1 answer

Access resource folder within jar

I am trying to read the contents of a resource folder once my JAR is built. The resource folder is marked as a source in the IDE settings (IntelliJ). I have tried the following methods: InputStream input =…
Colin747
  • 4,955
  • 18
  • 70
  • 118
3
votes
1 answer

Deserializing ArrayList of non-generic type using GSON

EDIT: the null pointer was due to a badly formed json. Hi, I'm trying to deserialize json data to an ArrayList of Restaurant object as follows (inpsired by what I found in…
jul
  • 36,404
  • 64
  • 191
  • 318
3
votes
4 answers

Gson fromJson returning empty JsonObject

So, I've got a String that is the result of a toJson method I've implemented on a class, and have confirmed in my test code that it is the correct Json representation of my class. My goal is to turn this String into a JsonObject and pass it to a…
3
votes
1 answer

Gson deserialize the boolean JSON only with false, regardless to input

My object consists of five fields : public class ConfigurationItem { @SerializedName("show_interest") boolean show_interest; @SerializedName("bid_with_price") boolean bid_with_price; @SerializedName("anonymous_orders") …
Rikki Tikki Tavi
  • 3,089
  • 5
  • 43
  • 81
3
votes
2 answers

Gson Converter with SimpleDateFormat

I'm running a code that uses Gson Converter with simple date format function and once in a while the date formatting is messes up either it's displaying date back in 1969-1970 depending on time zone or it takes it and displays some random…
3
votes
2 answers

Parsing json to a string in Gson

I have a json string: { "id":123, "name":"", "details":{} } I want to parse to this object: class Student { int id; String name; String details; } This is the error that I get: java.lang.RuntimeException: Unable to start activity…
Jimmy
  • 10,427
  • 18
  • 67
  • 122
3
votes
2 answers

Mask value of selected key in JSON

I have JSON request and response, I want to print the JSONs in the log, but there are some secured fields which I want to avoid to print in the log, I am trying to mask fields keys: example: before masking: …
jdev
  • 161
  • 3
  • 6
  • 14
1 2 3
99
100