JAVA实现json和对象间的相互转换,支持复杂对象格式

jackson-core-asl-1.9.12.jar

jackson-mapper-asl-1.9.12.jar

package com.psd.test;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
import org.codehaus.jackson.map.util.JSONPObject;
import org.codehaus.jackson.type.JavaType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 简单封装Jackson,实现JSON String<->Java Object的Mapper.
 * 
 * 封装不同的输出风格, 使用不同的builder函数创建实例.
 * 
 */
public class JsonMapper {

  private static Logger logger = LoggerFactory.getLogger(JsonMapper.class);

  private ObjectMapper mapper;

  public JsonMapper(Inclusion inclusion) {
    mapper = new ObjectMapper();
    //设置输出时包含属性的风格
    mapper.setSerializationInclusion(inclusion);
    //设置输入时忽略在JSON字符串中存在但Java对象实际没有的属性
    mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    //禁止使用int代表Enum的order()來反序列化Enum,非常危險
    mapper.configure(DeserializationConfig.Feature.FAIL_ON_NUMBERS_FOR_ENUMS, true);
  }

  /**
   * 创建输出全部属性到Json字符串的Mapper.
   */
  public static JsonMapper buildNormalMapper() {
    return new JsonMapper(Inclusion.ALWAYS);
  }

  /**
   * 创建只输出非空属性到Json字符串的Mapper.
   */
  public static JsonMapper buildNonNullMapper() {
    return new JsonMapper(Inclusion.NON_NULL);
  }

  /**
   * 创建只输出初始值被改变的属性到Json字符串的Mapper.
   */
  public static JsonMapper buildNonDefaultMapper() {
    return new JsonMapper(Inclusion.NON_DEFAULT);
  }

  /**
   * 创建只输出非Null且非Empty(如List.isEmpty)的属性到Json字符串的Mapper.
   */
  public static JsonMapper buildNonEmptyMapper() {
    return new JsonMapper(Inclusion.NON_EMPTY);
  }

  /**
   * 如果对象为Null, 返回"null".
   * 如果集合为空集合, 返回"[]".
   */
  public String toJson(Object object) {

    try {
      return mapper.writeValueAsString(object);
    } catch (IOException e) {
      throw NestedException.wrap(e);
    }
  }

  /**
   * 如果JSON字符串为Null或"null"字符串, 返回Null.
   * 如果JSON字符串为"[]", 返回空集合.
   * 
   * 如需读取集合如List/Map, 且不是List<String>这种简单类型时,先使用函數constructParametricType构造类型.
   * @see #constructParametricType(Class, Class...)
   */
  public <T> T fromJson(String jsonString, Class<T> clazz) {
    if (StringUtils.isEmpty(jsonString)) {
      return null;
    }

    try {
      return mapper.readValue(jsonString, clazz);
    } catch (IOException e) {
      throw NestedException.wrap(e);
    }
  }

  /**
   * 如果JSON字符串为Null或"null"字符串, 返回Null.
   * 如果JSON字符串为"[]", 返回空集合.
   * 
   * 如需读取集合如List/Map, 且不是List<String>这种简单类型时,先使用函數constructParametricType构造类型.
   * @see #constructParametricType(Class, Class...)
   */
  @SuppressWarnings("unchecked")
  public <T> T fromJson(String jsonString, JavaType javaType) {
    if (StringUtils.isEmpty(jsonString)) {
      return null;
    }

    try {
      return (T) mapper.readValue(jsonString, javaType);
    } catch (IOException e) {
      throw NestedException.wrap(e);
    }
  }
  
  @SuppressWarnings("unchecked")
  public <T> T fromJson(String jsonString, Class<?> parametrized, Class<?>... parameterClasses) {
    return (T) this.fromJson(jsonString, constructParametricType(parametrized, parameterClasses));
  }
  
  @SuppressWarnings("unchecked")
  public <T> List<T> fromJsonToList(String jsonString, Class<T> classMeta){
    return (List<T>) this.fromJson(jsonString,constructParametricType(List.class, classMeta));
  }
  
  @SuppressWarnings("unchecked")
  public <T> T fromJson(JsonNode node, Class<?> parametrized, Class<?>... parameterClasses) {
    JavaType javaType = constructParametricType(parametrized, parameterClasses);
    try {
      return (T) mapper.readValue(node, javaType);
    } catch (IOException e) {
      throw NestedException.wrap(e);
    }
  }
  
  @SuppressWarnings("unchecked")
  public <T> T pathAtRoot(String json, String path, Class<?> parametrized, Class<?>... parameterClasses){
    JsonNode rootNode = parseNode(json);
    JsonNode node = rootNode.path(path);
    return (T) fromJson(node, parametrized, parameterClasses);
  }
  
  @SuppressWarnings("unchecked")
  public <T> T pathAtRoot(String json, String path, Class<T> clazz){
    JsonNode rootNode = parseNode(json);
    JsonNode node = rootNode.path(path);
    return (T) fromJson(node, clazz);
  }

  /**
   * 構造泛型的Type如List<MyBean>, 则调用constructParametricType(ArrayList.class,MyBean.class)
   *             Map<String,MyBean>则调用(HashMap.class,String.class, MyBean.class)
   */
  public JavaType constructParametricType(Class<?> parametrized, Class<?>... parameterClasses) {
    return mapper.getTypeFactory().constructParametricType(parametrized, parameterClasses);
  }

  /**
   * 當JSON裡只含有Bean的部分屬性時,更新一個已存在Bean,只覆蓋該部分的屬性.
   */
  @SuppressWarnings("unchecked")
  public <T> T update(T object, String jsonString) {
    try {
      return (T) mapper.readerForUpdating(object).readValue(jsonString);
    } catch (JsonProcessingException e) {
      logger.warn("update json string:" + jsonString + " to object:" + object + " error.", e);
    } catch (IOException e) {
      logger.warn("update json string:" + jsonString + " to object:" + object + " error.", e);
    }
    return null;
  }

  /**
   * 輸出JSONP格式數據.
   */
  public String toJsonP(String functionName, Object object) {
    return toJson(new JSONPObject(functionName, object));
  }

  /**
   * 設定是否使用Enum的toString函數來讀寫Enum,
   * 為False時時使用Enum的name()函數來讀寫Enum, 默認為False.
   * 注意本函數一定要在Mapper創建後, 所有的讀寫動作之前調用.
   */
  public void setEnumUseToString(boolean value) {
    mapper.configure(SerializationConfig.Feature.WRITE_ENUMS_USING_TO_STRING, value);
    mapper.configure(DeserializationConfig.Feature.READ_ENUMS_USING_TO_STRING, value);
  }

  /**
   * 取出Mapper做进一步的设置或使用其他序列化API.
   */
  public ObjectMapper getMapper() {
    return mapper;
  }
  
  public JsonNode parseNode(String json){
    try {
      return mapper.readValue(json, JsonNode.class);
    } catch (IOException e) {
      throw NestedException.wrap(e);
    }
  }
  
  /**
   * 输出全部属性
   * @param object
   * @return
   */
  public static String toNormalJson(Object object){
    return new JsonMapper(Inclusion.ALWAYS).toJson(object);
  }
  
  /**
   * 输出非空属性
   * @param object
   * @return
   */
  public static String toNonNullJson(Object object){
    return new JsonMapper(Inclusion.NON_NULL).toJson(object);
  }
  
  /**
   * 输出初始值被改变部分的属性
   * @param object
   * @return
   */
  public static String toNonDefaultJson(Object object){
    return new JsonMapper(Inclusion.NON_DEFAULT).toJson(object);
  }
  
  /**
   * 输出非Null且非Empty(如List.isEmpty)的属性
   * @param object
   * @return
   */
  public static String toNonEmptyJson(Object object){
    return new JsonMapper(Inclusion.NON_EMPTY).toJson(object);
  }
  
  public void setDateFormat(String dateFormat){
    mapper.setDateFormat(new SimpleDateFormat(dateFormat));
  }
  
  public static String toLogJson(Object object){
    JsonMapper jsonMapper = new JsonMapper(Inclusion.NON_EMPTY);
    jsonMapper.setDateFormat("yyyy-MM-dd HH:mm:ss");
    return jsonMapper.toJson(object);
  }

}

package com.ebiz.logic.service.impl;

public class NestedException extends RuntimeException {

  private static final long serialVersionUID = 5893258079497055346L;
  
  private Throwable throwable;

  private NestedException(Throwable t) {
    this.throwable = t;
  }

  /** Wraps another exeception in a RuntimeException. */
  public static RuntimeException wrap(Throwable t) {
    if (t instanceof RuntimeException)
      return (RuntimeException) t;
    return new NestedException(t);
  }

  public Throwable getCause() {
    return this.throwable;
  }

  public void printStackTrace() {
    this.throwable.printStackTrace();
  }

}


package com.ebiz.uniservice.fightdata.test;

import java.util.List;

public class Student {

  private String name;
  private int age;
  private List<Teacher> teachers;

  public void setName(String name) {
    this.name = name;
  }

  public String getName() {
    return name;
  }

  public void setAge(int age) {
    this.age = age;
  }

  public int getAge() {
    return age;
  }

  public void setTeachers(List<Teacher> teachers) {
    this.teachers = teachers;
  }

  public List<Teacher> getTeachers() {
    return teachers;
  }

}

package com.ebiz.uniservice.fightdata.test;

public class Teacher {

  private String name;
  private int age;

  public Teacher() {
    
  }
  
  public Teacher(String name, int age) {
    super();
    this.name = name;
    this.age = age;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getName() {
    return name;
  }

  public void setAge(int age) {
    this.age = age;
  }

  public int getAge() {
    return age;
  }
}

package com.ebiz.uniservice.fightdata.test;

import java.util.ArrayList;

import com.ebiz.logic.service.impl.JsonMapper;

/**
 * 
 * @author jsqiu
 *
 */
public class JsonTest {
  public static void main(String[] args) {
    Student student = getStudents();
    Long beginTime = System.currentTimeMillis();
    //bean2json
    String json = JsonMapper.toLogJson(student);
    System.out.println("对象转换为json:" + json);
    System.out.println("转换用时:" + (System.currentTimeMillis()-beginTime) + "ms");
    //json2bean,需要注意:Student类和Teacher类必须有一个空的构造方法
    beginTime = System.currentTimeMillis();
    //JsonMapper提供了很多创建Mapper的方法,不是非要用buildNonDefaultMapper,你可以对比几种方法的转换时间,挑个最快的
    Student student2 = JsonMapper.buildNonDefaultMapper().fromJson(json, Student.class);
    System.out.println("json转换成对象:" + student2);
    System.out.println("转换用时:" + (System.currentTimeMillis()-beginTime) + "ms");
  }

  /**
   * 初始化一个student
   * @return
   */
  private static Student getStudents() {
    Teacher t1 = new Teacher("teacher1",35);
    Teacher t2 = new Teacher("teacher1",25);
    Teacher t3 = new Teacher("teacher1",30);
    ArrayList<Teacher> teachers = new ArrayList<Teacher>();
    teachers.add(t1);
    teachers.add(t2);
    teachers.add(t3);
    Student s = new Student();
    s.setAge(20);
    s.setName("student1");
    s.setTeachers(teachers);
    return s;
  }
}


对象转换为json:{"name":"student1","age":20,"teachers":[{"name":"teacher1","age":35},{"name":"teacher1","age":25},{"name":"teacher1","age":30}]}

json转换成对象:com.ebiz.uniservice.fightdata.test.Student@55e55f


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值