import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.codehaus.jackson.JsonGenerator;
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.type.TypeReference;
public class JacksonTest {
public static ObjectMapper getDefaultObjectMapper() {
ObjectMapper mapper =new ObjectMapper();
//设置将对象转换成JSON字符串时候:包含的属性不能为空或"";
//Include.Include.ALWAYS 默认
//Include.NON_DEFAULT 属性为默认值不序列化
//Include.NON_EMPTY 属性为 空("") 或者为 NULL 都不序列化
//Include.NON_NULL 属性为NULL 不序列化
mapper.setSerializationInclusion(Inclusion.NON_EMPTY);
//设置将MAP转换为JSON时候只转换值不等于NULL的
mapper.configure(SerializationConfig.Feature.WRITE_NULL_MAP_VALUES,false);
mapper.configure(JsonGenerator.Feature.ESCAPE_NON_ASCII,true);
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
//设置有属性不能映射成PO时不报错
mapper.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);
return mapper;
}
public static void main(String[] args) throws Exception{
//准备数据
Name name1 =new Name("zhang","san");
Name name2 =new Name("li","si");
Name name3 =new Name("wang","wu");
Student student1 =new Student(1,name1,"一班",new Date());
Student student2 =new Student(2,name2,"二班",new Date());
Student student3 =new Student(3,name3,"三班",new Date());
List studentList =new ArrayList();
studentList.add(student1);
studentList.add(student2);
studentList.add(student3);
Map studentMap =new HashMap();
studentMap.put("1", student1);
studentMap.put("2", student2);
studentMap.put("3", student3);
Student json2object =null;
List json2list =null;
Map json2map =null;
ObjectMapper mapper = getDefaultObjectMapper();
/* Object --> JSON */
String object4json = mapper.writeValueAsString(student1);
System.out.println("Object ----> JSON");
System.out.println(object4json);
System.out.println("------------------------------------------------------");
/* List --> JSON */
String listforjson = mapper.writeValueAsString(studentList);
System.out.println("List ----> JSON");
System.out.println(listforjson);
System.out.println("------------------------------------------------------");
/* Map ----> JSON */
String map4json = mapper.writeValueAsString(studentMap);
System.out.println("Map ----> JSON");
System.out.println(map4json);
System.out.println("------------------------------------------------------");
/* JSON --> Object */
json2object = mapper.readValue(object4json, Student.class);
System.out.println("JSON ----> Object");
System.out.println(json2object);
System.out.println("------------------------------------------------------");
/* JSON --> List */
json2list = mapper.readValue(listforjson,new TypeReference>() {});
System.out.println("JSON --> List");
System.out.println(json2list.toString());
System.out.println("------------------------------------------------------");
/* JSON --> Map */
json2map = mapper.readValue(map4json,new TypeReference>() {});
System.out.println("JSON --> Map");
System.out.println(json2map.toString());
}
}
网友评论