JAVA实现json和对象间的相互转换

一、新建一个项目,导入这两个jar包,下载见附件:
jackson-core-asl-1.9.12.jar
jackson-mapper-asl-1.9.12.jar

二、新建一个工具类

JsonMapper.java
Java代码
package com.ebiz.logic.service.impl;

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;

import com.ebiz.framework.util.date.DateUtil;

/**
* 简单封装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这种简单类型时,先使用函數constructParametricType构造类型.
* @see #constructParametricType(Class, Class...)
*/
public T fromJson(String jsonString, Class 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这种简单类型时,先使用函數constructParametricType构造类型.
* @see #constructParametricType(Class, Class...)
*/
@SuppressWarnings("unchecked")
public 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 fromJson(String jsonString, Class parametrized, Class... parameterClasses) {
return (T) this.fromJson(jsonString, constructParametricType(parametrized, parameterClasses));
}

@SuppressWarnings("unchecked")
public List fromJsonToList(String jsonString, Class classMeta){
return (List) this.fromJson(jsonString,constructParametricType(List.class, classMeta));
}

@SuppressWarnings("unchecked")
public 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 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 pathAtRoot(String json, String path, Class clazz){
JsonNode rootNode = parseNode(json);
JsonNode node = rootNode.path(path);
return (T) fromJson(node, clazz);
}

/**
* 構造泛型的Type如List, 则调用constructParametricType(ArrayList.class,MyBean.class)
* Map则调用(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 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(DateUtil.yyyy_MM_dd_HH_mm_ss);
return jsonMapper.toJson(object);
}

}
三、再提供一个异常类

NestedException.java
Java代码
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();
}

}
新建两个实体

Student.java
Java代码
package com.ebiz.uniservice.fightdata.test;

import java.util.List;

public class Student {

private String name;
private int age;
private List 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 teachers) {
this.teachers = teachers;
}

public List getTeachers() {
return teachers;
}

}

Teacher.java
Java代码
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;
}
}

好了,来测试一把

JsonTest.java
Java代码
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 teachers = new ArrayList();
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}]}
转换用时:224ms
json转换成对象:com.ebiz.uniservice.fightdata.test.Student@55e55f
转换用时:25ms

你可能感兴趣的:(JAVA,json,jsonp,java)