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:com.continuuity.weave.internal.json.RuntimeSpecificationCodec.java

License:Open Source License

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

    String name = jsonObj.get("name").getAsString();
    WeaveRunnableSpecification runnable = context.deserialize(jsonObj.get("runnable"),
            WeaveRunnableSpecification.class);
    ResourceSpecification resources = context.deserialize(jsonObj.get("resources"),
            ResourceSpecification.class);
    Collection<LocalFile> files = context.deserialize(jsonObj.get("files"),
            new TypeToken<Collection<LocalFile>>() {
            }.getType());//from w  w w  . ja  va2s .  c  om

    return new DefaultRuntimeSpecification(name, runnable, resources, files);
}

From source file:com.continuuity.weave.internal.json.StackTraceElementCodec.java

License:Open Source License

@Override
public StackTraceElement deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObj = json.getAsJsonObject();
    return new StackTraceElement(jsonObj.get("className").getAsString(), jsonObj.get("method").getAsString(),
            jsonObj.get("file").getAsString(), jsonObj.get("line").getAsInt());
}

From source file:com.continuuity.weave.internal.json.StateNodeCodec.java

License:Open Source License

@Override
public StateNode deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObj = json.getAsJsonObject();
    return new StateNode(ServiceController.State.valueOf(jsonObj.get("state").getAsString()),
            context.<StackTraceElement[]>deserialize(jsonObj.get("stackTraces"), StackTraceElement[].class));
}

From source file:com.continuuity.weave.internal.json.WeaveRunnableSpecificationCodec.java

License:Open Source License

@Override
public WeaveRunnableSpecification 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();
    Map<String, String> arguments = context.deserialize(jsonObj.get("arguments"),
            new TypeToken<Map<String, String>>() {
            }.getType());//from   www  .  ja  v a 2s.co m

    return new DefaultWeaveRunnableSpecification(className, name, arguments);
}

From source file:com.continuuity.weave.internal.json.WeaveRunResourcesCodec.java

License:Apache License

@Override
public WeaveRunResources deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObj = json.getAsJsonObject();
    return new DefaultWeaveRunResources(jsonObj.get("instanceId").getAsInt(),
            jsonObj.get("containerId").getAsString(), jsonObj.get("virtualCores").getAsInt(),
            jsonObj.get("memoryMB").getAsInt(), jsonObj.get("host").getAsString());
}

From source file:com.continuuity.weave.internal.json.WeaveSpecificationCodec.java

License:Open Source License

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

    String name = jsonObj.get("name").getAsString();
    Map<String, RuntimeSpecification> runnables = context.deserialize(jsonObj.get("runnables"),
            new TypeToken<Map<String, RuntimeSpecification>>() {
            }.getType());/*from ww w. j ava  2 s  . co m*/
    List<WeaveSpecification.Order> orders = context.deserialize(jsonObj.get("orders"),
            new TypeToken<List<WeaveSpecification.Order>>() {
            }.getType());

    return new DefaultWeaveSpecification(name, runnables, orders);
}

From source file:com.continuuity.weave.internal.logging.LogEntryDecoder.java

License:Open Source License

@Override
public LogEntry deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (!json.isJsonObject()) {
        return null;
    }/*w w w.ja  v  a 2 s  . c  o  m*/
    JsonObject jsonObj = json.getAsJsonObject();

    final String name = jsonObj.get("name").getAsString();
    final String host = jsonObj.get("host").getAsString();
    final long timestamp = Long.parseLong(jsonObj.get("timestamp").getAsString());
    final LogEntry.Level level = LogEntry.Level.valueOf(jsonObj.get("level").getAsString());
    final String className = jsonObj.get("className").getAsString();
    final String method = jsonObj.get("method").getAsString();
    final String file = jsonObj.get("file").getAsString();
    final String line = jsonObj.get("line").getAsString();
    final String thread = jsonObj.get("thread").getAsString();
    final String message = jsonObj.get("message").getAsString();

    final StackTraceElement[] stackTraces = context.deserialize(jsonObj.get("stackTraces").getAsJsonArray(),
            StackTraceElement[].class);

    return new LogEntry() {
        @Override
        public String getLoggerName() {
            return name;
        }

        @Override
        public String getHost() {
            return host;
        }

        @Override
        public long getTimestamp() {
            return timestamp;
        }

        @Override
        public Level getLogLevel() {
            return level;
        }

        @Override
        public String getSourceClassName() {
            return className;
        }

        @Override
        public String getSourceMethodName() {
            return method;
        }

        @Override
        public String getFileName() {
            return file;
        }

        @Override
        public int getLineNumber() {
            if (line.equals("?")) {
                return -1;
            } else {
                return Integer.parseInt(line);
            }
        }

        @Override
        public String getThreadName() {
            return thread;
        }

        @Override
        public String getMessage() {
            return message;
        }

        @Override
        public StackTraceElement[] getStackTraces() {
            return stackTraces;
        }
    };
}

From source file:com.continuuity.weave.yarn.YarnWeaveController.java

License:Open Source License

/**
 * Fetches applicationId from the ZK instance node.
 *///from w w  w.  j  av a2 s  .  co  m
private ApplicationId fetchApplicationId() {
    byte[] data = getLiveNodeData();
    if (data == null) {
        LOG.warn("No live data node.");
        return null;
    }
    JsonElement json = new Gson().fromJson(new String(data, Charsets.UTF_8), JsonElement.class);
    if (!json.isJsonObject()) {
        LOG.warn("Unable to decode live data node.");
        return null;
    }
    JsonObject jsonObj = json.getAsJsonObject();
    json = jsonObj.get("data");
    if (!json.isJsonObject()) {
        LOG.warn("Property data not found in live data node.");
        return null;
    }
    jsonObj = json.getAsJsonObject();
    ApplicationId appId = Records.newRecord(ApplicationId.class);
    appId.setId(jsonObj.get("appId").getAsInt());
    appId.setClusterTimestamp(jsonObj.get("appIdClusterTime").getAsLong());

    return appId;
}

From source file:com.continuuity.weave.yarn.YarnWeaveRunnerService.java

License:Apache License

/**
 * Decodes application ID stored inside the node data.
 * @param nodeData The node data to decode from. If it is {@code null}, this method would return {@code null}.
 * @return The ApplicationId or {@code null} if failed to decode.
 *//*from w  w  w .jav a2 s  .  co m*/
private ApplicationId getApplicationId(NodeData nodeData) {
    byte[] data = nodeData == null ? null : nodeData.getData();
    if (data == null) {
        return null;
    }

    Gson gson = new Gson();
    JsonElement json = gson.fromJson(new String(data, Charsets.UTF_8), JsonElement.class);
    if (!json.isJsonObject()) {
        LOG.warn("Unable to decode live data node.");
        return null;
    }

    JsonObject jsonObj = json.getAsJsonObject();
    json = jsonObj.get("data");
    if (!json.isJsonObject()) {
        LOG.warn("Property data not found in live data node.");
        return null;
    }

    try {
        ApplicationMasterLiveNodeData amLiveNode = gson.fromJson(json, ApplicationMasterLiveNodeData.class);
        return YarnUtils.createApplicationId(amLiveNode.getAppIdClusterTime(), amLiveNode.getAppId());
    } catch (Exception e) {
        LOG.warn("Failed to decode application live node data.", e);
        return null;
    }
}

From source file:com.couchbase.cbadmin.assets.NodeGroup.java

License:Open Source License

public NodeGroup(JsonElement json) throws RestApiException {
    Deserialized obj = new Gson().fromJson(json, Deserialized.class);

    if ((this.name = obj.name) == null) {
        throw new IllegalArgumentException("Malformed JSON");
    }//from   ww w .j a v a 2 s  . com

    this.uri = getFromString(obj.uri);
    if (obj.nodes == null) {
        throw new IllegalArgumentException("Node list was empty");
    }
    for (JsonElement e : obj.nodes) {
        nodes.add(new Node(e.getAsJsonObject()));
    }
}