Example usage for com.google.gson JsonDeserializationContext deserialize

List of usage examples for com.google.gson JsonDeserializationContext deserialize

Introduction

In this page you can find the example usage for com.google.gson JsonDeserializationContext deserialize.

Prototype

public <T> T deserialize(JsonElement json, Type typeOfT) throws JsonParseException;

Source Link

Document

Invokes default deserialization on the specified object.

Usage

From source file:co.cask.cdap.proto.codec.AbstractSpecificationCodec.java

License:Apache License

protected final <V> List<V> deserializeList(JsonElement json, JsonDeserializationContext context,
        Class<V> valueType) {
    Type type = new TypeToken<List<V>>() {
    }.where(new TypeParameter<V>() {
    }, valueType).getType();/*from  w  w  w. j  ava  2  s . c  o  m*/
    List<V> list = context.deserialize(json, type);
    return list == null ? Collections.<V>emptyList() : list;
}

From source file:co.cask.cdap.proto.codec.AuditMessageTypeAdapter.java

License:Apache License

@Override
public AuditMessage deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObj = json.getAsJsonObject();
    long timeMillis = jsonObj.get("time").getAsLong();
    EntityId entityId = context.deserialize(jsonObj.getAsJsonObject("entityId"), EntityId.class);
    String user = jsonObj.get("user").getAsString();
    AuditType auditType = context.deserialize(jsonObj.getAsJsonPrimitive("type"), AuditType.class);

    AuditPayload payload;// w  w w.  j av a 2s . c  o m
    JsonObject jsonPayload = jsonObj.getAsJsonObject("payload");
    switch (auditType) {
    case METADATA_CHANGE:
        payload = context.deserialize(jsonPayload, MetadataPayload.class);
        break;
    case ACCESS:
        payload = context.deserialize(jsonPayload, AccessPayload.class);
        break;
    default:
        payload = AuditPayload.EMPTY_PAYLOAD;
    }
    return new AuditMessage(timeMillis, entityId, user, auditType, payload);
}

From source file:co.cask.cdap.proto.codec.BasicThrowableCodec.java

License:Apache License

@Override
public BasicThrowable deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObj = json.getAsJsonObject();
    String className = jsonObj.get("className").getAsString();
    String message = jsonObj.get("message").getAsString();
    JsonArray stackTraces = jsonObj.get("stackTraces").getAsJsonArray();
    StackTraceElement[] stackTraceElements = context.deserialize(stackTraces, StackTraceElement[].class);
    JsonElement cause = jsonObj.get("cause");
    BasicThrowable basicThrowable = null;
    if (cause != null) {
        basicThrowable = context.deserialize(cause, BasicThrowable.class);
    }/*from w  w  w . j a va  2 s. co m*/
    return new BasicThrowable(className, message, stackTraceElements, basicThrowable);
}

From source file:co.cask.cdap.proto.codec.EntityIdTypeAdapter.java

License:Apache License

@Override
public EntityId deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject map = json.getAsJsonObject();
    JsonElement entityTypeJson = map.get("entity");
    if (entityTypeJson == null) {
        throw new JsonParseException("Expected entity in EntityId JSON");
    }//w w w . ja  va  2 s  .co m

    String entityTypeString = entityTypeJson.getAsString();
    EntityType type = EntityType.valueOf(entityTypeString);
    if (type == null) {
        throw new JsonParseException("Invalid entity: " + entityTypeString);
    }

    return context.deserialize(json, type.getIdClass());
}

From source file:co.cask.cdap.proto.codec.FlowletSpecificationCodec.java

License:Apache License

@Override
public FlowletSpecification deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObj = json.getAsJsonObject();

    String className = jsonObj.get("className").getAsString();
    String name = jsonObj.get("name").getAsString();
    String description = jsonObj.get("description").getAsString();
    FailurePolicy policy = FailurePolicy.valueOf(jsonObj.get("failurePolicy").getAsString());
    Set<String> dataSets = deserializeSet(jsonObj.get("datasets"), context, String.class);
    Map<String, String> properties = deserializeMap(jsonObj.get("properties"), context, String.class);
    Resources resources = context.deserialize(jsonObj.get("resources"), Resources.class);

    return new DefaultFlowletSpecification(className, name, description, policy, dataSets, properties,
            resources);/* ww w. j  a v  a  2s .c  om*/
}

From source file:co.cask.cdap.proto.codec.IdTypeAdapter.java

License:Apache License

@Override
public Id deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject map = json.getAsJsonObject();
    JsonElement elementTypeJson = map.get("elementType");
    if (elementTypeJson == null) {
        throw new JsonParseException("Expected elementType in Id JSON");
    }/* w  ww .ja  va 2s  .  c om*/

    String elementTypeString = elementTypeJson.getAsString();
    EntityType type = EntityType.valueOf(elementTypeString);
    if (type == null) {
        throw new JsonParseException("Invalid elementType: " + elementTypeString);
    }

    return context.deserialize(json, type.getIdClass());
}

From source file:co.cask.cdap.proto.codec.MapReduceSpecificationCodec.java

License:Apache License

/**
 * Deserialize the resources field from the serialized object.
 *
 * @param jsonObj The object representing the MapReduceSpecification
 * @param prefix Field name prefix. Either "mapper" or "reducer"
 * @param context The context to deserialize object.
 * @return A {@link Resources} or {@code null}.
 *//*from  w  w  w.ja  va 2s  .com*/
private Resources deserializeResources(JsonObject jsonObj, String prefix, JsonDeserializationContext context) {
    // See if it of new format
    String name = prefix + "Resources";
    JsonElement element = jsonObj.get(name);
    if (element != null) {
        return context.deserialize(element, Resources.class);
    }

    // Try the old format, which is an int field representing the memory in MB.
    name = prefix + "MemoryMB";
    element = jsonObj.get(name);
    if (element != null) {
        return new Resources(element.getAsInt());
    }
    return null;
}

From source file:co.cask.cdap.proto.codec.ScheduleSpecificationCodec.java

License:Apache License

@Override
public ScheduleSpecification deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObj = json.getAsJsonObject();

    JsonElement scheduleTypeJson = jsonObj.get("scheduleType");
    ScheduleType scheduleType;//from   w  w  w. j  a v a 2 s.  c om
    if (scheduleTypeJson == null) {
        // For backwards compatibility with spec persisted with older versions than 2.8, we need these lines
        scheduleType = null;
    } else {
        scheduleType = context.deserialize(jsonObj.get("scheduleType"), ScheduleType.class);
    }

    Schedule schedule = null;
    if (scheduleType == null) {
        JsonObject scheduleObj = jsonObj.get("schedule").getAsJsonObject();
        String name = context.deserialize(scheduleObj.get("name"), String.class);
        String description = context.deserialize(scheduleObj.get("description"), String.class);
        String cronEntry = context.deserialize(scheduleObj.get("cronEntry"), String.class);
        schedule = Schedules.builder(name).setDescription(description).createTimeSchedule(cronEntry);
    } else {
        switch (scheduleType) {
        case TIME:
            schedule = context.deserialize(jsonObj.get("schedule"), TimeSchedule.class);
            break;
        case STREAM:
            schedule = context.deserialize(jsonObj.get("schedule"), StreamSizeSchedule.class);
            break;
        }
    }

    ScheduleProgramInfo program = context.deserialize(jsonObj.get("program"), ScheduleProgramInfo.class);
    Map<String, String> properties = deserializeMap(jsonObj.get("properties"), context, String.class);
    return new ScheduleSpecification(schedule, program, properties);
}

From source file:co.cask.cdap.proto.codec.SparkSpecificationCodec.java

License:Apache License

/**
 * Deserialize {@link Resources} object from a json property named with {@code <prefix>Resources}.
 * A {@code null} value will be returned if no such property exist.
 *///from w  w w  .jav a 2 s.  co m
@Nullable
private Resources deserializeResources(JsonObject jsonObj, String prefix, JsonDeserializationContext context) {
    String name = prefix + "Resources";
    JsonElement element = jsonObj.get(name);
    return element == null ? null : (Resources) context.deserialize(element, Resources.class);
}

From source file:co.cask.cdap.proto.codec.WorkerSpecificationCodec.java

License:Apache License

@Override
public WorkerSpecification deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObj = (JsonObject) json;

    String className = jsonObj.get("className").getAsString();
    String name = jsonObj.get("name").getAsString();
    String description = jsonObj.get("description").getAsString();
    Map<String, String> properties = deserializeMap(jsonObj.get("properties"), context, String.class);
    Resources resources = context.deserialize(jsonObj.get("resources"), Resources.class);
    Set<String> datasets = deserializeSet(jsonObj.get("datasets"), context, String.class);
    int instances = jsonObj.get("instances").getAsInt();

    return new WorkerSpecification(className, name, description, properties, datasets, resources, instances);
}