All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.leancloud.gson.GeneralObjectAdapter Maven / Gradle / Ivy

package cn.leancloud.gson;

import com.google.gson.*;
import com.google.gson.internal.ConstructorConstructor;
import com.google.gson.internal.Excluder;
import com.google.gson.internal.bind.JsonAdapterAnnotationTypeAdapterFactory;
import com.google.gson.internal.bind.ReflectiveTypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;

public class GeneralObjectAdapter extends TypeAdapter {
  private TypeAdapter typeAdapter = null;

  public GeneralObjectAdapter(FieldNamingPolicy policy, TypeToken typeToken) {
    ConstructorConstructor constructorConstructor = new ConstructorConstructor(new HashMap>());
    JsonAdapterAnnotationTypeAdapterFactory jsonAdapterFactory = new JsonAdapterAnnotationTypeAdapterFactory(constructorConstructor);
    Excluder excluder = Excluder.DEFAULT;
    ReflectiveTypeAdapterFactory reflectiveTypeAdapterFactory =
            new ReflectiveTypeAdapterFactory(constructorConstructor, policy, excluder, jsonAdapterFactory);
    typeAdapter = reflectiveTypeAdapterFactory.create(new GsonBuilder().create(), typeToken);
  }

  public void write(JsonWriter writer, T object) throws IOException {
    this.typeAdapter.write(writer, object);
  }

  public T read(JsonReader reader) throws IOException {
    return this.typeAdapter.read(reader);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy