List of usage examples for com.google.gson JsonElement getAsJsonObject
public JsonObject getAsJsonObject()
From source file:com.continuuity.loom.codec.json.upgrade.NodeUpgradeCodec.java
License:Apache License
@Override public Node deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { JsonObject jsonObj = json.getAsJsonObject(); String id = context.deserialize(jsonObj.get("id"), String.class); String clusterId = context.deserialize(jsonObj.get("clusterId"), String.class); Set<Service> services = context.deserialize(jsonObj.get("services"), new TypeToken<Set<Service>>() { }.getType());// www. ja v a2s .c om JsonObject properties = jsonObj.getAsJsonObject("properties"); String ipaddress = context.deserialize(properties.get("ipaddress"), String.class); String hostname = context.deserialize(properties.get("hostname"), String.class); Integer nodenum = context.deserialize(properties.get("nodenum"), Integer.class); if (nodenum == null) { nodenum = 0; } Set<String> automators = context.deserialize(properties.get("automators"), new TypeToken<Set<String>>() { }.getType()); Set<String> nodeServices = context.deserialize(properties.get("services"), new TypeToken<Set<String>>() { }.getType()); String hardwaretype = context.deserialize(properties.get("hardwaretype"), String.class); String imagetype = context.deserialize(properties.get("imagetype"), String.class); String flavor = context.deserialize(properties.get("flavor"), String.class); String image = context.deserialize(properties.get("image"), String.class); NodeProperties nodeProperties = NodeProperties.builder().setHostname(hostname).setIpaddress(ipaddress) .setNodenum(nodenum).setHardwaretype(hardwaretype).setImagetype(imagetype).setFlavor(flavor) .setImage(image).setAutomators(automators).setServiceNames(nodeServices).setSSHUser("root").build(); return new Node(id, clusterId, services, nodeProperties); }
From source file:com.continuuity.loom.codec.json.upgrade.ProviderUpgradeCodec.java
License:Apache License
@Override public Provider deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { JsonObject jsonObj = json.getAsJsonObject(); String name = context.deserialize(jsonObj.get("name"), String.class); String description = context.deserialize(jsonObj.get("description"), String.class); String providerType = context.deserialize(jsonObj.get("providertype"), String.class); Map<String, String> provisionerFields = deserializeProvisionerFields(jsonObj, context); return new Provider(name, description, providerType, provisionerFields); }
From source file:com.continuuity.loom.codec.json.upgrade.ProviderUpgradeCodec.java
License:Apache License
private Map<String, String> deserializeProvisionerFields(JsonObject jsonObj, JsonDeserializationContext context) { if (!jsonObj.has("provisioner")) { return Maps.newHashMap(); }//from w w w . j a va 2 s. co m JsonObject fields = jsonObj.get("provisioner").getAsJsonObject(); // if its the old type if (fields.has("auth")) { JsonElement authElement = fields.get("auth"); if (authElement.isJsonObject()) { fields = authElement.getAsJsonObject(); } } return context.deserialize(fields, new TypeToken<Map<String, String>>() { }.getType()); }
From source file:com.continuuity.loom.codec.json.upgrade.ServiceActionUpgradeCodec.java
License:Apache License
@Override public ServiceAction deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { JsonObject jsonObj = json.getAsJsonObject(); String actionType = context.deserialize(jsonObj.get("type"), String.class); Map<String, String> fields = context.deserialize(jsonObj.get("fields"), new TypeToken<Map<String, String>>() { }.getType());// w w w . ja v a2s. c o m // For backwards compatibility. if (jsonObj.has("script")) { if (fields == null) { fields = Maps.newHashMap(); } fields.put("script", jsonObj.get("script").getAsString()); } if (jsonObj.has("data")) { if (fields == null) { fields = Maps.newHashMap(); } fields.put("data", jsonObj.get("data").getAsString()); } return new ServiceAction(actionType, fields); }
From source file:com.continuuity.loom.codec.json.upgrade.ServiceUpgradeCodec.java
License:Apache License
@Override public Service deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { JsonObject jsonObj = json.getAsJsonObject(); String name = context.deserialize(jsonObj.get("name"), String.class); String description = context.deserialize(jsonObj.get("description"), String.class); Set<String> dependsOn = context.deserialize(jsonObj.get("dependson"), new TypeToken<Set<String>>() { }.getType());/* ww w. j a va2s .c o m*/ ServiceDependencies dependencies = context.deserialize(jsonObj.get("dependencies"), ServiceDependencies.class); // for backwards compatibility, "dependson" expanded to "dependencies" if (dependsOn != null && !dependsOn.isEmpty()) { if (dependencies == null) { dependencies = new ServiceDependencies(null, null, null, new ServiceStageDependencies(dependsOn, null)); } else if (dependencies.getRuntime().getRequires().isEmpty()) { dependencies = new ServiceDependencies(dependencies.getProvides(), dependencies.getConflicts(), dependencies.getInstall(), new ServiceStageDependencies(dependsOn, dependencies.getRuntime().getUses())); } } JsonObject provisioner = context.deserialize(jsonObj.get("provisioner"), JsonObject.class); Map<ProvisionerAction, ServiceAction> actions = Collections.emptyMap(); if (provisioner != null) { actions = context.deserialize(provisioner.get("actions"), new TypeToken<Map<ProvisionerAction, ServiceAction>>() { }.getType()); } return new Service(name, description, dependencies, actions); }
From source file:com.continuuity.loom.macro.Expander.java
License:Apache License
/** * Given a JSON tree, find the element specified by the path (or the root if path is null). In that subtree, * recursively traverse all elements, and expand all String typed right hand side values. If a macro cannot be * expanded due to the cluster object missing certain data, that macro will be left unexpanded. * * @param json A JSON tree/* w w w .ja va 2 s . c o m*/ * @param path the path to expand under * @param cluster the cluster to use for expanding macros. * @param nodes the cluster nodes to use for expanding macros. * @param node the cluster node to use for expanding macros. * @return a new JSON tree if any expansion took place, and the original JSON tree otherwise. * @throws SyntaxException if a macro expression is ill-formed. * @throws IncompleteClusterException if the cluster does not have the meta data to expand all macros. */ public static JsonElement expand(JsonElement json, @Nullable java.util.List<String> path, Cluster cluster, Set<Node> nodes, Node node) throws SyntaxException, IncompleteClusterException { // if path is given, if (path != null && !path.isEmpty()) { String first = path.get(0); if (json.isJsonObject()) { JsonObject object = json.getAsJsonObject(); JsonElement json1 = object.get(first); if (json1 != null) { JsonElement expanded = expand(json1, path.subList(1, path.size()), cluster, nodes, node); if (expanded != json1) { // only construct new json object if actual expansion happened JsonObject object1 = new JsonObject(); for (Map.Entry<String, JsonElement> entry : object.entrySet()) { object1.add(entry.getKey(), entry.getKey().equals(first) ? expanded : entry.getValue()); } return object1; } } } // path was given, but either no corresponding subtree was found or no expansion happened... return json; } if (json.isJsonPrimitive()) { JsonPrimitive primitive = json.getAsJsonPrimitive(); if (primitive.isString()) { String value = primitive.getAsString(); String expanded = expand(value, cluster, nodes, node); if (!expanded.equals(value)) { // only return a new json element if actual expansion happened return new JsonPrimitive(expanded); } } } if (json.isJsonArray()) { JsonArray array = json.getAsJsonArray(); JsonArray array1 = new JsonArray(); boolean expansionHappened = false; for (JsonElement element : array) { JsonElement expanded = expand(element, path, cluster, nodes, node); if (expanded != element) { expansionHappened = true; } array1.add(expanded); } // only return a new json array if actual expansion happened if (expansionHappened) { return array1; } } if (json.isJsonObject()) { JsonObject object = json.getAsJsonObject(); JsonObject object1 = new JsonObject(); boolean expansionHappened = false; for (Map.Entry<String, JsonElement> entry : object.entrySet()) { JsonElement expanded = expand(entry.getValue(), path, cluster, nodes, node); if (expanded != entry.getValue()) { expansionHappened = true; } object1.add(entry.getKey(), expand(entry.getValue(), path, cluster, nodes, node)); } if (expansionHappened) { return object1; } } return json; }
From source file:com.continuuity.weave.internal.json.ArgumentsCodec.java
License:Open Source License
@Override public Arguments deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject jsonObj = json.getAsJsonObject(); List<String> arguments = context.deserialize(jsonObj.get("arguments"), new TypeToken<List<String>>() { }.getType());// w w w . j a v a2 s . com Map<String, Collection<String>> args = context.deserialize(jsonObj.get("runnableArguments"), new TypeToken<Map<String, Collection<String>>>() { }.getType()); ImmutableMultimap.Builder<String, String> builder = ImmutableMultimap.builder(); for (Map.Entry<String, Collection<String>> entry : args.entrySet()) { builder.putAll(entry.getKey(), entry.getValue()); } return new Arguments(arguments, builder.build()); }
From source file:com.continuuity.weave.internal.json.LocalFileCodec.java
License:Open Source License
@Override public LocalFile deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject jsonObj = json.getAsJsonObject(); String name = jsonObj.get("name").getAsString(); URI uri = URI.create(jsonObj.get("uri").getAsString()); long lastModified = jsonObj.get("lastModified").getAsLong(); long size = jsonObj.get("size").getAsLong(); boolean archive = jsonObj.get("archive").getAsBoolean(); JsonElement pattern = jsonObj.get("pattern"); return new DefaultLocalFile(name, uri, lastModified, size, archive, (pattern == null || pattern.isJsonNull()) ? null : pattern.getAsString()); }
From source file:com.continuuity.weave.internal.json.ResourceReportCodec.java
License:Apache License
@Override public ResourceReport deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject jsonObj = json.getAsJsonObject(); String appMasterId = jsonObj.get("appMasterId").getAsString(); WeaveRunResources masterResources = context.deserialize(jsonObj.get("appMasterResources"), WeaveRunResources.class); Map<String, Collection<WeaveRunResources>> resources = context.deserialize(jsonObj.get("runnableResources"), new TypeToken<Map<String, Collection<WeaveRunResources>>>() { }.getType());//from w ww. j a v a 2 s. co m return new DefaultResourceReport(appMasterId, masterResources, resources); }
From source file:com.continuuity.weave.internal.json.ResourceSpecificationCodec.java
License:Open Source License
@Override public ResourceSpecification deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject jsonObj = json.getAsJsonObject(); return new DefaultResourceSpecification(jsonObj.get("cores").getAsInt(), jsonObj.get("memorySize").getAsInt(), jsonObj.get("instances").getAsInt(), jsonObj.get("uplink").getAsInt(), jsonObj.get("downlink").getAsInt()); }