# Gson — Java JSON Serialization Library by Google > Gson is a Java library from Google that converts Java objects to JSON and back, handling generics, nested objects, and custom serialization with minimal configuration. ## Install Save in your project root: # Gson — Java JSON Serialization Library by Google ## Quick Use ```xml com.google.code.gson gson 2.11.0 ``` ```java Gson gson = new Gson(); String json = gson.toJson(myObject); MyClass obj = gson.fromJson(json, MyClass.class); ``` ## Introduction Gson provides a straightforward way to serialize Java objects to JSON and deserialize JSON back to Java objects. Originally built by Google for internal projects, it has become one of the most widely adopted JSON libraries in the Java ecosystem thanks to its simplicity and zero-annotation default behavior. ## What Gson Does - Serializes any Java object to a JSON string with a single method call - Deserializes JSON strings to Java objects including generics and nested types - Handles complex type hierarchies with TypeToken for generic type preservation - Supports custom serializers and deserializers for fine-grained control - Works with Java records, enums, and collections out of the box ## Architecture Overview Gson uses a TypeAdapter-based pipeline. When serializing, it inspects the object's class via reflection, maps fields to JSON properties, and writes them through a JsonWriter. Deserialization reads tokens via JsonReader and constructs objects using reflection or registered TypeAdapters. Custom adapters can override any step. The GsonBuilder provides a fluent API for configuring field naming, null handling, pretty-printing, and exclusion strategies. ## Self-Hosting & Configuration - Add the single gson dependency via Maven Central or Gradle — no transitive dependencies - Use GsonBuilder for custom configuration: date formats, field exclusion, naming policies - Register TypeAdapter or JsonSerializer/JsonDeserializer for custom type handling - Enable lenient parsing with setLenient() for non-strict JSON sources - Use @Expose and @SerializedName annotations for field-level control ## Key Features - Zero-configuration default: works out of the box for most Java classes - Supports Java generics via TypeToken without type erasure issues - Pretty-printing with setPrettyPrinting() for human-readable output - Streaming API (JsonReader/JsonWriter) for memory-efficient processing of large documents - Thread-safe Gson instances can be shared across an application ## Comparison with Similar Tools - **Jackson** — faster performance and richer annotation support; Gson is simpler for basic use cases - **Moshi** — modern design by Square with Kotlin-first support; Gson has broader legacy adoption - **JSON-B (Jakarta)** — standards-based; Gson is lighter and does not require a Jakarta EE container - **org.json** — minimal API with no object mapping; Gson provides full serialization/deserialization - **Fastjson2** — high performance from Alibaba; Gson has a more stable API and wider community trust ## FAQ **Q: Is Gson thread-safe?** A: Yes. A Gson instance is immutable after creation and can be safely shared across threads. Custom TypeAdapters must also be thread-safe. **Q: How does Gson handle null fields?** A: By default Gson omits null fields during serialization. Call serializeNulls() on GsonBuilder to include them. **Q: Can Gson serialize Java records?** A: Yes. Starting with Gson 2.10, Java records are supported natively. Earlier versions require a custom TypeAdapter. **Q: How do I handle polymorphic types with Gson?** A: Register a RuntimeTypeAdapterFactory or write a custom JsonDeserializer that inspects a type discriminator field to select the correct subclass. ## Sources - https://github.com/google/gson - https://google.github.io/gson/ --- Source: https://tokrepo.com/en/workflows/32f143d0-4409-11f1-9bc6-00163e2b0d79 Author: AI Open Source