fastjson针对map的简单测试
2017-02-11 11:13
309 查看
能够很好的支持复杂对象序列化和反序列化的json工具有阿里巴巴的fastjson和Google的gson,但是两个还不一样。gson参见:http://blog.csdn.net/lk_blog/article/details/7685210
下面是我对fastjson针对map类型的简单测试结果:
package com.yjl.javabase.json;
import com.alibaba.fastjson.JSON;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class FastJsonTest {
public static void main(String[] args) {
Map<String, Student> stringStudentMap = new HashMap<>();
Student s1 = new Student(11, "aaa", "bbb", 111.111, new Date());
Student s2 = new Student(22, "cccc", "ddddd", 222.222, new Date());
stringStudentMap.put("one", s1);
stringStudentMap.put("tow", s2);
System.out.println(JSON.toJSONString(stringStudentMap));
System.out.println();
Map<Student, String> studentStringMap = new HashMap<>();
studentStringMap.put(s1, "three");
studentStringMap.put(s2, "four");
System.out.println(JSON.toJSONString(studentStringMap));
}
}
class Student {
private int age;
private String firstName;
private String lastName;
private double heigth;
private Date birth;
public Student(int age, String firstName, String lastName, double heigth, Date birth) {
this.age = age;
this.firstName = firstName;
this.lastName = lastName;
this.heigth = heigth;
this.birth = birth;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public double getHeigth() {
return heigth;
}
public void setHeigth(double heigth) {
this.heigth = heigth;
}
}
结果如下:
{"one":{"age":11,"birth":1486782339687,"firstName":"aaa","heigth":111.111,"lastName":"bbb"},"tow":{"age":22,"birth":1486782339687,"firstName":"cccc","heigth":222.222,"lastName":"ddddd"}}
{{"age":11,"birth":1486782339687,"firstName":"aaa","heigth":111.111,"lastName":"bbb"}:"three",{"age":22,"birth":1486782339687,"firstName":"cccc","heigth":222.222,"lastName":"ddddd"}:"four"}
当map的key是实体类的时候出来的结果不是标准的json,而gson使用数组表示的。
下面是我对fastjson针对map类型的简单测试结果:
package com.yjl.javabase.json;
import com.alibaba.fastjson.JSON;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class FastJsonTest {
public static void main(String[] args) {
Map<String, Student> stringStudentMap = new HashMap<>();
Student s1 = new Student(11, "aaa", "bbb", 111.111, new Date());
Student s2 = new Student(22, "cccc", "ddddd", 222.222, new Date());
stringStudentMap.put("one", s1);
stringStudentMap.put("tow", s2);
System.out.println(JSON.toJSONString(stringStudentMap));
System.out.println();
Map<Student, String> studentStringMap = new HashMap<>();
studentStringMap.put(s1, "three");
studentStringMap.put(s2, "four");
System.out.println(JSON.toJSONString(studentStringMap));
}
}
class Student {
private int age;
private String firstName;
private String lastName;
private double heigth;
private Date birth;
public Student(int age, String firstName, String lastName, double heigth, Date birth) {
this.age = age;
this.firstName = firstName;
this.lastName = lastName;
this.heigth = heigth;
this.birth = birth;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public double getHeigth() {
return heigth;
}
public void setHeigth(double heigth) {
this.heigth = heigth;
}
}
结果如下:
{"one":{"age":11,"birth":1486782339687,"firstName":"aaa","heigth":111.111,"lastName":"bbb"},"tow":{"age":22,"birth":1486782339687,"firstName":"cccc","heigth":222.222,"lastName":"ddddd"}}
{{"age":11,"birth":1486782339687,"firstName":"aaa","heigth":111.111,"lastName":"bbb"}:"three",{"age":22,"birth":1486782339687,"firstName":"cccc","heigth":222.222,"lastName":"ddddd"}:"four"}
当map的key是实体类的时候出来的结果不是标准的json,而gson使用数组表示的。
相关文章推荐
- 关于map、hash_map 和 unordered_map 的简单性能测试
- 03-22 简单测试了一下map和列表解析的速度对比
- 多种Map简单使用和测试
- 针对B/S、C/S架构的180个简单测试案例
- c++中map容器简单功能实现测试附带结构体指针测试
- 针对B/S、C/S架构的180个简单测试案例
- ACE 中 ACE_Mem_Map 类的简单简单测试
- 针对B/S、C/S架构的180个简单测试案例
- 针对B/S、C/S架构的180个简单测试案例
- 第四简单的4-mybatis,sql传参parameterType为包装类和map类型的测试
- 简单的innerHtml测试
- 【虫师--系列】JMeter基础之一 一个简单的性能测试
- 搭建LAMP架构— 测试:搭建一个简单的Discuz论坛
- 黑马程序员——关于Set,List,Map集合的简单总结
- Spring Security教程(2)----SpringSecurity简单测试
- map的遍历方式(使用Junit测试)
- map容器简单应用
- linux下expect环境安装以及简单脚本测试
- ProtoBuf 简单测试
- tensorflow学习笔记四:mnist实例--用简单的神经网络来训练和测试