All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.odpf.depot.utils.JsonUtils Maven / Gradle / Ivy

There is a newer version: 0.3.8
Show newest version
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;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy