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

ru.greatbit.utils.serialize.json.JsonMarshaller Maven / Gradle / Ivy

package ru.greatbit.utils.serialize.json;

import org.codehaus.jackson.map.AnnotationIntrospector;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.xc.JaxbAnnotationIntrospector;
import org.springframework.stereotype.Service;

/**
 * Created by azee on 4/11/14.
 */
@Service
public class JsonMarshaller {


    /**
     * Marshall an object without root
     * @param object - an object to marshall
     * @param  - object class
     * @return String - marshalled object
     * @throws Exception
     */
    public  String marshal(T object) throws Exception{
        return marshal(object, "");
    }


    /**
     * Marshall an object
     * Object can be wrapped into root element if provided
     * @param object - an object to marshall
     * @param rootName - String root name to wrap
     * @param  - object class
     * @return - String - marshalled object
     * @throws Exception
     */
    public  String marshal(T object, String rootName) throws Exception{
        String result;
        ObjectMapper mapper = new ObjectMapper();
        AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
        mapper.getDeserializationConfig().withAnnotationIntrospector(introspector);
        result = mapper.writeValueAsString(object);
        if (rootName != null && !"".equals(rootName)){
            result = "{\"" + rootName + "\": " + result + "}";
        }
        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy