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.

9159 questions
1056
votes
34 answers

Android Studio: Add jar as library?

I'm trying to use the new Android Studio but I can't seem to get it working correctly. I'm using the Gson library to serialize/deserialize JSON-objects. But the library somehow isn't included in the build. I had created a new project with just a…
Ozzie
  • 11,365
  • 3
  • 18
  • 24
493
votes
7 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
  • 15,365
  • 6
  • 39
  • 53
465
votes
14 answers

Google Gson - deserialize list object? (generic type)

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,228
  • 11
  • 34
  • 49
431
votes
15 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,204
  • 10
  • 60
  • 58
377
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
  • 23,444
  • 11
  • 58
  • 92
315
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
  • 9,633
  • 5
  • 35
  • 53
312
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
  • 7,822
  • 16
  • 60
  • 90
302
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
  • 38,521
  • 65
  • 199
  • 353
272
votes
12 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,013
  • 13
  • 43
  • 63
227
votes
8 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…
MLQ
  • 12,329
  • 12
  • 79
  • 130
200
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,123
  • 6
  • 32
  • 49
162
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,427
  • 6
  • 26
  • 36
151
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,619
  • 6
  • 30
  • 45
147
votes
10 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,475
  • 6
  • 16
  • 27
144
votes
15 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
  • 1,741
  • 2
  • 9
  • 20
1
2 3
99 100