Example usage for com.google.gson JsonObject getAsJsonArray

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

Introduction

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

Prototype

public JsonArray getAsJsonArray(String memberName) 

Source Link

Document

Convenience method to get the specified member as a JsonArray.

Usage

From source file:org.apache.gobblin.compaction.audit.PinotAuditCountHttpClient.java

License:Apache License

/**
 * A thread-safe method which fetches a tier-to-count mapping.
 * The returned json object from Pinot contains below information
 * {/*from   w  w w  . j  a v a  2s  .co  m*/
 *    "aggregationResults":[
 *      {
 *          "groupByResult":[
 *            {
 *              "value":"172765137.00000",
 *              "group":[
 *                "kafka-08-tracking-local"
 *              ]
 *            }
 *          ]
 *      }
 *    ],
 *    "exceptions":[
 *    ]
 *    .....
 * }
 * @param datasetName name of dataset
 * @param start time start point in milliseconds
 * @param end time end point in milliseconds
 * @return A tier to record count mapping when succeeded. Otherwise a null value is returned
 */
public Map<String, Long> fetch(String datasetName, long start, long end) throws IOException {
    Map<String, Long> map = new HashMap<>();
    String query = "select tier, sum(count) from kafkaAudit where " + "eventType=\"" + datasetName + "\" and "
            + "beginTimestamp >= \"" + start + "\" and " + "beginTimestamp < \"" + end + "\" group by tier";

    String fullURL = targetUrl + URLEncoder.encode(query, Charsets.UTF_8.toString());
    HttpGet req = new HttpGet(fullURL);
    String rst = null;
    HttpEntity entity = null;
    log.info("Full url for {} is {}", datasetName, fullURL);

    try {
        CloseableHttpResponse response = httpClient.execute(req, HttpClientContext.create());
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode < 200 || statusCode >= 300) {
            throw new IOException(String.format("status code: %d, reason: %s", statusCode,
                    response.getStatusLine().getReasonPhrase()));
        }

        entity = response.getEntity();
        rst = EntityUtils.toString(entity);
    } finally {
        if (entity != null) {
            EntityUtils.consume(entity);
        }
    }

    JsonObject all = PARSER.parse(rst).getAsJsonObject();
    JsonArray aggregationResults = all.getAsJsonArray("aggregationResults");
    if (aggregationResults == null || aggregationResults.size() == 0) {
        log.error(all.toString());
        throw new IOException("No aggregation results " + all.toString());
    }

    JsonObject aggregation = (JsonObject) aggregationResults.get(0);
    JsonArray groupByResult = aggregation.getAsJsonArray("groupByResult");
    if (groupByResult == null || groupByResult.size() == 0) {
        log.error(aggregation.toString());
        throw new IOException("No aggregation results " + aggregation.toString());
    }

    log.info("Audit count for {} is {}", datasetName, groupByResult);
    for (JsonElement ele : groupByResult) {
        JsonObject record = (JsonObject) ele;
        map.put(record.getAsJsonArray("group").get(0).getAsString(),
                (long) Double.parseDouble(record.get("value").getAsString()));
    }

    return map;
}

From source file:org.apache.mxnetexamples.javaapi.infer.bert.BertDataParser.java

License:Apache License

/**
 * Parse the Vocabulary to JSON files//from   w ww . j av a 2  s.co m
 * [PAD], [CLS], [SEP], [MASK], [UNK] are reserved tokens
 * @param jsonFile the filePath of the vocab.json
 * @throws Exception
 */
void parseJSON(String jsonFile) throws Exception {
    Gson gson = new Gson();
    token2idx = new HashMap<>();
    idx2token = new LinkedList<>();
    JsonObject jsonObject = gson.fromJson(new FileReader(jsonFile), JsonObject.class);
    JsonArray arr = jsonObject.getAsJsonArray("idx_to_token");
    for (JsonElement element : arr) {
        idx2token.add(element.getAsString());
    }
    JsonObject preMap = jsonObject.getAsJsonObject("token_to_idx");
    for (String key : preMap.keySet()) {
        token2idx.put(key, preMap.get(key).getAsInt());
    }
}

From source file:org.apache.nifi.stateless.core.StatelessFlow.java

License:Apache License

public static StatelessFlow createAndEnqueueFromJSON(final JsonObject args, final ClassLoader systemClassLoader,
        final File narWorkingDir)
        throws InitializationException, IOException, ProcessorInstantiationException, NiFiRegistryException {
    if (args == null) {
        throw new IllegalArgumentException("Flow arguments can not be null");
    }/* ww w  . ja v a  2s . c o  m*/

    System.out.println("Running flow from json: " + args.toString());

    if (!args.has(REGISTRY) || !args.has(BUCKETID) || !args.has(FLOWID)) {
        throw new IllegalArgumentException(
                "The following parameters must be provided: " + REGISTRY + ", " + BUCKETID + ", " + FLOWID);
    }

    final String registryurl = args.getAsJsonPrimitive(REGISTRY).getAsString();
    final String bucketID = args.getAsJsonPrimitive(BUCKETID).getAsString();
    final String flowID = args.getAsJsonPrimitive(FLOWID).getAsString();

    int flowVersion = -1;
    if (args.has(FLOWVERSION)) {
        flowVersion = args.getAsJsonPrimitive(FLOWVERSION).getAsInt();
    }

    boolean materializeContent = true;
    if (args.has(MATERIALIZECONTENT)) {
        materializeContent = args.getAsJsonPrimitive(MATERIALIZECONTENT).getAsBoolean();
    }

    final List<String> failurePorts = new ArrayList<>();
    if (args.has(FAILUREPORTS)) {
        args.getAsJsonArray(FAILUREPORTS).forEach(port -> failurePorts.add(port.getAsString()));
    }

    final SSLContext sslContext = getSSLContext(args);
    final VersionedFlowSnapshot snapshot = new RegistryUtil(registryurl, sslContext).getFlowByID(bucketID,
            flowID, flowVersion);

    final Map<VariableDescriptor, String> inputVariables = new HashMap<>();
    final VersionedProcessGroup versionedGroup = snapshot.getFlowContents();
    if (versionedGroup != null) {
        for (final Map.Entry<String, String> entry : versionedGroup.getVariables().entrySet()) {
            final String variableName = entry.getKey();
            final String variableValue = entry.getValue();
            inputVariables.put(new VariableDescriptor(variableName), variableValue);
        }
    }

    final Set<Parameter> parameters = new HashSet<>();
    final Set<String> parameterNames = new HashSet<>();
    if (args.has(PARAMETERS)) {
        final JsonElement parametersElement = args.get(PARAMETERS);
        final JsonObject parametersObject = parametersElement.getAsJsonObject();

        for (final Map.Entry<String, JsonElement> entry : parametersObject.entrySet()) {
            final String parameterName = entry.getKey();
            final JsonElement valueElement = entry.getValue();

            if (parameterNames.contains(parameterName)) {
                throw new IllegalStateException("Cannot parse configuration because Parameter '" + parameterName
                        + "' has been defined twice");
            }

            parameterNames.add(parameterName);

            if (valueElement.isJsonObject()) {
                final JsonObject valueObject = valueElement.getAsJsonObject();

                final boolean sensitive;
                if (valueObject.has(PARAMETER_SENSITIVE)) {
                    sensitive = valueObject.get(PARAMETER_SENSITIVE).getAsBoolean();
                } else {
                    sensitive = false;
                }

                if (valueObject.has(PARAMETER_VALUE)) {
                    final String value = valueObject.get(PARAMETER_VALUE).getAsString();
                    final ParameterDescriptor descriptor = new ParameterDescriptor.Builder().name(parameterName)
                            .sensitive(sensitive).build();
                    final Parameter parameter = new Parameter(descriptor, value);
                    parameters.add(parameter);
                } else {
                    throw new IllegalStateException("Cannot parse configuration because Parameter '"
                            + parameterName + "' does not have a value associated with it");
                }
            } else {
                final String parameterValue = entry.getValue().getAsString();
                final ParameterDescriptor descriptor = new ParameterDescriptor.Builder().name(parameterName)
                        .build();
                final Parameter parameter = new Parameter(descriptor, parameterValue);
                parameters.add(parameter);
            }
        }
    }

    final ParameterContext parameterContext = new StatelessParameterContext(parameters);
    final ExtensionManager extensionManager = ExtensionDiscovery.discover(narWorkingDir, systemClassLoader);
    final StatelessFlow flow = new StatelessFlow(snapshot.getFlowContents(), extensionManager,
            () -> inputVariables, failurePorts, materializeContent, sslContext, parameterContext);
    flow.enqueueFromJSON(args);
    return flow;
}

From source file:org.apache.nifi.stateless.core.StatelessFlow.java

License:Apache License

public void enqueueFromJSON(final JsonObject json) {
    final JsonArray flowFiles;
    if (json.has(FLOWFILES)) {
        flowFiles = json.getAsJsonArray(FLOWFILES);
    } else {/*from   w ww .j  a v  a  2 s . com*/
        return;
    }

    if (flowFiles.size() == 0) {
        return;
    }

    if (sourceComponent == null) {
        throw new IllegalStateException("Configuration specifies to inject " + flowFiles.size()
                + " FlowFiles into the flow, but the Flow does not contain an Input Port.");
    }

    final Queue<StatelessFlowFile> input = new LinkedList<>();
    flowFiles.forEach(f -> {
        final JsonObject file = f.getAsJsonObject();
        final String content = file.getAsJsonPrimitive(CONTENT).getAsString();

        final Map<String, String> attributes = new HashMap<>();
        file.entrySet().forEach(entry -> {
            if (!CONTENT.equals(entry.getKey())) {
                attributes.put(entry.getKey(), entry.getValue().getAsString());
            }
        });

        input.add(new StatelessFlowFile(content, attributes, sourceComponent.isMaterializeContent()));
    });

    //enqueue data
    sourceComponent.enqueueAll(input);
}

From source file:org.apache.twill.internal.json.ResourceSpecificationCodec.java

License:Apache License

@Override
public ResourceSpecification deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObj = json.getAsJsonObject();
    final List<String> hosts = context.deserialize(jsonObj.getAsJsonArray("hosts"), List.class);
    final List<String> racks = context.deserialize(jsonObj.getAsJsonArray("racks"), List.class);
    return new DefaultResourceSpecification(jsonObj.get("cores").getAsInt(),
            jsonObj.get("memorySize").getAsInt(), jsonObj.get("instances").getAsInt(),
            jsonObj.get("uplink").getAsInt(), jsonObj.get("downlink").getAsInt());
}

From source file:org.apache.zeppelin.interpreter.InterpreterSetting.java

License:Apache License

public void convertPermissionsFromUsersToOwners(JsonObject jsonObject) {
    if (jsonObject != null) {
        JsonObject option = jsonObject.getAsJsonObject("option");
        if (option != null) {
            JsonArray users = option.getAsJsonArray("users");
            if (users != null) {
                if (this.option.getOwners() == null) {
                    this.option.owners = new LinkedList<>();
                }//from  w ww . j a v  a 2  s.com
                for (JsonElement user : users) {
                    this.option.getOwners().add(user.getAsString());
                }
            }
        }
    }
}

From source file:org.ballerinalang.composer.service.ballerina.parser.service.util.BLangFragmentParser.java

License:Open Source License

public static String parseFragment(WorkspaceDocumentManager docManager, BLangSourceFragment sourceFragment) {
    try {//from  w ww  .  j a  v  a  2 s .c o  m
        String parsableString = getParsableString(sourceFragment);
        JsonElement jsonElement = getJsonModel(docManager, parsableString);
        if (jsonElement instanceof JsonObject) {
            JsonObject jsonModel = (JsonObject) jsonElement;
            if (jsonModel.getAsJsonArray(SYNTAX_ERRORS) != null) {
                return jsonModel.toString();
            }
            JsonObject jsonNodeForFragment = getJsonNodeForFragment(jsonModel, sourceFragment);
            return jsonNodeForFragment.toString();
        } else {
            throw new Exception("Incorrect format of the generated JSON");
        }
    } catch (Exception e) {
        logger.error("Error while parsing BLang fragment.", e);
        JsonObject errObj = new JsonObject();
        errObj.addProperty(ERROR, e.getMessage());
        return errObj.toString();
    }
}

From source file:org.ballerinalang.composer.service.ballerina.parser.service.util.BLangFragmentParser.java

License:Open Source License

protected static JsonObject getJsonNodeForFragment(JsonObject jsonModel, BLangSourceFragment fragment) {
    JsonObject fragmentNode = null;/*from ww  w . ja v a2s .  c o  m*/
    JsonArray jsonArray = jsonModel.getAsJsonArray(BLangJSONModelConstants.TOP_LEVEL_NODES);
    JsonObject rootConstruct = jsonArray.get(0).getAsJsonObject(); // 0 is package def
    switch (fragment.getExpectedNodeType()) {
    case BLangFragmentParserConstants.TOP_LEVEL_NODE:
        fragmentNode = rootConstruct;
        break;
    case BLangFragmentParserConstants.SERVICE_RESOURCE:
        fragmentNode = rootConstruct.getAsJsonArray(BLangJSONModelConstants.RESOURCES).get(0).getAsJsonObject();
        break;
    case BLangFragmentParserConstants.CONNECTOR_ACTION:
        fragmentNode = rootConstruct.getAsJsonArray(BLangJSONModelConstants.ACTIONS).get(0).getAsJsonObject();
        break;
    case BLangFragmentParserConstants.WORKER:
        fragmentNode = rootConstruct.getAsJsonArray(BLangJSONModelConstants.WORKERS).get(0).getAsJsonObject();
        break;
    case BLangFragmentParserConstants.VARIABLE_REFERENCE_LIST:
        // 0 & 1 are function args and return types, 2 is the assignment statement
        JsonObject assignmentStmt = rootConstruct.getAsJsonArray(BLangJSONModelConstants.CHILDREN).get(2)
                .getAsJsonObject();
        // 0th child is the var ref list expression of assignment stmt
        fragmentNode = assignmentStmt.getAsJsonArray(BLangJSONModelConstants.CHILDREN).get(0).getAsJsonObject();
        break;
    case BLangFragmentParserConstants.FIELD_DEFINITION_LIST:
        // 0th element in the fields property of the record is fieldVariable.
        fragmentNode = rootConstruct.getAsJsonObject(BLangJSONModelConstants.TYPE_NODE)
                .getAsJsonArray(BLangJSONModelConstants.FIELDS).get(0).getAsJsonObject();
        break;
    case BLangFragmentParserConstants.ANON_RECORD:
        fragmentNode = jsonModel;
        break;
    case BLangFragmentParserConstants.TRANSACTION_FAILED:
    case BLangFragmentParserConstants.EXPRESSION:
    case BLangFragmentParserConstants.STATEMENT:
        // For Expression - 0th child is the var ref expression of var def stmt
        // For Statement - 0 & 1 are function args and return types, 2 is the statement came from source
        // fragment
        fragmentNode = rootConstruct.getAsJsonObject(BLangJSONModelConstants.BODY)
                .getAsJsonArray(BLangJSONModelConstants.STATEMENTS).get(0).getAsJsonObject();
        break;
    case BLangFragmentParserConstants.ENDPOINT_VAR_DEF:
        fragmentNode = rootConstruct.getAsJsonArray(BLangJSONModelConstants.ENDPOINT_NODES).get(0)
                .getAsJsonObject();
        break;
    case BLangFragmentParserConstants.JOIN_CONDITION:
        JsonObject bodyJsonObj = rootConstruct.getAsJsonObject(BLangJSONModelConstants.BODY);
        fragmentNode = bodyJsonObj.getAsJsonArray(BLangJSONModelConstants.STATEMENTS).get(0).getAsJsonObject();
        break;
    case BLangFragmentParserConstants.ARGUMENT_PARAMETER:
        fragmentNode = rootConstruct.getAsJsonArray(BLangJSONModelConstants.PARAMETERS).get(0)
                .getAsJsonObject();
        break;
    case BLangFragmentParserConstants.RETURN_PARAMETER:
        fragmentNode = rootConstruct.getAsJsonArray(BLangJSONModelConstants.RETURN_PARAMETERS).get(0)
                .getAsJsonObject();
        break;
    default:
        fragmentNode = new JsonObject();
        fragmentNode.addProperty(ERROR, "cannot find node for given fragment");
    }
    return fragmentNode;
}

From source file:org.ballerinalang.composer.service.workspace.rest.datamodel.BLangFragmentParser.java

License:Open Source License

public static String parseFragment(BLangSourceFragment sourceFragment) {
    try {// ww  w. jav a 2s.c o m
        String parsableString = getParsableString(sourceFragment);
        JsonObject jsonModel = getJsonModel(parsableString);
        if (jsonModel.getAsJsonArray(SYNTAX_ERRORS) != null) {
            return jsonModel.toString();
        }
        JsonObject jsonNodeForFragment = getJsonNodeForFragment(jsonModel, sourceFragment);
        return jsonNodeForFragment.toString();
    } catch (Exception e) {
        logger.error("Error while parsing BLang fragment.", e);
        JsonObject errObj = new JsonObject();
        errObj.addProperty(ERROR, e.getMessage());
        return errObj.toString();
    }
}

From source file:org.ballerinalang.composer.service.workspace.rest.datamodel.BLangFragmentParser.java

License:Open Source License

protected static JsonObject getJsonNodeForFragment(JsonObject jsonModel, BLangSourceFragment fragment) {
    JsonObject fragmentNode = null;//from   ww  w.  jav a 2 s . c om
    JsonArray jsonArray = jsonModel.getAsJsonArray(BLangJSONModelConstants.ROOT);
    JsonObject functionObj = jsonArray.get(1).getAsJsonObject(); // 0 is package def
    switch (fragment.getExpectedNodeType()) {
    case BLangFragmentParserConstants.EXPRESSION:
        // 0 & 1 are function args and return types, 2 is the var def stmt
        JsonObject varDef = functionObj.getAsJsonArray(BLangJSONModelConstants.CHILDREN).get(2)
                .getAsJsonObject();
        // 0th child is the var ref expression of var def stmt
        fragmentNode = varDef.getAsJsonArray(BLangJSONModelConstants.CHILDREN).get(1).getAsJsonObject();
        break;
    case BLangFragmentParserConstants.VARIABLE_REFERENCE_LIST:
        // 0 & 1 are function args and return types, 2 is the assignment statement
        JsonObject assignmentStmt = functionObj.getAsJsonArray(BLangJSONModelConstants.CHILDREN).get(2)
                .getAsJsonObject();
        // 0th child is the var ref list expression of assignment stmt
        fragmentNode = assignmentStmt.getAsJsonArray(BLangJSONModelConstants.CHILDREN).get(0).getAsJsonObject();
        break;
    case BLangFragmentParserConstants.STATEMENT:
        // 0 & 1 are function args and return types, 2 is the statement came from source fragment
        fragmentNode = functionObj.getAsJsonArray(BLangJSONModelConstants.CHILDREN).get(2).getAsJsonObject();
        break;
    case BLangFragmentParserConstants.JOIN_CONDITION:
        fragmentNode = functionObj.getAsJsonArray(BLangJSONModelConstants.CHILDREN).get(2).getAsJsonObject()
                .getAsJsonArray(BLangJSONModelConstants.CHILDREN).get(0).getAsJsonObject();
        fragmentNode.remove(BLangJSONModelConstants.CHILDREN);
        fragmentNode.remove(BLangJSONModelConstants.JOIN_PARAMETER);
        break;
    case BLangFragmentParserConstants.ARGUMENT_PARAMETER:
        fragmentNode = functionObj.getAsJsonArray(BLangJSONModelConstants.CHILDREN).get(0).getAsJsonObject()
                .getAsJsonArray(BLangJSONModelConstants.CHILDREN).get(0).getAsJsonObject();
        break;
    case BLangFragmentParserConstants.RETURN_PARAMETER:
        fragmentNode = functionObj.getAsJsonArray(BLangJSONModelConstants.CHILDREN).get(1).getAsJsonObject()
                .getAsJsonArray(BLangJSONModelConstants.CHILDREN).get(0).getAsJsonObject();
        break;
    case BLangFragmentParserConstants.TRANSACTION_FAILED:
        fragmentNode = functionObj.getAsJsonArray(BLangJSONModelConstants.CHILDREN).get(2).getAsJsonObject()
                .getAsJsonArray(BLangJSONModelConstants.CHILDREN).get(1).getAsJsonObject()
                .getAsJsonArray(BLangJSONModelConstants.CHILDREN).get(0).getAsJsonObject();
        break;
    default:
        fragmentNode = new JsonObject();
        fragmentNode.addProperty(ERROR, "cannot find node for given fragment");
    }
    return fragmentNode;
}