// Path: Server/src/main/java/com/easychat/exception/BadRequestException.java
// public class BadRequestException extends BasicException{
//     public BadRequestException(String error, String description) {
//         super(error, description);
//     }
// }
// 
// Path: Server/src/main/java/com/easychat/model/error/ErrorType.java
// public final class ErrorType {
//     public static final String NO_ERROR = "no_error";
//     public static final String INVALID_GRANT = "invalid_grant";
//     public static final String SERVICE_RESOURCE_NOT_FOUND = "service_resource_not_found";
//     public static final String DUPLICATE_UNIQUE_PROPERTY_EXISTS="duplicate_unique_property_exists";
//     public static final String ILLEGAL_ARGUMENT="illegal_argument";
//     public static final String AUTH_BAD_ACCESS_TOKEN="auth_bad_access_token";
// }


import com.easychat.exception.BadRequestException;
import com.easychat.model.error.ErrorType;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
package com.easychat.utils;



/**
 * Created by yonah on 15-11-13.
 */
public class JsonUtils {
    /**
     * Logger for this class
     */
    private static final Logger logger = LoggerFactory.getLogger(JsonUtils.class);

    private final static ObjectMapper objectMapper = new ObjectMapper();

    static {
        objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
    }

    private JsonUtils() {
    }

    public static String encode(Object obj) {
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (JsonGenerationException e) {
            logger.error("encode(Object)", e); //$NON-NLS-1$
        } catch (JsonMappingException e) {
            logger.error("encode(Object)", e); //$NON-NLS-1$
        } catch (IOException e) {
            logger.error("encode(Object)", e); //$NON-NLS-1$
        }
        return null;
    }

    /**
     * 将json string反序列化成对象
     *
     * @param json
     * @param valueType
     * @return
     */
// Path: Server/src/main/java/com/easychat/exception/BadRequestException.java
// public class BadRequestException extends BasicException{
//     public BadRequestException(String error, String description) {
//         super(error, description);
//     }
// }
// 
// Path: Server/src/main/java/com/easychat/model/error/ErrorType.java
// public final class ErrorType {
//     public static final String NO_ERROR = "no_error";
//     public static final String INVALID_GRANT = "invalid_grant";
//     public static final String SERVICE_RESOURCE_NOT_FOUND = "service_resource_not_found";
//     public static final String DUPLICATE_UNIQUE_PROPERTY_EXISTS="duplicate_unique_property_exists";
//     public static final String ILLEGAL_ARGUMENT="illegal_argument";
//     public static final String AUTH_BAD_ACCESS_TOKEN="auth_bad_access_token";
// }


// Path: Server/src/main/java/com/easychat/utils/JsonUtils.java
import com.easychat.exception.BadRequestException;
import com.easychat.model.error.ErrorType;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;

package com.easychat.utils;



/**
 * Created by yonah on 15-11-13.
 */
public class JsonUtils {
    /**
     * Logger for this class
     */
    private static final Logger logger = LoggerFactory.getLogger(JsonUtils.class);

    private final static ObjectMapper objectMapper = new ObjectMapper();

    static {
        objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
    }

    private JsonUtils() {
    }

    public static String encode(Object obj) {
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (JsonGenerationException e) {
            logger.error("encode(Object)", e); //$NON-NLS-1$
        } catch (JsonMappingException e) {
            logger.error("encode(Object)", e); //$NON-NLS-1$
        } catch (IOException e) {
            logger.error("encode(Object)", e); //$NON-NLS-1$
        }
        return null;
    }

    /**
     * 将json string反序列化成对象
     *
     * @param json
     * @param valueType
     * @return
     */
    public static <T> T decode(String json, Class<T> valueType) throws BadRequestException {