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

io.castled.utils.JsonUtils Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package io.castled.utils;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.google.common.collect.Maps;
import io.castled.exceptions.CastledRuntimeException;

import javax.json.JsonNumber;
import javax.json.JsonObject;
import javax.json.JsonString;
import javax.json.JsonValue;
import java.io.IOException;
import java.util.Map;

public class JsonUtils {

    public static final ObjectMapper objectMapper;

    static {
        objectMapper = new ObjectMapper()
                .setSerializationInclusion(JsonInclude.Include.NON_NULL)
                .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
                .setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
                .registerModule(new JavaTimeModule());
    }

    public static  T byteArrayToObject(byte[] value, Class clazz) {
        try {
            return objectMapper.readValue(value, clazz);
        } catch (IOException e) {
            throw new CastledRuntimeException(e);
        }
    }

    public static  T jsonStringToTypeReference(String jsonString, TypeReference typeReference) {
        try {
            return objectMapper.readValue(jsonString, typeReference);
        } catch (JsonProcessingException e) {
            throw new CastledRuntimeException(e);
        }
    }

    public static Map jsonStringToMap(String jsonString) {
        try {
            return objectMapper.readValue(jsonString, new TypeReference>() {
            });
        } catch (JsonProcessingException e) {
            throw new CastledRuntimeException(e);
        }
    }

    public static Map jsonObjectToMap(JsonObject jsonObject) {
        Map objectMap = Maps.newHashMap();
        for (String jsonKey : jsonObject.keySet()) {
            JsonValue jsonValue = jsonObject.get(jsonKey);
            objectMap.put(jsonKey, processJsonValue(jsonValue));
        }
        return objectMap;
    }

    private static Object processJsonValue(JsonValue jsonValue) {
        switch (jsonValue.getValueType()) {
            case OBJECT:
                return jsonObjectToMap((JsonObject) jsonValue);
            case STRING:
                return ((JsonString) jsonValue).getString();
            case TRUE:
                return true;
            case FALSE:
                return false;
            case NUMBER:
                if (((JsonNumber) jsonValue).isIntegral()) {
                    return ((JsonNumber) jsonValue).longValue();
                }
            default:
                throw new CastledRuntimeException("Invalid json value type: " + jsonValue.getValueType());
        }

    }

    public static  T jsonStringToObject(String jsonString, Class clazz) {
        try {
            return objectMapper.readValue(jsonString, clazz);
        } catch (IOException e) {
            throw new CastledRuntimeException(e);
        }
    }


    public static byte[] objectToByteArray(Object object) {
        try {
            return objectMapper.writeValueAsBytes(object);
        } catch (JsonProcessingException e) {
            throw new CastledRuntimeException(e.getMessage());
        }
    }

    public static JsonNode jsonParserToJsonNode(JsonParser jsonParser) {
        try {
            return objectMapper.readTree(jsonParser);
        } catch (IOException e) {
            throw new CastledRuntimeException(e.getMessage());
        }
    }

    public static  T jsonParserToObject(JsonParser jsonParser, Class clazz) {
        try {
            JsonNode jsonNode = objectMapper.readTree(jsonParser);
            return jsonNodeToObject(jsonNode, clazz);
        } catch (IOException e) {
            throw new CastledRuntimeException(e.getMessage());
        }
    }

    public static  T jsonNodeToObject(JsonNode jsonNode, Class clazz) {
        try {
            return objectMapper.treeToValue(jsonNode, clazz);
        } catch (JsonProcessingException e) {
            throw new CastledRuntimeException(e.getMessage());
        }
    }

    public static JsonNode jsonStringToJsonNode(String jsonString) {
        try {
            return objectMapper.readTree(jsonString);
        } catch (JsonProcessingException e) {
            throw new CastledRuntimeException(e.getMessage());
        }

    }

    public static Map jsonNodeToMap(JsonNode jsonNode) {
        return objectMapper.convertValue(jsonNode, new TypeReference>() {
        });

    }

    public static String objectToString(Object object) {
        try {
            return objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            throw new CastledRuntimeException(e.getMessage());
        }
    }

    public static Map objectToMap(Object object) {
        return objectMapper.convertValue(object, new TypeReference>() {
        });
    }

    public static  T mapToObject(Map map, Class clazz) {
        return objectMapper.convertValue(map, clazz);

    }

    public static JsonNode mapToJsonNode(Map map) {
        return objectMapper.valueToTree(map);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy