Example usage for com.google.gson JsonObject JsonObject

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

Introduction

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

Prototype

JsonObject

Source Link

Usage

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

License:Apache License

@Override
public JsonElement serialize(PartitionKey partitionKey, Type type,
        JsonSerializationContext jsonSerializationContext) {
    JsonObject jsonObj = new JsonObject();
    for (Map.Entry<String, Comparable> entry : partitionKey.getFields().entrySet()) {
        jsonObj.add(entry.getKey(), serializeComparable(entry.getValue(), jsonSerializationContext));
    }/*from   w  ww.  ja va  2s  .c o m*/
    return jsonObj;
}

From source file:co.cask.cdap.cli.command.app.CreateAppCommand.java

License:Apache License

@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
    String appName = arguments.get(ArgumentName.APP.toString());
    Id.Application appId = Id.Application.from(cliConfig.getCurrentNamespace(), appName);

    String artifactName = arguments.get(ArgumentName.ARTIFACT_NAME.toString());
    String artifactVersion = arguments.get(ArgumentName.ARTIFACT_VERSION.toString());
    ArtifactScope artifactScope = ArtifactScope
            .valueOf(arguments.get(ArgumentName.SCOPE.toString()).toUpperCase());
    ArtifactSummary artifact = new ArtifactSummary(artifactName, artifactVersion, artifactScope);

    JsonObject config = new JsonObject();
    String configPath = arguments.getOptional(ArgumentName.APP_CONFIG_FILE.toString());
    if (configPath != null) {
        File configFile = resolver.resolvePathToFile(configPath);
        try (FileReader reader = new FileReader(configFile)) {
            AppRequest<JsonObject> appRequest = GSON.fromJson(reader, configType);
            config = appRequest.getConfig();
        }/* ww w.j av  a 2s.  c o  m*/
    }

    AppRequest<JsonObject> appRequest = new AppRequest<>(artifact, config);
    applicationClient.deploy(appId, appRequest);
    output.println("Successfully created application");
}

From source file:co.cask.cdap.cli.command.app.UpdateAppCommand.java

License:Apache License

@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
    String appName = arguments.get(ArgumentName.APP.toString());
    Id.Application appId = Id.Application.from(cliConfig.getCurrentNamespace(), appName);

    String artifactName = arguments.get(ArgumentName.ARTIFACT_NAME.toString());
    String artifactVersion = arguments.get(ArgumentName.ARTIFACT_VERSION.toString());
    ArtifactScope artifactScope = ArtifactScope
            .valueOf(arguments.get(ArgumentName.SCOPE.toString()).toUpperCase());
    ArtifactSummary artifact = new ArtifactSummary(artifactName, artifactVersion, artifactScope);

    JsonObject config = new JsonObject();
    String configPath = arguments.getOptional(ArgumentName.APP_CONFIG_FILE.toString());
    if (configPath != null) {
        File configFile = resolver.resolvePathToFile(configPath);
        try (FileReader reader = new FileReader(configFile)) {
            AppRequest<JsonObject> appRequest = GSON.fromJson(reader, CONFIG_TYPE);
            config = appRequest.getConfig();
        }/* ww  w.ja v a2 s. c  om*/
    }

    AppRequest<JsonObject> appRequest = new AppRequest<>(artifact, config);
    applicationClient.update(appId, appRequest);
    output.println("Successfully updated application");
}

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

License:Apache License

@Override
public JsonElement serialize(Discoverable src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject jsonObj = new JsonObject();
    jsonObj.addProperty("service", src.getName());
    jsonObj.addProperty("hostname", src.getSocketAddress().getHostName());
    jsonObj.addProperty("port", src.getSocketAddress().getPort());
    return jsonObj;
}

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

License:Apache License

@Override
public JsonElement serialize(ResourceAssignment src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject json = new JsonObject();
    json.addProperty("name", src.getName());

    src.getAssignments().entries();// ww  w .ja va  2 s .c o m
    JsonArray assignments = new JsonArray();

    for (Map.Entry<Discoverable, PartitionReplica> entry : src.getAssignments().entries()) {
        JsonArray entryJson = new JsonArray();
        entryJson.add(context.serialize(entry.getKey(), Discoverable.class));
        entryJson.add(context.serialize(entry.getValue()));
        assignments.add(entryJson);
    }

    json.add("assignments", assignments);

    return json;
}

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

License:Apache License

@Override
public JsonElement serialize(ServiceDiscovered serviceDiscovered, Type typeOfSrc,
        JsonSerializationContext context) {
    JsonArray object = new JsonArray();
    for (Discoverable discoverable : serviceDiscovered) {
        JsonObject discoverableJson = new JsonObject();
        discoverableJson.addProperty("host", discoverable.getSocketAddress().getHostName());
        discoverableJson.addProperty("port", discoverable.getSocketAddress().getPort());

        object.add(discoverableJson);//from   w w w  . j a v  a  2  s.c  o  m
    }
    return object;
}

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

License:Apache License

@Override
public JsonElement serialize(SetMultimap<K, V> src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject obj = new JsonObject();
    obj.add("map", context.serialize(src.asMap()));
    return obj;/*w w w . j a  va 2  s.  c o  m*/
}

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

License:Apache License

@Override
public JsonElement serialize(StructuredRecord src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject obj = new JsonObject();
    try {//from  ww  w  .j  ava 2s. c  om
        obj.addProperty("record", StructuredRecordStringConverter.toJsonString(src));
        obj.addProperty("schema", src.getSchema().toString());
        return obj;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:co.cask.cdap.etl.spark.batch.DatasetInfoTypeAdapter.java

License:Apache License

@Override
public JsonElement serialize(DatasetInfo src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject jsonObj = new JsonObject();
    jsonObj.addProperty("datasetName", src.getDatasetName());
    jsonObj.add("datasetArgs", context.serialize(src.getDatasetArgs()));
    if (src.getSplits() != null && !src.getSplits().isEmpty()) {
        jsonObj.addProperty("datasetSplitClass", src.getSplits().get(0).getClass().getName());
        jsonObj.add("datasetSplits", context.serialize(src.getSplits()));
    }//from  w ww.  j  a  v  a 2 s.  c om
    return jsonObj;
}

From source file:co.cask.cdap.etl.spark.batch.InputFormatProviderTypeAdapter.java

License:Apache License

@Override
public JsonElement serialize(InputFormatProvider src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject jsonObj = new JsonObject();
    jsonObj.addProperty("inputFormatClass", src.getInputFormatClassName());
    jsonObj.add("inputFormatConfig", context.serialize(src.getInputFormatConfiguration()));
    return jsonObj;
}