Example usage for com.google.gson JsonObject get

List of usage examples for com.google.gson JsonObject get

Introduction

In this page you can find the example usage for com.google.gson JsonObject get.

Prototype

public JsonElement get(String memberName) 

Source Link

Document

Returns the member with the specified name.

Usage

From source file:co.cask.cdap.api.dataset.lib.ConditionCodec.java

License:Apache License

@Override
public PartitionFilter.Condition deserialize(JsonElement jsonElement, Type type,
        JsonDeserializationContext deserializationContext) throws JsonParseException {

    JsonObject jsonObject = jsonElement.getAsJsonObject();
    boolean isSingleValue = jsonObject.get("isSingleValue").getAsBoolean();
    if (isSingleValue) {
        return new PartitionFilter.Condition<>(jsonObject.get("fieldName").getAsString(),
                deserializeComparable(jsonObject.get("lower"), deserializationContext));
    } else {/*from  ww w  .  jav  a 2s  .  c  o m*/
        return new PartitionFilter.Condition<>(jsonObject.get("fieldName").getAsString(),
                deserializeComparable(jsonObject.get("lower"), deserializationContext),
                deserializeComparable(jsonObject.get("upper"), deserializationContext));
    }
}

From source file:co.cask.cdap.client.rest.handlers.StreamConfigHttpRequestHandler.java

License:Apache License

@Override
public void handle(HttpRequest httpRequest, HttpResponse response, HttpContext httpContext)
        throws HttpException, IOException {

    RequestLine requestLine = httpRequest.getRequestLine();
    String method = requestLine.getMethod();
    int statusCode;
    if (!HttpMethod.PUT.equals(method)) {
        statusCode = HttpStatus.SC_NOT_IMPLEMENTED;
    } else {//  w w w.  java2 s . com
        String uri = requestLine.getUri();
        String streamName = TestUtils.getStreamNameFromUri(uri);
        if (TestUtils.SUCCESS_STREAM_NAME.equals(streamName)) {
            statusCode = HttpStatus.SC_BAD_REQUEST;
            BasicHttpEntityEnclosingRequest request = (BasicHttpEntityEnclosingRequest) httpRequest;
            HttpEntity requestEntity = request.getEntity();
            if (requestEntity != null) {
                JsonObject jsonContent = RestClient.toJsonObject(requestEntity);
                if (jsonContent != null) {
                    long ttl = jsonContent.get(TTL_ATTRIBUTE_NAME).getAsLong();
                    if (ttl == RestTest.STREAM_TTL) {
                        statusCode = HttpStatus.SC_OK;
                    }
                }
            }
        } else if (TestUtils.AUTH_STREAM_NAME.equals(streamName)) {
            statusCode = TestUtils.authorize(httpRequest);
        } else {
            statusCode = TestUtils.getStatusCodeByStreamName(streamName);
        }
    }
    response.setStatusCode(statusCode);
}

From source file:co.cask.cdap.client.rest.RestStreamClient.java

License:Apache License

@Override
public long getTTL(String stream) throws IOException {
    HttpGet getRequest = new HttpGet(restClient.getBaseURL()
            .resolve(String.format("/%s/streams/%s/info", restClient.getVersion(), stream)));
    CloseableHttpResponse httpResponse = restClient.execute(getRequest);
    long ttl;/*  w  w  w. j  a  v  a 2  s.c o m*/
    try {
        int responseCode = httpResponse.getStatusLine().getStatusCode();
        LOG.debug("Get TTL Response Code : {}", responseCode);
        RestClient.responseCodeAnalysis(httpResponse);
        JsonObject jsonContent = RestClient.toJsonObject(httpResponse.getEntity());
        ttl = jsonContent.get(TTL_ATTRIBUTE_NAME).getAsLong();
    } finally {
        httpResponse.close();
    }
    return ttl;
}

From source file:co.cask.cdap.common.conf.PluginClassDeserializer.java

License:Apache License

@Override
public PluginClass deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (!json.isJsonObject()) {
        throw new JsonParseException("PluginClass should be a JSON Object");
    }/*from ww w.jav  a 2s.c o m*/

    JsonObject jsonObj = json.getAsJsonObject();

    String type = jsonObj.has("type") ? jsonObj.get("type").getAsString() : Plugin.DEFAULT_TYPE;
    String name = getRequired(jsonObj, "name").getAsString();
    String description = jsonObj.has("description") ? jsonObj.get("description").getAsString() : "";
    String className = getRequired(jsonObj, "className").getAsString();

    Set<String> endpointsSet = new HashSet<>();
    if (jsonObj.has("endpoints")) {
        endpointsSet = context.deserialize(jsonObj.get("endpoints"), ENDPOINTS_TYPE);
    }

    Map<String, PluginPropertyField> properties = jsonObj.has("properties")
            ? context.<Map<String, PluginPropertyField>>deserialize(jsonObj.get("properties"), PROPERTIES_TYPE)
            : ImmutableMap.<String, PluginPropertyField>of();

    return new PluginClass(type, name, description, className, null, properties, endpointsSet);
}

From source file:co.cask.cdap.common.conf.PluginClassDeserializer.java

License:Apache License

private JsonElement getRequired(JsonObject jsonObj, String name) {
    if (!jsonObj.has(name)) {
        throw new JsonParseException("Property '" + name + "' is missing from PluginClass.");
    }//w ww. j  a  v  a2s  .  com
    return jsonObj.get(name);
}

From source file:co.cask.cdap.common.zookeeper.coordination.DiscoverableCodec.java

License:Apache License

@Override
public Discoverable deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObj = json.getAsJsonObject();
    final String service = jsonObj.get("service").getAsString();
    String hostname = jsonObj.get("hostname").getAsString();
    int port = jsonObj.get("port").getAsInt();
    final InetSocketAddress address = new InetSocketAddress(hostname, port);

    return new Discoverable() {
        @Override//from   ww  w  .j  a v a2s.  c  o  m
        public String getName() {
            return service;
        }

        @Override
        public InetSocketAddress getSocketAddress() {
            return address;
        }
    };
}

From source file:co.cask.cdap.common.zookeeper.coordination.ResourceAssignmentTypeAdapter.java

License:Apache License

@Override
public ResourceAssignment deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (!json.isJsonObject()) {
        throw new JsonParseException("Expect a json object, got " + json);
    }//  w  w w  . j  a  v a2  s . c  o  m

    JsonObject jsonObj = json.getAsJsonObject();
    String name = jsonObj.get("name").getAsString();

    Multimap<Discoverable, PartitionReplica> assignments = TreeMultimap
            .create(DiscoverableComparator.COMPARATOR, PartitionReplica.COMPARATOR);
    JsonArray assignmentsJson = context.deserialize(jsonObj.get("assignments"), JsonArray.class);
    for (JsonElement element : assignmentsJson) {
        if (!element.isJsonArray()) {
            throw new JsonParseException("Expect a json array, got " + element);
        }

        JsonArray entryJson = element.getAsJsonArray();
        if (entryJson.size() != 2) {
            throw new JsonParseException("Expect json array of size = 2, got " + entryJson.size());
        }
        Discoverable key = context.deserialize(entryJson.get(0), Discoverable.class);
        PartitionReplica value = context.deserialize(entryJson.get(1), PartitionReplica.class);
        assignments.put(key, value);
    }

    return new ResourceAssignment(name, assignments);
}

From source file:co.cask.cdap.data.tools.UpgradeTool.java

License:Apache License

private void upgradeMetadataDatasetSpec(MetadataScope scope, DatasetId metadataDatasetId) {
    DatasetSpecification oldMetadataDatasetSpec = datasetInstanceManager.get(metadataDatasetId);
    if (oldMetadataDatasetSpec == null) {
        LOG.info("Metadata Dataset {} not found. No upgrade necessary.", metadataDatasetId);
        return;/*from  w w  w .  ja va 2s .  com*/
    }
    // Updating the type in the spec using Gson. Doing choosing this option over:
    // 1. Build a new DatasetSpecification using the DatasetSpecification Builder: This seems clean, but because
    // of the namespacing logic in the builder, you would need to change names of the embedded datasets first,
    // leading to unnecessary complex logic for this temporary code.
    // TODO: CDAP-7834: Adding new indexed columns should be supported by IndexedTable.
    // TODO: CDAP-7835: This should be moved out (probably to MetadataService) so it can be run after CDAP starts up.
    Gson gson = new Gson();
    JsonObject jsonObject = gson.toJsonTree(oldMetadataDatasetSpec, DatasetSpecification.class)
            .getAsJsonObject();
    JsonObject metadataDatasetProperties = jsonObject.get("properties").getAsJsonObject();
    metadataDatasetProperties.addProperty("scope", scope.name());
    // change the columnsToIndex since in 4.0 we added 4 more index columns
    JsonObject metadataIndexObject = jsonObject.get("datasetSpecs").getAsJsonObject().get("metadata_index")
            .getAsJsonObject();
    JsonObject properties = metadataIndexObject.get("properties").getAsJsonObject();
    properties.addProperty("columnsToIndex", MetadataDataset.COLUMNS_TO_INDEX);
    properties.addProperty("scope", scope.name());
    JsonObject dProperties = metadataIndexObject.get("datasetSpecs").getAsJsonObject().get("d")
            .getAsJsonObject().get("properties").getAsJsonObject();
    JsonObject iProperties = metadataIndexObject.get("datasetSpecs").getAsJsonObject().get("i")
            .getAsJsonObject().get("properties").getAsJsonObject();
    dProperties.addProperty("columnsToIndex", MetadataDataset.COLUMNS_TO_INDEX);
    dProperties.addProperty("scope", scope.name());
    iProperties.addProperty("columnsToIndex", MetadataDataset.COLUMNS_TO_INDEX);
    iProperties.addProperty("scope", scope.name());

    DatasetSpecification newMetadataDatasetSpec = gson.fromJson(jsonObject, DatasetSpecification.class);
    datasetInstanceManager.delete(metadataDatasetId);
    datasetInstanceManager.add(NamespaceId.SYSTEM, newMetadataDatasetSpec);
    LOG.info("Found old Metadata Dataset Spec {}. Upgraded it to new spec {}.", oldMetadataDatasetSpec,
            newMetadataDatasetSpec);
}

From source file:co.cask.cdap.etl.common.SetMultimapCodec.java

License:Apache License

@Override
public SetMultimap<K, V> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject obj = json.getAsJsonObject();
    Map<K, Collection<V>> map = context.deserialize(obj.get("map"), mapType);
    SetMultimap<K, V> multimap = HashMultimap.create();
    for (Map.Entry<K, Collection<V>> entry : map.entrySet()) {
        multimap.putAll(entry.getKey(), entry.getValue());
    }//from  w w  w  .  j  a  va2  s .c om
    return multimap;
}

From source file:co.cask.cdap.etl.mock.realtime.StructuredRecordCodec.java

License:Apache License

@Override
public StructuredRecord deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject obj = json.getAsJsonObject();
    try {//from  w w w  .  j  a v  a  2 s.com
        Schema schema = Schema.parseJson(obj.get("schema").getAsString());
        return StructuredRecordStringConverter.fromJsonString(obj.get("record").getAsString(), schema);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}