Deep Dive into Gson's Type Adapter Mechanism

data class UserProfile(val userName: String, val userAge: Int)

class ExampleActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.layout_main)

        val Input = "{\"userName\":\"dev_user\",\"userAge\":25}"
        val user = Gson().fromJson(Input, UserProfile::class.java)
    }
}

Core Deserialization Logic

public <T> T fromJson(String , Class<T> targetClass) {
    Object result = fromJson(, (Type) targetClass);
    return Primitives.wrap(targetClass).cast(result);
}

public <T> T fromJson(String , Type type) {
    if ( == null) {
        return null;
    }
    StringReader stringReader = new StringReader();
    T resultInstance = (T) fromJson(stringReader, type);
    return resultInstance;
}

public <T> T fromJson(Reader reader, Type type) {
    JsonReader Reader = newJsonReader(reader);
    T parsedObject = (T) fromJson(Reader, type);
    assertFullConsumption(parsedObject, Reader);
    return parsedObject;
}

public <T> T fromJson(JsonReader reader, Type type) {
    TypeToken<T> token = (TypeToken<T>) TypeToken.get(type);
    // Acquire the appropriate adapter
    TypeAdapter<T> adapter = getAdapter(token);
    // Deserialize and populate the object
    T instance = adapter.read(reader);
    return instance;
}

Acquiring the Type Adapter

public <T> TypeAdapter<T> getAdapter(TypeToken<T> token) {
    // Caching logic omitted for brevity
    
    for (TypeAdapterFactory provider : factoryList) {
        // Attempt to create an adapter
        TypeAdapter<T> potentialAdapter = provider.create(this, token);
        if (potentialAdapter != null) {
            // Configure the wrapper/delegate
            call.setDelegate(potentialAdapter);
            typeTokenCache.put(token, potentialAdapter);
            return potentialAdapter;
        }
    }
    // ...
}

Factory Initialization Strategy

The factoryList is a collection of TypeAdapterFactory instances initialized during the Gson constructor execution. This list determines priority for deserialization.

final List<TypeAdapterFactory> factoryList = new ArrayList<>();

// Core built-in adapters
factoryList.add(TypeAdapters.JSON_ELEMENT_FACTORY);
factoryList.add(ObjectTypeAdapter.FACTORY);

// Exclusion strategy logic
factoryList.add(excluder);

// User-defined factories are inserted here to ensure they take precedence
factoryList.addAll(customFactories);

// Standard platform type adapters
factoryList.add(TypeAdapters.STRING_FACTORY);
factoryList.add(TypeAdapters.INTEGER_FACTORY);
factoryList.add(TypeAdapters.BOOLEAN_FACTORY);
factoryList.add(TypeAdapters.BYTE_FACTORY);
factoryList.add(TypeAdapters.SHORT_FACTORY);
// Number adapters configuration
TypeAdapter<Number> longInstance = longAdapter(longSerializationPolicy);
factoryList.add(TypeAdapters.newFactory(long.class, Long.class, longInstance));
factoryList.add(TypeAdapters.newFactory(double.class, Double.class,
        doubleAdapter(serializeSpecialFloatingPointValues)));
factoryList.add(TypeAdapters.newFactory(float.class, Float.class,
        floatAdapter(serializeSpecialFloatingPointValues)));
factoryList.add(TypeAdapters.NUMBER_FACTORY);
// Additional atomic and utility types omitted for conciseness

Tags: java gson JSON source code analysis

Posted on Tue, 11 Aug 2026 16:24:36 +0000 by hyster