Example usage for com.google.gson JsonElement toString

List of usage examples for com.google.gson JsonElement toString

Introduction

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

Prototype

@Override
public String toString() 

Source Link

Document

Returns a String representation of this element.

Usage

From source file:gittest.API.java

public static HashMap<String, Object> createHashMapFromJsonString(String json) {

    JsonObject object = (JsonObject) parser.parse(json);
    Set<Map.Entry<String, JsonElement>> set = object.entrySet();
    Iterator<Map.Entry<String, JsonElement>> iterator = set.iterator();
    HashMap<String, Object> map = new HashMap<String, Object>();

    while (iterator.hasNext()) {

        Map.Entry<String, JsonElement> entry = iterator.next();
        String key = entry.getKey();
        JsonElement value = entry.getValue();

        if (null != value) {
            if (!value.isJsonPrimitive()) {
                if (value.isJsonObject()) {

                    map.put(key, createHashMapFromJsonString(value.toString()));
                } else if (value.isJsonArray() && value.toString().contains(":")) {

                    List<HashMap<String, Object>> list = new ArrayList<>();
                    JsonArray array = value.getAsJsonArray();
                    if (null != array) {
                        for (JsonElement element : array) {
                            list.add(createHashMapFromJsonString(element.toString()));
                        }/* w  w  w .  j a v a  2 s.c  om*/
                        map.put(key, list);
                    }
                } else if (value.isJsonArray() && !value.toString().contains(":")) {
                    map.put(key, value.getAsJsonArray());
                }
            } else {
                map.put(key, value.getAsString());
            }
        }
    }

    return map;
}

From source file:gobblin.configuration.WorkUnitState.java

License:Apache License

/**
 * Backoff the actual high watermark to the low watermark returned by {@link WorkUnit#getLowWatermark()}.
 *///from   w ww.j  av  a  2 s.c  o  m
public void backoffActualHighWatermark() {
    JsonElement lowWatermark = this.workUnit.getLowWatermark();
    if (lowWatermark == null) {
        return;
    }
    setProp(ConfigurationKeys.WORK_UNIT_STATE_ACTUAL_HIGH_WATER_MARK_KEY, lowWatermark.toString());
}

From source file:gobblin.couchbase.converter.AnyToCouchbaseJsonConverter.java

License:Apache License

@Override
public Iterable<RawJsonDocument> convertRecord(String outputSchema, Object inputRecord, WorkUnitState workUnit)
        throws DataConversionException {

    JsonElement jsonElement = GSON.toJsonTree(inputRecord);
    if (!jsonElement.isJsonObject()) {
        throw new DataConversionException(
                "Expecting json element " + jsonElement.toString() + " to be of type JsonObject.");
    }//from   w w  w .j  a  v a2s  .c o m

    JsonObject jsonObject = jsonElement.getAsJsonObject();

    if (!jsonObject.has(keyField)) {
        throw new DataConversionException(
                "Could not find key field " + keyField + " in json object " + jsonObject.toString());
    }

    JsonElement keyValueElement = jsonObject.get(keyField);
    String keyString;
    try {
        keyString = keyValueElement.getAsString();
    } catch (Exception e) {
        throw new DataConversionException(
                "Could not get the key " + keyValueElement.toString() + " as a string", e);
    }
    String valueString = GSON.toJson(jsonElement);

    RawJsonDocument jsonDocument = RawJsonDocument.create(keyString, valueString);
    return new SingleRecordIterable<>(jsonDocument);
}

From source file:gobblin.writer.http.SalesforceRestWriter.java

License:Apache License

private HttpUriRequest newRequest(RequestBuilder builder, JsonElement payload) {
    try {//from w w w . ja v  a  2  s  .c o  m
        builder.addHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.getMimeType())
                .addHeader(HttpHeaders.AUTHORIZATION, "OAuth " + accessToken)
                .setEntity(new StringEntity(payload.toString(), ContentType.APPLICATION_JSON));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    if (getLog().isDebugEnabled()) {
        getLog().debug("Request builder: "
                + ToStringBuilder.reflectionToString(builder, ToStringStyle.SHORT_PREFIX_STYLE));
    }
    return builder.build();
}

From source file:gov.nasa.jpl.mudrod.metadata.pre.ApiHarvester.java

License:Apache License

private void importSingleFileToES(InputStream is) {
    try {/*www . j  a  va2 s . co  m*/
        String jsonTxt = IOUtils.toString(is);
        JsonParser parser = new JsonParser();
        JsonElement item = parser.parse(jsonTxt);
        IndexRequest ir = new IndexRequest(props.getProperty(MudrodConstants.ES_INDEX_NAME),
                props.getProperty(MudrodConstants.RAW_METADATA_TYPE)).source(item.toString());
        es.getBulkProcessor().add(ir);
    } catch (IOException e) {
        LOG.error("Error indexing metadata record!", e);
    }
}

From source file:gov.nasa.jpl.mudrod.metadata.pre.ApiHarvester.java

License:Apache License

/**
 * harvestMetadatafromWeb: Harvest metadata from PO.DAAC web service.
 *///from   www  . j  a va2s . c o m
private void harvestMetadatafromWeb() {
    LOG.info("Metadata download started.");
    int startIndex = 0;
    int doc_length = 0;
    JsonParser parser = new JsonParser();
    do {
        String searchAPI = "https://podaac.jpl.nasa.gov/api/dataset?startIndex=" + Integer.toString(startIndex)
                + "&entries=10&sortField=Dataset-AllTimePopularity&sortOrder=asc&id=&value=&search=";
        HttpRequest http = new HttpRequest();
        String response = http.getRequest(searchAPI);

        JsonElement json = parser.parse(response);
        JsonObject responseObject = json.getAsJsonObject();
        JsonArray docs = responseObject.getAsJsonObject("response").getAsJsonArray("docs");

        doc_length = docs.size();

        File file = new File(props.getProperty(MudrodConstants.RAW_METADATA_PATH));
        if (!file.exists()) {
            if (file.mkdir()) {
                LOG.info("Directory is created!");
            } else {
                LOG.error("Failed to create directory!");
            }
        }
        for (int i = 0; i < doc_length; i++) {
            JsonElement item = docs.get(i);
            int docId = startIndex + i;
            File itemfile = new File(
                    props.getProperty(MudrodConstants.RAW_METADATA_PATH) + "/" + docId + ".json");

            try (FileWriter fw = new FileWriter(itemfile.getAbsoluteFile());
                    BufferedWriter bw = new BufferedWriter(fw);) {
                itemfile.createNewFile();
                bw.write(item.toString());
            } catch (IOException e) {
                LOG.error("Error writing metadata to local file!", e);
            }
        }

        startIndex += 10;

        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            LOG.error("Error entering Elasticsearch Mappings!", e);
            Thread.currentThread().interrupt();
        }

    } while (doc_length != 0);

    LOG.info("Metadata downloading finished");
}

From source file:gov.nasa.jpl.mudrod.recommendation.pre.ImportMetadata.java

License:Apache License

/**
 * importToES: Index metadata into elasticsearch from local file directory.
 * Please make sure metadata have been harvest from web service before
 * invoking this method.//  w ww .j ava2s  .  co m
 */
private void importToES() {
    es.deleteType(props.getProperty("indexName"), props.getProperty("recom_metadataType"));

    es.createBulkProcessor();
    File directory = new File(props.getProperty(MudrodConstants.RAW_METADATA_PATH));
    File[] fList = directory.listFiles();
    for (File file : fList) {
        InputStream is;
        try {
            is = new FileInputStream(file);
            try {
                String jsonTxt = IOUtils.toString(is);
                JsonParser parser = new JsonParser();
                JsonElement item = parser.parse(jsonTxt);
                IndexRequest ir = new IndexRequest(props.getProperty(MudrodConstants.ES_INDEX_NAME),
                        props.getProperty("recom_metadataType")).source(item.toString());

                // preprocessdata

                es.getBulkProcessor().add(ir);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }

    es.destroyBulkProcessor();
}

From source file:gr.demokritos.iit.api.API.java

/**
 * Get information about relations in json form. Overload above method
 * to accept array of json in a single string instead of an array of json
 * strings.//from w ww.j av a 2 s.  c o m
 * @param json, a string of json representing an array of json of tweets
 * @param isSAG, true if json contains sag posts instead of tweets
 * @return String in JSON format, counts of relations found in tweets
 */
public static String RE(String json, boolean isSAG) {
    // obviously the plural of json is jsoni, not jsons
    // Initialize if this is the first run
    JsonArray text_array = new JsonParser().parse(json).getAsJsonArray();
    ArrayList<String> jsoni = new ArrayList();
    for (JsonElement json_text : text_array) {
        jsoni.add(json_text.toString());
    }
    return gr.demokritos.iit.re.API.RE(jsoni, isSAG);
}

From source file:gr.demokritos.iit.api.API.java

/**
 * Get information about named entities in json form. Overload above method
 * to accept array of json in a single string instead of an array of json
 * strings./*from w ww.jav  a 2 s  .  c  o m*/
 * @param json, a string of json representing an array of json of tweets
 * @param isSAG, true if json contains sag posts instead of tweets
 * @return List of json, named entities information about tweets
 */
public static List<String> NER(String json, boolean isSAG) {
    //obviously the plural of json is jsoni, not jsons
    // Initialize if this is the first run
    JsonArray text_array = new JsonParser().parse(json).getAsJsonArray();
    ArrayList<String> jsoni = new ArrayList();
    for (JsonElement json_text : text_array) {
        jsoni.add(json_text.toString());
    }
    return gr.demokritos.iit.ner.API.NER(jsoni, isSAG);
}

From source file:hd3gtv.mydmam.metadata.container.ContainerOperations.java

License:Open Source License

public static JsonObject getJsonObject(JsonElement json, boolean can_null) throws JsonParseException {
    if (json.isJsonNull()) {
        if (can_null) {
            return null;
        } else {/*from w ww  .ja  v a2  s .co m*/
            throw new JsonParseException("Json element is null");
        }
    }
    if (json.isJsonObject() == false) {
        throw new JsonParseException("Json element is not an object: " + json.toString());
    }
    return (JsonObject) json.getAsJsonObject();
}