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
622
votes
8 answers

Representing null in JSON

What is the preferred method for returning null values in JSON? Is there a different preference for primitives? For example, if my object on the server has an Integer called "myCount" with no value, the most correct JSON for that value would…
pherris
  • 17,195
  • 8
  • 42
  • 58
529
votes
15 answers

Deserialize a List object with Gson?

I want to transfer a list object via Google Gson, but I don't know how to deserialize generic types. What I tried after looking at this (BalusC's answer): MyClass mc = new Gson().fromJson(result, new List() {}.getClass()); but then I get…
jellyfish
  • 7,868
  • 11
  • 37
  • 49
452
votes
17 answers

Gson: How to exclude specific fields from Serialization without annotations

I'm trying to learn Gson and I'm struggling with field exclusion. Here are my classes public class Student { private Long id; private String firstName = "Philip"; private String middleName …
Liviu T.
  • 23,584
  • 10
  • 62
  • 58
399
votes
5 answers

Jackson Vs. Gson

After searching through some existing libraries for JSON, I have finally ended up with these two: Jackson Google GSon I am a bit partial towards GSON, but word on the net is that GSon suffers from a certain celestial performance issue (as of Sept…
Suraj Chandran
  • 24,433
  • 12
  • 63
  • 94
336
votes
11 answers

GSON throwing "Expected BEGIN_OBJECT but was BEGIN_ARRAY"?

I'm trying to parse a JSON string like this one [ { "updated_at":"2012-03-02 21:06:01", "fetched_at":"2012-03-02 21:28:37.728840", "description":null, "language":null, "title":"JOHN", …
Roger Travis
  • 8,402
  • 18
  • 67
  • 94
331
votes
10 answers

Gson: Directly convert String to JsonObject (no POJO)

Can't seem to figure this out. I'm attempting JSON tree manipulation in GSON, but I have a case where I do not know or have a POJO to convert a string into, prior to converting to JsonObject. Is there a way to go directly from a String to…
7zark7
  • 10,015
  • 5
  • 39
  • 54
319
votes
16 answers

How can I convert JSON to a HashMap using Gson?

I'm requesting data from a server which returns data in the JSON format. Casting a HashMap into JSON when making the request wasn't hard at all but the other way seems to be a little tricky. The JSON response looks like this: { "header" : { …
Mridang Agarwalla
  • 43,201
  • 71
  • 221
  • 382
297
votes
14 answers

Converting JSON data to Java object

I want to be able to access properties from a JSON string within my Java action method. The string is available by simply saying myJsonString = object.getJson(). Below is an example of what the string can look like: { 'title':…
Faiyet
  • 5,341
  • 14
  • 51
  • 66
255
votes
9 answers

Gson - convert from Json to a typed ArrayList

Using the Gson library, how do I convert a JSON string to an ArrayList of a custom class JsonLog? Basically, JsonLog is an interface implemented by different kinds of logs made by my Android app--SMS logs, call logs, data logs--and this ArrayList is…
Matthew Quiros
  • 13,385
  • 12
  • 87
  • 132
213
votes
8 answers

GSON - Date format

I'm trying to have a custom date format in Gson output, but .setDateFormat(DateFormat.FULL) doesn't seem to work and it the same with .registerTypeAdapter(Date.class, new DateSerializer()). It's like Gson doesn't care about the object "Date" and…
Stéphane Piette
  • 5,341
  • 6
  • 34
  • 50
193
votes
22 answers

"Expected BEGIN_OBJECT but was STRING at line 1 column 1"

I have this method: public static Object parseStringToObject(String json) { String Object = json; Gson gson = new Gson(); Object objects = gson.fromJson(object, Object.class); parseConfigFromObjectToString(object); return…
Crapo Wolf
  • 2,241
  • 2
  • 11
  • 20
183
votes
19 answers

Unable to create converter for my class in Android Retrofit library

Im migrating from using Volley to Retrofit, I already have gson class that I used before for converting JSONObject reponse to a object that implements gson annotations. When I'm trying to make http get request using retrofit but then my app crashes…
Earwin delos Santos
  • 2,965
  • 7
  • 20
  • 29
173
votes
11 answers

JSON parsing using Gson for Java

I would like to parse data from JSON which is of type String. I am using Google Gson. I have: jsonLine = " { "data": { "translations": [ { "translatedText": "Hello world" } ] } } "; and my class is: public class JsonParsing{ …
Martynas
  • 2,545
  • 7
  • 30
  • 36
169
votes
16 answers

Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $

What is this error ? How can I fix this? My app is running but can't load data. And this is my Error: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $ This is my fragment : public class news extends Fragment…
Erfan
  • 3,059
  • 3
  • 22
  • 49
165
votes
4 answers

Is it OK to use Gson instance as a static field in a model bean (reuse)?

Here's the model I implemented: public class LoginSession { private static final Gson gson = new Gson(); private String id; private String name; private long timestamp; public LoginSession(String id, String name) { …
philipjkim
  • 3,999
  • 7
  • 35
  • 48
1
2 3
99 100