com.capitalone.dashboard.testutil.GsonUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core Show documentation
Show all versions of core Show documentation
Core package shared by API layer and Microservices
package com.capitalone.dashboard.testutil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializer;
import org.bson.types.ObjectId;
import java.lang.reflect.Type;
public class GsonUtil {
private static final GsonBuilder gsonBuilder = new GsonBuilder()
.registerTypeAdapter(ObjectId.class, (JsonSerializer) (src, typeOfSrc, context) -> new JsonPrimitive(src.toHexString()))
.registerTypeAdapter(ObjectId.class, (JsonDeserializer) GsonUtil::deserialize);
private static ObjectId deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
if (json instanceof JsonObject) {
JsonObject jo = (JsonObject) json;
return new ObjectId(jo.get("timestamp").getAsInt(), jo.get("machineIdentifier").getAsInt(), jo.get("processIdentifier").getAsShort(), jo.get("counter").getAsInt());
}
return new ObjectId(json.getAsString());
}
public static Gson getGson() {
return gsonBuilder.create();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy