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
155
votes
2 answers

Can you avoid Gson converting "<" and ">" into unicode escape sequences?

I noticed that Gson converts the string "<" into an unicode escape sequence in JSON output. Can you avoid this somehow, or do characters like "<" and ">" always have to be escaped in JSON? Consider this example which prints {"s":"\u003c"}; I'd want…
Jonik
  • 80,077
  • 70
  • 264
  • 372
154
votes
1 answer

Gson ignoring map entries with value=null

Gson gson = new Gson(); Map map = new HashMap(); map.put("a", 1); map.put("b", null); System.out.println(gson.toJson(map)); //prints {"a":1} How do I get it to include all entries?
m2o
  • 6,475
  • 6
  • 27
  • 24
153
votes
7 answers

How to use TypeToken + generics with Gson in Kotlin

I'm unable to get a List of generic type from a custom class (Turns): val turnsType = TypeToken>() {}.type val turns = Gson().fromJson(pref.turns, turnsType) it said: cannot access '' it is 'public /*package*/' in 'TypeToken'
Juan Saravia
  • 7,661
  • 6
  • 29
  • 41
151
votes
7 answers

Using Enums while parsing JSON with GSON

This is related to a previous question that I asked here earlier JSON parsing using Gson I am trying to parse the same JSON, but now I have changed my classes a little bit. { "lower": 20, "upper": 40, "delimiter": " ", "scope":…
Sachin Kulkarni
  • 1,655
  • 3
  • 13
  • 14
148
votes
3 answers

Kotlin Data Class from Json using GSON

I have Java POJO class like this: class Topic { @SerializedName("id") long id; @SerializedName("name") String name; } and I have a Kotlin data class Like this data class Topic(val id: Long, val name: String) How to provide the…
erluxman
  • 18,155
  • 20
  • 92
  • 126
141
votes
4 answers

How to deserialize a list using GSON or another JSON library in Java?

I can serialize a List
Valter Silva
  • 16,446
  • 52
  • 137
  • 218
135
votes
5 answers

What is the basic purpose of @SerializedName annotation in Android using Gson

What is the basic purpose of @SerializedName annotation in Android using Gson? Give me some different examples. I can't understand the main purpose of using it.
Muhammad Ali
  • 1,377
  • 2
  • 9
  • 7
134
votes
4 answers

Multiple GSON @SerializedName per field?

Is there any way in Gson to map multiple JSON fields to a single Java object member variable? Let's say I have a Java class... public class MyClass { String id; String name; } I want to use this single class with two different services.…
Gus
  • 2,531
  • 4
  • 22
  • 28
134
votes
6 answers

Parsing JSON array into java.util.List with Gson

I have a JsonObject named "mapping" with the following content: { "client": "127.0.0.1", "servers": [ "8.8.8.8", "8.8.4.4", "156.154.70.1", "156.154.71.1" ] } I know I can get the array "servers"…
Abel Callejo
  • 13,779
  • 10
  • 69
  • 84
133
votes
4 answers

Convert JSON style properties names to Java CamelCase names with GSON

I'm using GSON to convert JSON data I get to a Java object. It works pretty well in all my tests. The problem is that our real objects have some properties named like is_online. GSON only maps them if they are named totally equal, it would be nice…
Janusz
  • 187,060
  • 113
  • 301
  • 369
124
votes
6 answers

Gson library in Android Studio

Can someone give me step by step guide to add the Gson library to an Android project? I tried the JSON built-in library but that seems to be a bit tedious right now. I saw a couple of examples based on Gson, and that seems really easy.
Venky
  • 1,929
  • 3
  • 21
  • 36
117
votes
13 answers

Get nested JSON object with GSON using retrofit

I'm consuming an API from my android app, and all the JSON responses are like this: { 'status': 'OK', 'reason': 'Everything was fine', 'content': { < some data here > } The problem is that all my POJOs have a status, reason…
mikelar
  • 1,173
  • 3
  • 8
  • 4
115
votes
4 answers

Using GSON to parse a JSON array

I have a JSON file like this: [ { "number": "3", "title": "hello_world", }, { "number": "2", "title": "hello_world", } ] Before when files had a root element I would use: Wrapper w =…
Eduardo
  • 6,900
  • 17
  • 77
  • 121
114
votes
9 answers

Polymorphism with gson

I have a problem deserializing a json string with Gson. I receive an array of commands. The command can be start, stop , some other type of command. Naturally I have polymorphism, and start/stop command inherit from command. How can I serialize…
Sophie
  • 1,580
  • 3
  • 16
  • 20
107
votes
11 answers

How to convert a String to JsonObject using gson library

Please advice how to convert a String to JsonObject using gson library. What I unsuccesfully do: String string = "abcde"; Gson gson = new Gson(); JsonObject json = new JsonObject(); json = gson.toJson(string); // Can't convert String to…
Eugene
  • 59,186
  • 91
  • 226
  • 333