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

yakworks.json.jackson.JacksonJson.groovy Maven / Gradle / Ivy

There is a newer version: 3.14
Show newest version
/*
* Copyright 2021 original authors
* SPDX-License-Identifier: Apache-2.0
*/
package yakworks.json.jackson;

import java.lang.reflect.Type

import groovy.transform.CompileStatic

import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper

/**
 * Static helpers around Jackson. Many of these are not really needed but serves as what we consider more obvious
 * naming when dealing with Maps. Also serve as reminders or a dingus on how things can be done.
 */
@CompileStatic //JacksonJson.
class JacksonJson {

    static ObjectMapper getObjectMapper() {
        ObjectMapperWrapper.instance.objectMapper
    }

    /** convert object to json string */
    static String toJson(Object object){
        stringify(object)
    }

    /** convert object to json string */
    static String stringify(Object value){
        return objectMapper.writeValueAsString(value)
    }

    /**
     * parse string and expect the class type back.
     * usually would call this with parseJson(text, Map) or parseJson(text, List)
     */
    public static  T parseJson(String text, Class clazz) {
        def parsedObj = objectMapper.readValue(text, clazz)
        return (T)parsedObj
    }

    public static  T fromString(String string, Class clazz) {
        return objectMapper.readValue(string, clazz)
    }

    public static  T fromString(String string, Type type) {
        return objectMapper.readValue(string, objectMapper.getTypeFactory().constructType(type))
    }

    static JsonNode toJsonNode(String value) {
        return objectMapper.readTree(value)
    }

    /** binds the data to new instance of the pased in class */
    public static  T bind(Object data, Class toValueType) throws IllegalArgumentException {
        return objectMapper.convertValue(data, toValueType);
    }

    /** binds the data to new instance of the pased in class */
    public static  T bind(T instance, Object data) {
        return objectMapper.updateValue(instance, data);
    }

    public static  T fromBytes(byte[] value, Class clazz) throws IOException {
        return objectMapper.readValue(value, clazz);
    }

    public static  T fromBytes(byte[] value, Type type) throws IOException {
        return objectMapper.readValue(value, objectMapper.getTypeFactory().constructType(type));

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy