io.odpf.depot.utils.JsonUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of depot Show documentation
Show all versions of depot Show documentation
A sink connector library containing multiple sink implementations
package io.odpf.depot.utils;
import io.odpf.depot.config.OdpfSinkConfig;
import org.json.JSONObject;
public class JsonUtils {
/**
* Creates a json Object based on the configuration.
* If String mode is enabled, it converts all the fields in string.
*
* @param config Sink Configuration
* @param payload Json Payload in byyes
* @return Json object
*/
public static JSONObject getJsonObject(OdpfSinkConfig config, byte[] payload) {
JSONObject jsonObject = new JSONObject(new String(payload));
if (!config.getSinkConnectorSchemaJsonParserStringModeEnabled()) {
return jsonObject;
}
// convert to all objects to string
JSONObject jsonWithStringValues = new JSONObject();
jsonObject.keySet()
.forEach(k -> {
Object value = jsonObject.get(k);
if (value instanceof JSONObject) {
throw new UnsupportedOperationException("nested json structure not supported yet");
}
if (JSONObject.NULL.equals(value)) {
return;
}
jsonWithStringValues.put(k, value.toString());
});
return jsonWithStringValues;
}
}