Search in sources :

Example 1 with FieldConfig

use of com.nextdoor.bender.deserializer.json.GenericJsonDeserializerConfig.FieldConfig in project bender by Nextdoor.

the class GenericJsonDeserializer method deserialize.

@Override
public DeserializedEvent deserialize(String raw) {
    GenericJsonEvent devent = new GenericJsonEvent(null);
    JsonElement elm;
    try {
        elm = parser.parse(raw);
    } catch (JsonSyntaxException e) {
        throw new DeserializationException(e);
    }
    if (!elm.isJsonObject()) {
        throw new DeserializationException("event is not a json object");
    }
    JsonObject obj = elm.getAsJsonObject();
    /*
     * Convert fields which are nested json strings into json objects
     */
    for (FieldConfig fconfig : this.nestedFieldConfigs) {
        if (obj.has(fconfig.getField())) {
            JsonElement msg = obj.get(fconfig.getField());
            /*
         * Find the JSON in the string and replace the field
         */
            NestedData data = deserialize(msg);
            obj.remove(fconfig.getField());
            obj.add(fconfig.getField(), data.nested);
            /*
         * If the string contained data before the JSON store it in a new field
         */
            if (fconfig.getPrefixField() != null && data.prefix != null) {
                obj.add(fconfig.getPrefixField(), data.prefix);
            }
        }
    }
    if (rootNodeOverridePath != null) {
        obj = JsonPathProvider.read(obj, rootNodeOverridePath);
        if (obj == null) {
            throw new DeserializationException(rootNodeOverridePath + " path not found in object");
        }
    }
    devent.setPayload(obj);
    return devent;
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonElement(com.google.gson.JsonElement) FieldConfig(com.nextdoor.bender.deserializer.json.GenericJsonDeserializerConfig.FieldConfig) JsonObject(com.google.gson.JsonObject) DeserializationException(com.nextdoor.bender.deserializer.DeserializationException)

Aggregations

JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 DeserializationException (com.nextdoor.bender.deserializer.DeserializationException)1 FieldConfig (com.nextdoor.bender.deserializer.json.GenericJsonDeserializerConfig.FieldConfig)1