List of usage examples for com.google.gson GsonBuilder registerTypeAdapterFactory
public GsonBuilder registerTypeAdapterFactory(TypeAdapterFactory factory)
From source file:org.sensorhub.impl.module.ModuleConfigJsonFile.java
License:Mozilla Public License
public ModuleConfigJsonFile(String moduleConfigPath) { configFile = new File(moduleConfigPath); configMap = new LinkedHashMap<String, ModuleConfig>(); // init json serializer/deserializer final GsonBuilder builder = new GsonBuilder(); builder.setPrettyPrinting();/*from www. ja va 2 s . co m*/ builder.disableHtmlEscaping(); builder.registerTypeAdapterFactory(new RuntimeTypeAdapterFactory<Object>(Object.class, OBJ_CLASS_FIELD)); gson = builder.create(); readJSON(); }
From source file:org.terasology.logic.behavior.core.BehaviorTreeBuilder.java
License:Apache License
private void initGson() { if (gson == null) { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeHierarchyAdapter(BehaviorNode.class, this); gsonBuilder.registerTypeAdapterFactory(new UriTypeAdapterFactory()); gsonBuilder.registerTypeAdapter(BehaviorTree.class, new TypeAdapter<BehaviorTree>() { @Override//from ww w .j a va 2s . com public void write(JsonWriter out, BehaviorTree value) throws IOException { if (value != null) { // TODO doublecheck URN out.value(value.getUrn().toString()); } else { out.value(""); } } @Override public BehaviorTree read(JsonReader in) throws IOException { String uri = in.nextString(); AssetManager assetManager = CoreRegistry.get(AssetManager.class); return assetManager.getAsset(new ResourceUrn(uri), BehaviorTree.class).orElse( assetManager.getAsset(new ResourceUrn("Behaviors:fallback"), BehaviorTree.class).get()); } }); gson = gsonBuilder.create(); } }
From source file:uk.ac.horizon.artcodes.ExperienceParser.java
License:Open Source License
public static Gson createGson(Context context) { if (gson == null) { GsonBuilder builder = new GsonBuilder(); if (context == null || Feature.get(context, R.bool.feature_load_old_experiences).isEnabled()) { builder.registerTypeAdapterFactory(new ExperienceTypeAdapterFactor()); }//from ww w .j a v a 2s. c o m gson = builder.create(); } return gson; }
From source file:us.blanshard.sudoku.messages.Rpc.java
License:Apache License
/** * Registers type adapters in the given builder so that {@link Request} and * {@link Response} objects can be serialized and deserialized in a symmetric * way./*w ww. j a v a2s . com*/ * * <p> * The request deserializer requires the "method" field of the request to come * before the "params" field, which requirement is not strictly conformant * with JSON. The serializer ensures that they are in that order. * * <p> * Likewise, the response's "id" field must come before the "result" field. */ public static GsonBuilder register(GsonBuilder builder, final Function<String, Type> methodToParamsType, final Function<Integer, Type> idToResultType) { builder.registerTypeAdapterFactory(new TypeAdapterFactory() { @SuppressWarnings("unchecked") @Override public <T> TypeAdapter<T> create(final Gson gson, TypeToken<T> type) { if (type.getRawType() == Request.class) { TypeAdapter<Request> typeAdapter = new RequestAdapter(gson, methodToParamsType); return (TypeAdapter<T>) typeAdapter.nullSafe(); } if (type.getRawType() == Response.class) { TypeAdapter<Response<Object>> typeAdapter = new ResponseAdapter(gson, idToResultType); return (TypeAdapter<T>) typeAdapter.nullSafe(); } return null; } }); return builder.serializeSpecialFloatingPointValues(); }
From source file:xyz.truenight.support.realm.RealmSupportGsonFactory.java
License:Apache License
/** * Returns {@link com.google.gson.Gson} instance which supports serialization of {@link io.realm.internal.RealmObjectProxy}. * * @param builder builder with preset of params * @param hook provider of {@link io.realm.Realm} which will be used to unmanage {@link io.realm.RealmObject} before serialization. */// w ww . j a v a 2 s . c o m public static Gson create(GsonBuilder builder, RealmHook hook) { return builder.registerTypeAdapterFactory(new RealmModelAdapterFactory(hook)).create(); }