`

FastJson 测试用例

    博客分类:
  • java
 
阅读更多
package jms.test;



import java.util.ArrayList;

import java.util.Date;

import java.util.List;

import java.util.Map;

import jms.model.Dept;

import jms.model.Employee;

import jms.model.Student;

import org.junit.Test;

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONObject;

import com.alibaba.fastjson.TypeReference;

import com.alibaba.fastjson.serializer.SerializeConfig;

import com.alibaba.fastjson.serializer.SerializerFeature;

import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;

import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;



public class TestFastjson {



//fastjson序列化单个对象 与反序列化

@Test

public void test1() {

Employee e = new Employee("001", "张三", 23, new Date());



//序列化

String jsonStr = JSON.toJSONString(e);

System.out.println(jsonStr);



//反序列化

Employee emp = JSON.parseObject(jsonStr, Employee.class);

System.out.println(emp.getName());

}



//fastjson序列化list集合 与反序列化

@Test

public void test2() {

Employee e = new Employee("001", "张三", 23, new Date());

Employee e2 = new Employee("002", "李四", 29, new Date());



List<Employee> emps = new ArrayList<Employee>();

emps.add(e);

emps.add(e2);



//fastjson序列化list, 返回来的是一个json数组,由[]包含两个json

String jsonArryStr = JSON.toJSONString(emps);

System.out.println(jsonArryStr);



// //反序列化

//法一

// List<Employee> empList = JSON.parseObject(jsonArryStr, new TypeReference<List<Employee>>(){} );

//法二

List<Employee> empList = JSON.parseArray(jsonArryStr,Employee.class);

for (Employee employee : empList) {

System.out.println(employee.getName());

System.out.println(employee.getBirthDay());

}





}



//fastjson序列化复杂对象 与反序列化

@Test

public void test3() {

Employee e = new Employee("001", "张三", 23, new Date());

Employee e2 = new Employee("002", "李四", 29, new Date());



List<Employee> emps = new ArrayList<Employee>();

emps.add(e);

emps.add(e2);



Dept dept = new Dept("d001", "研发部", emps);



//序列化

String jsonStr = JSON.toJSONString(dept);

System.out.println(jsonStr);



//反序列化

Dept d = JSON.parseObject(jsonStr, Dept.class);

System.out.println(d.getName());



//json转map

//法一

Map<String, Object> map1 = JSON.parseObject(jsonStr);//返回JSONObject,JSONObject实现Map<String, Object>接口

//法二

// Map<String, Object> map1 = (Map<String, Object>)JSON.parse(jsonStr);

for (String key : map1.keySet()) {

System.out.println(key + ":" + map1.get(key));

}

}



//fastjson 的 JSONObject的使用

@Test

public void test4() {

Employee e = new Employee("001", "张三", 23, new Date());



//序列化

String jsonStr = JSON.toJSONString(e);

System.out.println(jsonStr);



//反序列化 (可以和test1比较) 

JSONObject emp = JSON.parseObject(jsonStr, JSONObject.class);

System.out.println(emp);

System.out.println(emp.getString("name"));



//再放一个Employee不存在的字段

emp.put("salary", "8000");

System.out.println(emp.toJSONString());

System.out.println(emp.get("salary"));



}



//fastjson序列化字符串

@Test

public void test5(){



List<String> strs = new ArrayList<String>();

strs.add("hello");

strs.add("world");

strs.add("banana");



//序列化

String jsonStr = JSON.toJSONString(strs);

System.out.println(jsonStr);



//反序列化

List<String> strList = JSON.parseObject(jsonStr, new TypeReference<List<String>>(){} );

// List<String> strList = JSON.parseArray(jsonStr, String.class);//等同于上一句

for (String str : strList) {

System.out.println(str);

}

}



//fastjson过滤字段

@Test

public void test6() {



Employee e = new Employee("001", "张三", 23, new Date());

Employee e2 = new Employee("002", "李四", 29, new Date());



List<Employee> emps = new ArrayList<Employee>();

emps.add(e);

emps.add(e2);



//构造过滤器

SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Employee.class, "id", "age");

String jsonStr =JSON.toJSONString(emps, filter);



System.out.println(jsonStr);

}





//fastjson 日期处理

@Test

public void test7(){



Date date = new Date();



String dateStr = JSON.toJSONString(date);

System.out.println(dateStr);



String dateStr2 = JSON.toJSONStringWithDateFormat(date, "yyyy-MM-dd HH:mm:ss");

System.out.println(dateStr2);



//序列化实体

Employee emp = new Employee("001", "张三", 23, new Date());



//法一

String empStr = JSON.toJSONStringWithDateFormat(emp, "yyyy-MM-dd HH:mm:ss");

System.out.println(empStr);



//法二

String empStr2 = JSON.toJSONString(emp, SerializerFeature.WriteDateUseDateFormat);

System.out.println(empStr2);



//法三

SerializeConfig config = new SerializeConfig();

config.put(Date.class, new SimpleDateFormatSerializer("yyyy年MM月dd日 HH时mm分ss秒"));

String empStr3 = JSON.toJSONString(emp, config);

System.out.println(empStr3);

}



//fastjson 去掉值的双引号 实现JSONAware接口

@Test

public void test8(){

//见同级目录的Function.java

}





//fastjson 注解形式  (别名命名, 过滤字段, 日期格式)

@Test

public void test9(){

Student stu = new Student("001", "张三", 23, new Date());

String jsonStr = JSON.toJSONString(stu);

System.out.println(jsonStr);



}

}

转载:http://changxianbest.iteye.com/blog/2181891
分享到:
评论

相关推荐

    Fastjson扫描测试工具.rar

    在渗透测试中遇到json数据一般都会测试下有没有反序列化,然而JSON库有Fastjson,JackJson,Gson等等,那么怎么判断后端不是Fastjson呢?可以构造特定的payload来进行探测分析

    fastjson使用案例源码

    这是本人写方便复习,该资源并没有什么用处

    Gson,jackson,fastjson性能测试

    主要针对常用的json工具Gson,jackson,fastjson进行性能测试,一个完整的工程,包含测试类、测试结论,以及三个工具包的jar文件。可以直接在eclipse中运行。具体版本如下: fastjson-1.1.28.jar gson-1.7.1.jar ...

    测试fastjson与protobuf

    fastjson与protobuf,自己测试用代码

    fastJsonTest

    采用开源项目fastjson做的小测试,fastjson确实不错,怪不得2012年被评为最受欢迎开源项目之一

    fastjson的jar包,包含api文档

    Fastjson是一个Java语言编写的高性能功能完善的JSON库。 高性能: fastjson采用独创的算法,将parse的速度提升到极致,超过所有...fastjson有超过1500个testcase,每次构建都会跑一遍,丰富的测试场景保证了功能稳定。

    fastjson-1.2.54-API文档-中文版.zip

    赠送jar包:fastjson-1.2.54.jar; 赠送原API文档:fastjson-1.2.54-javadoc.jar; 赠送源代码:fastjson-1.2.54-sources.jar; 赠送Maven依赖信息文件:fastjson-1.2.54.pom; 包含翻译后的API文档:fastjson-...

    fastjson-1.2.83.jar下载

    fastjson-1.2.83.jar下载,fastjson是阿里巴巴的开源JSON解析库,可以解析JSON格式的字符串,支持将Java Bean序列化为JSON字符串,也支持从JSON字符串反序列化到JavaBean。fastjson采用全新的JSON解析算法,运行速度极快...

    fastjson-1.2.83 针对近期阿里fastjson包漏洞,升级解决

    Fastjson 1.2.80 及之前版本使用黑白名单用于防御反序列化漏洞,经研究该防御策略在特定条件下可绕过默认 autoType 关闭限制,攻击远程服务器,风险影响较大。建议 Fastjson 用户尽快采取安全措施保障系统安全

    fastjson-1.2.72-API文档-中文版.zip

    赠送jar包:fastjson-1.2.72.jar; 赠送原API文档:fastjson-1.2.72-javadoc.jar; 赠送源代码:fastjson-1.2.72-sources.jar; 赠送Maven依赖信息文件:fastjson-1.2.72.pom; 包含翻译后的API文档:fastjson-1.2....

    fastjson 各个 版本 jar

    fastjson-1.1.35.jar ,fastjson-1.1.36.jar ,fastjson-1.1.37.jar ,fastjson-1.1.44.jar ,fastjson-1.2.3.jar,fastjson-1.2.4.jar

    springboot2.0整合fastjson以及各种使用实例

    springboot2.0整合fastjson实例配置,在测试类中有各种fastjson的使用,序列化反序列化,以及jsonObject与jsonarray的使用,具体测试方法在测试类中

    fastjson-1.2.78-API文档-中文版.zip

    赠送jar包:fastjson-1.2.78.jar; 赠送原API文档:fastjson-1.2.78-javadoc.jar; 赠送源代码:fastjson-1.2.78-sources.jar; 赠送Maven依赖信息文件:fastjson-1.2.78.pom; 包含翻译后的API文档:fastjson-1.2....

    Alibaba Fastjson Jar包

    Alibaba Fastjson Jar包官方版是可以帮助使用Fastjson功能的Fastjson jar包,Fastjson是一个Java语言编写的高性能功能完善的JSON库,当然必备Alibaba Fastjson Jar包官方版。

    Android FastJSON小示例

    Android Studio FastJSON小示例,对应博文地址: http://blog.csdn.net/djstavaV/article/details/48785801

    最新fastJSON C#的JSON开发包 v2.1.18

    fastJSON 版本 v2.1.18

    fastjson的一个小例子

    fastjson 的一个小例子,里面是我博客中的代码, http://blog.csdn.net/yunxiaoxiaoyun/article/details/16811973

    fastjson-1.1.46.android.jar

    阿里巴巴 JSON解析jar包 fastjson,测试表明,fastjson具有极快的性能,超越任其他的Java Json parser 更新时间 2015年10月 27日

    springboot 使用spring cache缓存 和 使用fastjson配置redis系列化

    springboot 使用spring cache缓存 和 使用fastjson配置redis系列化,springboot 使用spring cache缓存 和 使用fastjson配置redis系列化,springboot 使用spring cache缓存 和 使用fastjson配置redis系列化,springboot ...

    fastjson,方便json转换

    实现json转换,fastjson json json转换,可以帮助开发中涉及json格式处理的程序猿。

Global site tag (gtag.js) - Google Analytics