org.javers.core.json.AtomicTypeAdapter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javers-core Show documentation
Show all versions of javers-core Show documentation
JaVers - object auditing and diff framework for Java
package org.javers.core.json;
import com.google.gson.*;
import org.javers.core.diff.changetype.Atomic;
import org.javers.core.json.JsonTypeAdapterTemplate;
/**
* @author bartosz walacik
*/
class AtomicTypeAdapter extends JsonTypeAdapterTemplate {
private final boolean typeSafety;
public AtomicTypeAdapter(boolean typeSafety) {
this.typeSafety = typeSafety;
}
@Override
public JsonElement toJson(Atomic sourceValue, JsonSerializationContext jsonSerializationContext) {
if (sourceValue.isNull()) {
return JsonNull.INSTANCE;
}
JsonElement rawValue = jsonSerializationContext.serialize(sourceValue.unwrap());
if (sourceValue.isJsonBasicType()|| !typeSafety){
return rawValue;
}
return wrapTypeSafely(sourceValue, jsonSerializationContext);
}
private JsonElement wrapTypeSafely(Atomic sourceValue, JsonSerializationContext jsonSerializationContext){
JsonObject element = new JsonObject();
element.addProperty("typeAlias", sourceValue.unwrap().getClass().getSimpleName());
element.add("value" , jsonSerializationContext.serialize(sourceValue.unwrap()));
return element;
}
@Override
public Atomic fromJson(JsonElement json, JsonDeserializationContext jsonDeserializationContext) {
throw new IllegalStateException("not implemented");
}
@Override
public Class getValueType() {
return Atomic.class;
}
}