Example usage for com.google.gson JsonElement getAsJsonObject

List of usage examples for com.google.gson JsonElement getAsJsonObject

Introduction

In this page you can find the example usage for com.google.gson JsonElement getAsJsonObject.

Prototype

public JsonObject getAsJsonObject() 

Source Link

Document

convenience method to get this element as a JsonObject .

Usage

From source file:co.cask.cdap.internal.app.WorkflowSpecificationCodec.java

License:Apache License

@Override
public WorkflowSpecification 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();
    List<WorkflowActionSpecification> actions = deserializeList(jsonObj.get("actions"), context,
            WorkflowActionSpecification.class);
    Map<String, MapReduceSpecification> mapReduces = deserializeMap(jsonObj.get("mapReduces"), context,
            MapReduceSpecification.class);

    List<Schedule> schedules = deserializeList(jsonObj.get("schedules"), context, Schedule.class);

    return new DefaultWorkflowSpecification(className, name, description, actions, mapReduces, schedules);
}

From source file:co.cask.cdap.internal.filesystem.LocationCodec.java

License:Apache License

@Override
public Location deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
    JsonObject jsonObj = json.getAsJsonObject();
    String uri = jsonObj.get("uri").getAsString();
    try {/*www  .j  av a 2  s .c  o  m*/
        return lf.create(new URI(uri));
    } catch (URISyntaxException e) {
        // should never happen
        throw new IllegalStateException("Invalid uri " + uri + " found. Cannot deserialize location.", e);
    }
}

From source file:co.cask.cdap.notifications.NotificationFeedInfoDeserializer.java

License:Apache License

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

    String category = obj.get("category").getAsString();
    JsonElement descriptionElement = obj.get("description");
    String description = descriptionElement == null ? "" : descriptionElement.getAsString();

    String namespace;//from   w w w .  j a va2 s .  c  o  m
    String feed;
    JsonElement namespaceElement = obj.get("namespace");
    // this means its the old Id.NotificationFeed object where namespace is something like "namespace":{"id":"default"}
    if (namespaceElement.isJsonObject()) {
        namespace = namespaceElement.getAsJsonObject().get("id").getAsString();
        feed = obj.get("name").getAsString();
    } else {
        namespace = namespaceElement.getAsString();
        feed = obj.get("feed").getAsString();
    }

    return new NotificationFeedInfo(namespace, category, feed, description);
}

From source file:co.cask.cdap.passport.http.client.PassportClient.java

License:Apache License

/**
 * Get List of VPC for the apiKey.// www .jav  a  2  s. c  om
 * @return List of VPC Names
 */
public List<String> getVPCList(String apiKey) {
    Preconditions.checkNotNull(apiKey, "ApiKey cannot be null");
    List<String> vpcList = Lists.newArrayList();

    try {
        String data = responseCache.getIfPresent(apiKey);

        if (data == null) {
            data = httpGet(API_BASE + "vpc/list", apiKey);
            if (data != null) {
                responseCache.put(apiKey, data);
            }
        }

        if (data != null) {
            JsonParser parser = new JsonParser();
            JsonElement element = parser.parse(data);
            JsonArray jsonArray = element.getAsJsonArray();

            for (JsonElement elements : jsonArray) {
                JsonObject vpc = elements.getAsJsonObject();
                if (vpc.get("vpc_name") != null) {
                    vpcList.add(vpc.get("vpc_name").getAsString());
                }
            }
        }
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
    return vpcList;
}

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;/*from   www  .jav  a2  s .  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  .  c  o m*/
    return new BasicThrowable(className, message, stackTraceElements, basicThrowable);
}

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

License:Apache License

@Override
public CustomActionSpecification 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();
    Set<String> datasets = deserializeSet(jsonObj.get("datasets"), context, String.class);
    Map<String, String> properties = deserializeMap(jsonObj.get("properties"), context, String.class);
    return new DefaultCustomActionSpecification(className, name, description, properties, datasets);
}

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");
    }/*from   www. j av  a 2  s .  c  o  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  a2  s  .  co  m
}

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

License:Apache License

@Override
public HttpServiceHandlerSpecification 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();
    Map<String, String> properties = deserializeMap(jsonObj.get("properties"), context, String.class);
    Set<String> datasets = deserializeSet(jsonObj.get("datasets"), context, String.class);
    List<ServiceHttpEndpoint> endpointsExposed;
    if (isOldSpec(jsonObj)) {
        endpointsExposed = ImmutableList.of();
    } else {//from w  w w  .jav  a 2  s  .c o m
        endpointsExposed = deserializeList(jsonObj.get("endpoints"), context, ServiceHttpEndpoint.class);
    }
    return new HttpServiceHandlerSpecification(className, name, description, properties, datasets,
            endpointsExposed);
}