Example usage for com.google.gson JsonNull INSTANCE

List of usage examples for com.google.gson JsonNull INSTANCE

Introduction

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

Prototype

JsonNull INSTANCE

To view the source code for com.google.gson JsonNull INSTANCE.

Click Source Link

Document

singleton for JsonNull

Usage

From source file:geomesa.example.twitter.ingest.TwitterParser.java

License:Apache License

private void conditionalSetArray(SimpleFeatureBuilder builder, JsonObject obj, String feature) {
    JsonElement a = obj.get(feature);/*from w ww. j  a  va2 s  .  c om*/
    if (a != null && a != JsonNull.INSTANCE) {
        for (Iterator<JsonElement> i = ((JsonArray) a).iterator(); i.hasNext();) {
            JsonElement e = i.next();
            if (e != null && e != JsonNull.INSTANCE) {
                builder.set(feature, e.getAsString());
            }
        }
    }
}

From source file:geomesa.example.twitter.ingest.TwitterParser.java

License:Apache License

private void conditionalSetString(SimpleFeatureBuilder builder, JsonObject obj, String feature) {
    JsonElement e = obj.get(feature);//from   w w  w .ja  v  a2 s.  c om
    if (e != null && e != JsonNull.INSTANCE) {
        builder.set(feature, e.getAsString());
    }
}

From source file:geomesa.example.twitter.ingest.TwitterParser.java

License:Apache License

private void conditionalSetObjectArray(SimpleFeature sf, JsonObject obj, String feature,
        String nestedAttribute) {

    JsonElement object = obj.get(feature);
    if (object != null && object != JsonNull.INSTANCE) {
        List<String> values = new ArrayList<>();
        for (Iterator<JsonElement> iter = ((JsonArray) object).iterator(); iter.hasNext();) {
            JsonElement next = iter.next();
            if (next != null && next != JsonNull.INSTANCE) {
                JsonElement attribute = ((JsonObject) next).get(nestedAttribute);
                if (attribute != null && attribute != JsonNull.INSTANCE) {
                    values.add(attribute.getAsString());
                }//from  w w w.  j av  a  2s .c  om
            }
        }
        if (!values.isEmpty()) {
            sf.setAttribute(feature, Joiner.on(",").join(values));
        }
    }
}

From source file:geomesa.example.twitter.ingest.TwitterParser.java

License:Apache License

private void conditionalSetArray(SimpleFeature sf, JsonObject obj, String feature) {
    JsonElement a = obj.get(feature);/*from  w w w  .  j a v  a2 s .c o  m*/
    if (a != null && a != JsonNull.INSTANCE) {
        for (Iterator<JsonElement> i = ((JsonArray) a).iterator(); i.hasNext();) {
            JsonElement e = i.next();
            if (e != null && e != JsonNull.INSTANCE) {
                sf.setAttribute(feature, e.getAsString());
            }
        }
    }
}

From source file:geomesa.example.twitter.ingest.TwitterParser.java

License:Apache License

private void conditionalSetString(SimpleFeature sf, JsonObject obj, String feature) {
    JsonElement e = obj.get(feature);/*from  w  w  w  .j  a v  a  2 s .  co m*/
    if (e != null && e != JsonNull.INSTANCE) {
        sf.setAttribute(feature, e.getAsString());
    }
}

From source file:geomesa.example.twitter.ingest.TwitterParser.java

License:Apache License

private JsonObject next(final JsonParser parser, final JsonReader reader, final String sourceName)
        throws IOException {
    while (reader.hasNext() && reader.peek() != JsonToken.END_DOCUMENT) {
        try {/*from ww w.j  a v a  2  s . c om*/
            final JsonElement element = parser.parse(reader);
            if (element != null && element != JsonNull.INSTANCE) {
                return element.getAsJsonObject();
            }
        } catch (Exception e) {
            log.error(sourceName + " - error parsing json", e);
            return null;
        }
    }
    return null;
}

From source file:gobblin.converter.csv.CsvToJsonConverter.java

License:Apache License

/**
 * Takes in a record with format String and splits the data based on SOURCE_SCHEMA_DELIMITER
 * Uses the inputSchema and the split record to convert the record to a JsonObject
 * @return a JsonObject representing the record
 * @throws IOException/*from w w  w.j a v  a 2s .  c  o m*/
 */
@Override
public Iterable<JsonObject> convertRecord(JsonArray outputSchema, String inputRecord, WorkUnitState workUnit)
        throws DataConversionException {
    String strDelimiter = workUnit.getProp(ConfigurationKeys.CONVERTER_CSV_TO_JSON_DELIMITER);
    if (Strings.isNullOrEmpty(strDelimiter)) {
        throw new IllegalArgumentException("Delimiter cannot be empty");
    }
    InputStreamCSVReader reader = new InputStreamCSVReader(inputRecord, strDelimiter.charAt(0),
            workUnit.getProp(ConfigurationKeys.CONVERTER_CSV_TO_JSON_ENCLOSEDCHAR,
                    ConfigurationKeys.DEFAULT_CONVERTER_CSV_TO_JSON_ENCLOSEDCHAR).charAt(0));
    List<String> recordSplit;
    try {
        recordSplit = Lists.newArrayList(reader.splitRecord());
    } catch (IOException e) {
        throw new DataConversionException(e);
    }
    JsonObject outputRecord = new JsonObject();

    for (int i = 0; i < outputSchema.size(); i++) {
        if (i < recordSplit.size()) {
            if (recordSplit.get(i) == null) {
                outputRecord.add(outputSchema.get(i).getAsJsonObject().get("columnName").getAsString(),
                        JsonNull.INSTANCE);
            } else if (recordSplit.get(i).isEmpty() || recordSplit.get(i).toLowerCase().equals(NULL)) {
                outputRecord.add(outputSchema.get(i).getAsJsonObject().get("columnName").getAsString(),
                        JsonNull.INSTANCE);
            } else {
                outputRecord.addProperty(outputSchema.get(i).getAsJsonObject().get("columnName").getAsString(),
                        recordSplit.get(i));
            }
        } else {
            outputRecord.add(outputSchema.get(i).getAsJsonObject().get("columnName").getAsString(),
                    JsonNull.INSTANCE);
        }
    }

    return new SingleRecordIterable<>(outputRecord);
}

From source file:gobblin.converter.csv.CsvToJsonConverterV2.java

License:Apache License

@VisibleForTesting
JsonObject createOutput(JsonArray outputSchema, String[] inputRecord) {
    Preconditions.checkArgument(outputSchema.size() == inputRecord.length,
            "# of columns mismatch. Input " + inputRecord.length + " , output: " + outputSchema.size());
    JsonObject outputRecord = new JsonObject();

    for (int i = 0; i < outputSchema.size(); i++) {
        String key = outputSchema.get(i).getAsJsonObject().get(COLUMN_NAME_KEY).getAsString();

        if (StringUtils.isEmpty(inputRecord[i]) || JSON_NULL_VAL.equalsIgnoreCase(inputRecord[i])) {
            outputRecord.add(key, JsonNull.INSTANCE);
        } else {//from   ww w.  ja  va 2  s. c o m
            outputRecord.addProperty(key, inputRecord[i]);
        }
    }

    return outputRecord;
}

From source file:gobblin.converter.csv.CsvToJsonConverterV2.java

License:Apache License

@VisibleForTesting
JsonObject createOutput(JsonArray outputSchema, String[] inputRecord, List<String> customOrder) {

    Preconditions.checkArgument(outputSchema.size() == customOrder.size(),
            "# of columns mismatch. Input " + outputSchema.size() + " , output: " + customOrder.size());
    JsonObject outputRecord = new JsonObject();
    Iterator<JsonElement> outputSchemaIterator = outputSchema.iterator();
    Iterator<String> customOrderIterator = customOrder.iterator();

    while (outputSchemaIterator.hasNext() && customOrderIterator.hasNext()) {
        String key = outputSchemaIterator.next().getAsJsonObject().get(COLUMN_NAME_KEY).getAsString();
        int i = Integer.parseInt(customOrderIterator.next());
        Preconditions.checkArgument(i < inputRecord.length,
                "Index out of bound detected in customer order. Index: " + i + " , # of CSV columns: "
                        + inputRecord.length);
        if (i < 0 || null == inputRecord[i] || JSON_NULL_VAL.equalsIgnoreCase(inputRecord[i])) {
            outputRecord.add(key, JsonNull.INSTANCE);
            continue;
        }//  w w  w .ja  v a  2  s.com
        outputRecord.addProperty(key, inputRecord[i]);
    }

    return outputRecord;
}

From source file:gobblin.converter.json.JsonStringToJsonIntermediateConverter.java

License:Apache License

/**
 * Takes in a record with format String and Uses the inputSchema to convert the record to a JsonObject
 * @return a JsonObject representing the record
 * @throws IOException/*from w w w  .j  a v  a  2 s  . c o m*/
 */
@Override
public Iterable<JsonObject> convertRecord(JsonArray outputSchema, String strInputRecord, WorkUnitState workUnit)
        throws DataConversionException {
    JsonParser jsonParser = new JsonParser();
    JsonObject inputRecord = (JsonObject) jsonParser.parse(strInputRecord);

    if (!this.unpackComplexSchemas) {
        return new SingleRecordIterable<>(inputRecord);
    }

    JsonObject outputRecord = new JsonObject();

    for (int i = 0; i < outputSchema.size(); i++) {
        String expectedColumnName = outputSchema.get(i).getAsJsonObject().get("columnName").getAsString();

        if (inputRecord.has(expectedColumnName)) {
            //As currently gobblin.converter.avro.JsonIntermediateToAvroConverter is not able to handle complex schema's so storing it as string

            if (inputRecord.get(expectedColumnName).isJsonArray()) {
                outputRecord.addProperty(expectedColumnName, inputRecord.get(expectedColumnName).toString());
            } else if (inputRecord.get(expectedColumnName).isJsonObject()) {
                //To check if internally in an JsonObject there is multiple hierarchy
                boolean isMultiHierarchyInsideJsonObject = false;
                for (Map.Entry<String, JsonElement> entry : ((JsonObject) inputRecord.get(expectedColumnName))
                        .entrySet()) {
                    if (entry.getValue().isJsonArray() || entry.getValue().isJsonObject()) {
                        isMultiHierarchyInsideJsonObject = true;
                        break;
                    }
                }
                if (isMultiHierarchyInsideJsonObject) {
                    outputRecord.addProperty(expectedColumnName,
                            inputRecord.get(expectedColumnName).toString());
                } else {
                    outputRecord.add(expectedColumnName, inputRecord.get(expectedColumnName));
                }

            } else {
                outputRecord.add(expectedColumnName, inputRecord.get(expectedColumnName));
            }
        } else {
            outputRecord.add(expectedColumnName, JsonNull.INSTANCE);
        }

    }
    return new SingleRecordIterable<>(outputRecord);
}