Gson maps Java objects to JSON text and parses JSON back into Java types. It works without requiring annotations on classes you cannot modify and emphasizes generic type support.
Overview
Gson gives Java applications a small API for turning in-memory objects into JSON strings and rebuilding objects from JSON. The toJson and fromJson entry points cover everyday serialization needs while leaving room for custom type adapters when default mapping is not enough.
A stated design goal is handling classes that lack source access or modification, so callers are not forced to add annotations just to participate in conversion. The library also advertises broad generic type coverage and support for deep inheritance trees and nested generic structures.
Optional adapters appear when related JDK modules are present, such as defaults for certain SQL date and time classes. Instance creation can fall back to unsafe allocation only when permitted, with documentation warning about environment limitations.
The project is in maintenance mode: existing defects are addressed, but large new features are unlikely. Documentation notes that JVM languages beyond Java may see incorrect behavior around nullability or default constructors, and Android release builds may prefer code-generating serializers because reflection conflicts with shrinking tools.
Key features
- toJson and fromJson helpers
- Works without class annotations
- Java Generics support
- Custom type adapters
- Complex object graphs
- Optional SQL type adapters
- JPMS module descriptor
- Maven Central distribution
Use cases
- REST API payload encoding
- Persisting config as JSON
- Parsing third-party JSON feeds
- Migrating legacy Java models
- Logging structured object state