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

io.soffa.commons.support.JsonUtil Maven / Gradle / Ivy

package io.soffa.commons.support;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.deser.std.DateDeserializers;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.type.ArrayType;
import com.fasterxml.jackson.databind.type.MapLikeType;
import io.soffa.commons.logging.Logger;
import lombok.SneakyThrows;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.*;

public class JsonUtil {

    private static final Logger logger = Logger.create(JsonUtil.class);

    private static final ObjectMapper mapper = new ObjectMapper()
        .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
        .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

    static {
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addDeserializer(Date.class, new DateDeserializers.DateDeserializer() {
            @Override
            public Date deserialize(JsonParser parser, DeserializationContext ctxt) throws IOException, JsonProcessingException {
                try {
                    return super.deserialize(parser, ctxt);
                } catch (InvalidFormatException e) {
                    if (parser.hasToken(JsonToken.VALUE_STRING)) {
                        String string = parser.getText().trim();
                        return DateUtil.parse(string);
                    }
                    throw e;
                }
            }
        });
        mapper.registerModule(simpleModule);
    }

    private JsonUtil() {
    }

    public static boolean isJson(String input) {
        try {
            new JSONObject(input);
            return true;
        } catch (JSONException e) {
            return false;
        }
    }

    public static String fromXml(String xmlInput) {
        if (xmlInput == null) return null;
        return XML.toJSONObject(xmlInput).toString();
    }

    @SneakyThrows
    public static  T fromXml(String xmlInput, String root, Class kind) {
        if (xmlInput == null) {
            return ClassUtil.newInstance(kind);
        }
        JSONObject object = XML.toJSONObject(xmlInput);
        return deserialize(object.getJSONObject(root).toString(), kind);
    }

    @SneakyThrows
    public static  T deserialize(String jsonString, Class type) {
        if (StringUtils.isEmpty(jsonString)) {
            return null;
        }
        if (type == String.class) {
            return type.cast(jsonString);
        }
        return mapper.readValue(jsonString, type);
    }

    @SneakyThrows
    public static  T convert(Object input, Class type) {
        if (input == null) {
            return ClassUtil.newInstance(type);
        }
        if(type.isInstance(input)) {
            return type.cast(input);
        }
        return mapper.convertValue(input, type);
    }

    @SneakyThrows
    public static String serialize(Object src) {
        if (src == null) return null;
        if (src instanceof String) return src.toString();
        return mapper.writeValueAsString(src);
    }

    @SneakyThrows
    public static void serializeToFile(Object content, File file) {
        if (content != null) {
            FileUtils.writeStringToFile(file, JsonUtil.serialize(content), StandardCharsets.UTF_8);
        } else {
            logger.warn("Nothing was written to file because provided content is null.");
        }
    }

    @SneakyThrows
    public static  Map deserializeMap(String input) {
        if (input == null) {
            new HashMap<>();
        }
        MapLikeType mapType = mapper.getTypeFactory().constructMapLikeType(Map.class, String.class, Object.class);
        return mapper.readValue(input, mapType);
    }

    @SneakyThrows
    public static  Map deserializeMap(InputStream input, Class type) {
        if (input == null) {
            return new HashMap<>();
        }
        MapLikeType mapType = mapper.getTypeFactory().constructMapLikeType(Map.class, String.class, type);
        return mapper.readValue(input, mapType);
    }

    @SneakyThrows
    public static  Map deserializeMap(String input, Class type) {
        if (input == null) {
            return new HashMap<>();
        }
        MapLikeType mapType = mapper.getTypeFactory().constructMapLikeType(Map.class, String.class, type);
        return mapper.readValue(input, mapType);
    }

    @SneakyThrows
    public static  List deserializeList(String input, Class type) {
        if (StringUtils.isBlank(input)) {
            return new ArrayList<>();
        }
        ArrayType mapType = mapper.getTypeFactory().constructArrayType(type);
        return Arrays.asList(mapper.readValue(input, mapType));
    }

    @SneakyThrows
    public static  List deserializeList(InputStream input, Class type) {
        if (input == null) {
            return new ArrayList<>();
        }
        ArrayType mapType = mapper.getTypeFactory().constructArrayType(type);
        return Arrays.asList(mapper.readValue(input, mapType));
    }

    @SuppressWarnings("unchecked")
    public static Map toMap(Object input) {
        return toMap(input, Object.class);
    }

    @SuppressWarnings("unchecked")
    public static  Map toMap(Object input, Class valueClass) {
        if (input == null) {
            return new HashMap<>();
        }
        if (input instanceof Map) {
            return (Map) input;
        }
        MapLikeType type = mapper.getTypeFactory().constructMapLikeType(Map.class, String.class, valueClass);
        if (input instanceof String) {
            try {
                return mapper.readValue((String) input, type);
            } catch (IOException e) {
                return new HashMap<>();
            }
        } else {
            return mapper.convertValue(input, type);
        }
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy