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;
}
Aggregations