org.javers.core.json.JsonConverter 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.json.typeadapter.commit.CdoSnapshotAssembler;
import org.javers.core.metamodel.object.CdoSnapshot;
import java.io.Reader;
import java.lang.reflect.Type;
import static org.javers.common.validation.Validate.argumentsAreNotNull;
/**
* Javers is meant to support various persistence stores for
* any kind of client's data, hence we use JSON format to serialize/deserialize client's domain objects.
*
*
* Javers uses Gson
* library which provides neat and pretty JSON representation for well known Java types.
*
*
* But sometimes Gson's default JSON representation isn't appropriate for your domain model.
* This is often the case when dealing with Values like Date or Money.
*
*
* If so, you can easily customize Javers serialization/deserialization behaviour
* by providing plugins for each of your custom type.
* Javers accepts several kind of plugins:
*
* - {@link BasicStringTypeAdapter} -
* extend it if you need to represent Value as single String and don't want to deal with JSON API.
* For concrete class example see {@link org.javers.java8support.LocalDateTimeTypeAdapter}.
*
- {@link JsonTypeAdapter} - use it if you need full control over JSON conversion
*
- native Gson {@link TypeAdapter}
*
- native Gson {@link JsonSerializer}
*
- native Gson {@link JsonDeserializer}
*
*
* Javers provides JsonTypeAdapters for some well known Values like java.time.LocalDateTime,
* org.joda.time.LocalDateTime.
*
* @author bartosz walacik
*/
public class JsonConverter {
private final Gson gson;
private final CdoSnapshotAssembler cdoSnapshotAssembler;
JsonConverter(Gson gson) {
argumentsAreNotNull(gson);
this.gson = gson;
this.cdoSnapshotAssembler = new CdoSnapshotAssembler(this);
}
public String toJson(Object value) {
return gson.toJson(value);
}
public JsonElement toJsonElement(Object value) {
return gson.toJsonTree(value);
}
public T fromJson(String json, Class expectedType){
return gson.fromJson(json, expectedType);
}
public Object fromJson(String json, Type expectedType) {
return gson.fromJson(json, expectedType);
}
public JsonElement fromJsonToJsonElement(String json){
return gson.fromJson(json, JsonElement.class);
}
public T fromJson(JsonElement json, Class expectedType) {
return gson.fromJson(json, expectedType);
}
public T fromJson(Reader reader, Type expectedType) {
return gson.fromJson(reader, expectedType);
}
public CdoSnapshot fromSerializedSnapshot(CdoSnapshotSerialized cdoSnapshotSerialized) {
return fromJson(cdoSnapshotAssembler.assemble(cdoSnapshotSerialized), CdoSnapshot.class);
}
}