com.telekom.m2m.cot.restsdk.util.ManagedObjectReferenceSerializer Maven / Gradle / Ivy
package com.telekom.m2m.cot.restsdk.util;
import com.google.gson.*;
import com.telekom.m2m.cot.restsdk.inventory.ManagedObject;
import com.telekom.m2m.cot.restsdk.inventory.ManagedObjectReference;
import java.lang.reflect.Type;
/**
* Created by Patrick Steinert on 03.09.16.
*/
public class ManagedObjectReferenceSerializer implements
JsonSerializer,
JsonDeserializer {
public ManagedObjectReference deserialize(JsonElement jsonElement, Type typeOfT, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
if (jsonElement.isJsonObject()) {
JsonElement jMO = jsonElement.getAsJsonObject().get("managedObject");
JsonElement selfString = jsonElement.getAsJsonObject().get("self");
ManagedObject mo = jsonDeserializationContext.deserialize(jMO, ManagedObject.class);
return new ManagedObjectReference(mo, selfString.getAsString());
}
// Undeviced to return null or Exception.
return null;
}
public JsonElement serialize(ManagedObjectReference src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject reducedMo = new JsonObject();
reducedMo.add("id", new JsonPrimitive(src.getManagedObject().getId()));
JsonObject jsonObject = new JsonObject();
jsonObject.add("managedObject", reducedMo);
return jsonObject;
}
}