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

com.askfast.util.JSONUtil Maven / Gradle / Ivy

There is a newer version: 1.5.7
Show newest version
package com.askfast.util;

import java.util.Collection;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JSONUtil
{
//    private static final Logger log = Logger.getLogger( JSONUtil.class.getName() );

    public static  T deserialize( String jsonString, Class DeserializeClass )
    throws Exception
    {
        T deserializedEntity = null;
        if ( jsonString != null && !jsonString.isEmpty() )
        {
            deserializedEntity = new ObjectMapper().readValue( jsonString, DeserializeClass );
        }
        return deserializedEntity;
    }

    public static String serialize( Object objectToBeSerialized ) throws Exception
    {
        ObjectMapper oMapper = new ObjectMapper();
        oMapper.setSerializationInclusion( Include.NON_NULL );
        String result = null;
        if ( oMapper.canSerialize( objectToBeSerialized.getClass() ) )
        {
            result = oMapper.writeValueAsString( objectToBeSerialized );
        }
        return result;
    }
    
    public static String toCDLString(Collection list) throws Exception {
        return serialize(list).replace( "[", "" ).replace("]", "").replace( "\"", "" );
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy