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
103
votes
15 answers

gson.toJson() throws StackOverflowError

I would like to generate a JSON String from my object: Gson gson = new Gson(); String json = gson.toJson(item); Everytime I try to do this, I get this error: 14:46:40,236 ERROR [[BomItemToJSON]] Servlet.service() for servlet BomItemToJSON threw…
nimrod
  • 5,595
  • 29
  • 85
  • 149
100
votes
4 answers

Gson: @Expose vs @SerializedName

With respect to Gson what is the difference between @Expose and @SerializedName("stringValue")?
Ahmed Faisal
  • 4,397
  • 12
  • 45
  • 74
99
votes
11 answers

How to parse JSON Array (Not Json Object) in Android

I have a trouble finding a way how to parse JSONArray. It looks like this: [{"name":"name1","url":"url1"},{"name":"name2","url":"url2"},...] I know how to parse it if the JSON was written differently (In other words, if I had json object returned…
SteBra
  • 4,188
  • 6
  • 37
  • 68
98
votes
3 answers

Gson custom seralizer for one variable (of many) in an object using TypeAdapter

I've seen plenty of simple examples of using a custom TypeAdapter. The most helpful has been Class TypeAdapter. But that hasn't answered my question yet. I want to customize the serialization of a single field in the object and let the default…
MountainX
  • 6,217
  • 8
  • 52
  • 83
96
votes
10 answers

How to prevent Gson from expressing integers as floats

Gson has some odd behavior when I try to convert a string to json. The code below transforms string draft into json responses. Is there a way to prevent gson from adding the '.0 to all integer values? ArrayList>…
Mark Murphy
  • 1,580
  • 1
  • 16
  • 29
95
votes
9 answers

How to Parse JSON Array with Gson

I want to parse JSON arrays and using gson. Firstly, I can log JSON output, server is responsing to client clearly. Here is my JSON output: [ { id : '1', title: 'sample title', .... }, { …
Ogulcan Orhan
  • 5,170
  • 6
  • 33
  • 49
95
votes
13 answers

Gson TypeToken with dynamic ArrayList item type

I have this code: Type typeOfObjectsList = new TypeToken>() {}.getType(); List objectsList = new Gson().fromJson(json, typeOfObjectsList); It converts a JSON string to a List of objects. But now I want to have this…
Amin Sh
  • 2,684
  • 2
  • 27
  • 42
94
votes
10 answers

com.google.gson.internal.LinkedTreeMap cannot be cast to my class

I have some problems with getting my object from a JSON string. I got the class Product public class Product { private String mBarcode; private String mName; private String mPrice; public Product(String barcode, String name, String…
Tanzmaus
  • 1,071
  • 1
  • 7
  • 7
93
votes
7 answers

Retrofit GSON serialize Date from json string into java.util.date

I am using the Retrofit library for my REST calls. Most of what I have done has been smooth as butter but for some reason I am having issues converting JSON timestamp strings into java.util.Date objects. The JSON that is coming in looks like this.…
jpotts18
  • 4,951
  • 5
  • 31
  • 31
92
votes
3 answers

Moshi vs Gson in android

I'm deciding on whether to use Moshi by square or Gson to serialize and deserialize model data. one thing i always did not like about Gson is i think it uses reflection which can be slow on android? Does Moshi use reflection also? What are some…
j2emanue
  • 60,549
  • 65
  • 286
  • 456
89
votes
12 answers

How to handle Dynamic JSON in Retrofit?

I am using the retrofit efficient networking library, but I am unable to handle Dynamic JSON which contains single prefix responseMessage which changes to object randomly, the same prefix ( responseMessage) changes to String in some cases…
LOG_TAG
  • 19,894
  • 12
  • 72
  • 105
88
votes
7 answers

Retrofit Expected BEGIN_OBJECT but was BEGIN_ARRAY

I'm fairly new to JSON parsing, I'm using the Retrofit library of Square and ran into this problem. I'm trying to parse this JSON reponse: [ { "id": 3, "username": "jezer", "regid": "oiqwueoiwqueoiwqueoiwq", …
Jezer Crespo
  • 2,152
  • 3
  • 24
  • 27
88
votes
11 answers

class A declares multiple JSON fields

i have a class A which has some private fields and the same class extends another class B which also has some private fields which are in class A. public class A extends B { private BigDecimal netAmountTcy; private BigDecimal netAmountPcy; …
Java Questions
  • 7,813
  • 41
  • 118
  • 176
83
votes
4 answers

Why does GSON use fields and not getters/setters?

Why does GSON use ONLY fields(private,public,protected)? Is there a way to tell GSON to use only getters and setters?
Zemzela
  • 3,060
  • 6
  • 23
  • 32
83
votes
3 answers

gson throws MalformedJsonException

I'm using gson to convert a json string to a Java-Object. The value of result2 is exactly the same as the value of result1. (Copied from debugger; Backslashs added) The following exception is thrown while converting…
Non
  • 1,936
  • 2
  • 17
  • 24