All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nflabs.zeppelin.server.ZAdapter Maven / Gradle / Ivy

There is a newer version: 0.3.3
Show newest version
package com.nflabs.zeppelin.server;

import java.lang.reflect.Type;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.nflabs.zeppelin.zengine.Z;

public class ZAdapter implements JsonSerializer, JsonDeserializer{

	@Override
	public JsonElement serialize(Z src, Type typeOfSrc, JsonSerializationContext context) throws JsonParseException {
		JsonObject result = new JsonObject();
		result.add("type", new JsonPrimitive(src.getClass().getName()));
        result.add("properties", context.serialize(src, src.getClass()));
		return result;
	}

	@Override
	public Z deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
        JsonObject jsonObject = json.getAsJsonObject();
        String type = jsonObject.get("type").getAsString();
        JsonElement element = jsonObject.get("properties");
 
        try {
            return context.deserialize(element, Class.forName(type));
        } catch (ClassNotFoundException cnfe) {
            throw new JsonParseException("Unknown element type: " + type, cnfe);
        }
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy