talira

Google's Java library for converting objects to and from JSON

open sourceApache-2.0free

Gson is Google's Java library for converting Java objects to JSON and back with toJson() and fromJson(). It works with arbitrary objects, including pre-existing classes you don't have the source for, without annotations, and fully supports Java generics and custom representations. It's in maintenance mode, and its README recommends a library with explicit support for Kotlin, and Kotlin Serialization on Android, where Gson's reflection clashes with code shrinking.

Problems it solves

  • turn my Java objects into JSON and parse JSON back without annotating every class
  • serialize objects from a third-party library I can't change to JSON in Java
  • parse JSON into generic Java collections

Features

  • convert Java objects to JSON and back with toJson and fromJson
  • works with classes you can't annotate
  • full support for Java generics
  • custom serializers and type adapters

Limitations

  • Not recommended on Android: its reflection breaks under R8 and ProGuard shrinking and obfuscation in release builds.1
  • Kotlin-specific features such as non-null types and default constructor arguments aren't supported.1
  • In maintenance mode: bugs are fixed, but large new features are unlikely.1

Alternatives

The same job

These do what Gson does.

  1. Jackson The Java JSON library: a suite of data-processing tools for Java and the JVM
  2. FASTJSON 2 High-performance JSON library for Java, the successor to fastjson
  3. Moshi A modern JSON library for Android, Java and Kotlin
  4. JSON-java (org.json) The reference JSON implementation for Java, in the public domain
  5. json Ruby's JSON parser and generator, shipped with Ruby as a default gem
  6. Jayway JsonPath Query and update JSON documents in Java with XPath-like JsonPath expressions

Compare Gson with the top 3