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
82
votes
7 answers

"Unparseable date: 1302828677828" trying to deserialize with Gson a millisecond-format date received from server

After 4 hours non-stop trying to resolve the problem I have decided to ask here if someone could help me. The problem is that my Android client when tries to deserialize the data received from a server throw the "Unparseable: 1302828677828"…
Alfonso
  • 821
  • 1
  • 6
  • 3
80
votes
7 answers

AssertionError in Gson EnumTypeAdapter when using Proguard Obfuscation

My project implements a TypeAdapter in Gson during serialization/deserialization for preserving object's polymorphism state. Anyhow, the project works fine during development tests, but when it is released with proguard obfuscation and tested, it…
waqaslam
  • 67,549
  • 16
  • 165
  • 178
77
votes
3 answers

serialize and deserialize enum with Gson

How can i serialize and deserialize a simple enum like this with gson 2.2.4 ? public enum Color { RED, BLUE, YELLOW; }
user2183448
  • 783
  • 1
  • 5
  • 7
76
votes
3 answers

GSON - Get JSON value from String

I'm trying to parse the JSON String "{'test': '100.00'}" and in order to get the value: 100.00 with the GSON library. My code looks like this: String myJSONString = "{'test': '100.00'}"; JsonObject jobj = new Gson().fromJson(myJSONString,…
jan
  • 3,923
  • 9
  • 38
  • 78
75
votes
2 answers

Gson and deserializing an array of objects with arrays in it

I am trying to use Gson to deserialize a json string returned from my webservice The structure would be returned as TypeDTO[]. where TypeDTO is like int id; String name; ArrayList items[] and ItemDTO is like int id; String name; Boolean…
DavieDave
  • 1,394
  • 2
  • 18
  • 35
73
votes
5 answers

Gson optional and required fields

How should one deal with Gsonand required versus optional fields? Since all fields are optional, I can't really fail my network request based on if the response json contains some key, Gsonwill simply parse it to null. Method I am using…
Niko
  • 8,093
  • 5
  • 49
  • 85
72
votes
5 answers

Gson: Is there an easier way to serialize a map

This link from the Gson project seems to indicate that I would have to do something like the following for serializing a typed Map to JSON: public static class NumberTypeAdapter implements JsonSerializer,…
stevebot
  • 23,275
  • 29
  • 119
  • 181
72
votes
1 answer

Trouble with Gson serializing an ArrayList of POJO's

I had been planning on using simpleXML for my serialization needs, but figured I would try JSON out, to learn something new. This is the code I am using to try and serialize an ArrayList of test POJO's using Gson 1.7.1. Note: I removed the…
RogueDeus
  • 847
  • 1
  • 8
  • 10
70
votes
5 answers

Converting Object to JSON and JSON to Object in PHP, (library like Gson for Java)

I am developing a web application in PHP, I need to transfer many objects from server as JSON string, is there any library existing for PHP to convert object to JSON and JSON String to Objec, like Gson library for Java.
farhan ali
  • 2,462
  • 1
  • 21
  • 15
69
votes
4 answers

Convert Map to JSON using Jackson

How can I convert a Map to a valid JSON using Jackson? I am doing it using Google's GSON via a Spring Boot REST Post method... Here's the RESTful Web Service: import java.util.Map; import org.springframework.web.bind.annotation.RequestBody; import…
PacificNW_Lover
  • 4,746
  • 31
  • 90
  • 144
69
votes
6 answers

Deserializing Generic Types with GSON

I have some problems with implementation of Json Deserialization in my Android application (with Gson library) I've made class like this public class MyJson{ public List posts; } And Deserialization call is: public class JsonDownloader
VizGhar
  • 3,036
  • 1
  • 25
  • 38
68
votes
3 answers

how to parse JSON file with GSON

I have a very simple JSON with reviews for products, like: { "reviewerID": "A2XVJBSRI3SWDI", "asin": "0000031887", "reviewerName": "abigail", "helpful": [0, 0], "unixReviewTime": 1383523200, "reviewText": "Perfect red tutu for the…
user299791
  • 2,021
  • 3
  • 31
  • 57
66
votes
5 answers

retrofit convertor factory can not access GsonConverterFactory

I have included these dependencies to my project: compile 'com.squareup.retrofit:retrofit:2.0.0-beta2' compile 'com.squareup.retrofit:converter-gson:2.0.0-beta1' I have a class where I am going to access my api's via retrofit: public static …
AEMLoviji
  • 3,217
  • 9
  • 37
  • 61
65
votes
13 answers

Java Type Generic as Argument for GSON

In GSON to get a list of objects you do Gson gson = new Gson(); Type token = new TypeToken>(){}.getType(); return gson.fromJson(json, token); It works great, but I want to go further and have MyType parametrized so I can have a common…
Rodrigo Asensio
  • 2,760
  • 4
  • 26
  • 26
63
votes
3 answers

Best approach to parse huge (extra large) JSON file

I'm trying to parse some huge JSON file (like http://eu.battle.net/auction-data/258993a3c6b974ef3e6f22ea6f822720/auctions.json) using gson library (http://code.google.com/p/google-gson/) in JAVA. I would like to know what is the best approach to…
Dax
  • 2,185
  • 2
  • 21
  • 20