十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
json 是 java 实现的序列化 json 框架。
我们提供的服务有:成都网站设计、做网站、微信公众号开发、网站优化、网站认证、贞丰ssl等。为上千余家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的贞丰网站制作公司
8 大基本类型支持
基本类型/对象数组/集合/枚举/对象 支持
@Field
注解的支持
FastJSON 在序列化本身存在一定限制。当对象中有集合,集合中还是对象时,结果不尽如人意。
FastJson 等 json 工具特性丰富,但是考虑过多,也兼容过多。
希望通过严格控制序列化和反序列,从而使得代码变得高效轻量,更加适合自己的使用场景,
后续序列化相关,将使用这个工具实现,方便自行拓展学习。
变更日志
JDK 1.7+
Maven 3.X
com.github.houbb
json
最新版本
import JsonBs;
import org.junit.Assert;
import org.junit.Test;
/**
* null 测试
* @author binbin.hou
* @since 0.0.1
*/
public class NullTest {
@Test
public void nullTest() {
final String string = null;
Assert.assertEquals("null", JsonBs.serialize(string));
final String result = JsonBs.deserialize("null", String.class);
Assert.assertNull(result);
}
}
以下演示所有测试代码见
test 模块
@Test
public void commonTest() {
final String string = "123";
Assert.assertEquals("\"123\"", JsonBs.serialize(string));
final String json = "\"123\"";
Assert.assertEquals(string, JsonBs.deserialize(json, String.class));
}
@Test
public void escapeTest() {
final String string = "\"123";
Assert.assertEquals("\"\"123\"", JsonBs.serialize(string));
final String json = "\"123\"";
Assert.assertEquals("123", JsonBs.deserialize(json, String.class));
}
@Test
public void escapeTest() {
char c = '\'';
final String json = "\"'\"";
Assert.assertEquals(json, JsonBs.serialize(c));
assert c == JsonBs.deserialize(json, char.class);
}
@Test
public void stringEmptyTest() {
String[] strings = new String[]{};
String json = "[]";
Assert.assertEquals(json, JsonBs.serialize(strings));
Assert.assertEquals(strings, JsonBs.deserialize(json, String[].class));
}
@Test
public void stringTest() {
String[] strings = new String[]{"a", "b", "c"};
final String json = "[\"a\",\"b\",\"c\"]";
Assert.assertEquals(json, JsonBs.serialize(strings));
Assert.assertEquals(strings, JsonBs.deserialize(json, String[].class));
}
public void intTest() {
int[] ints = new int[]{1,2,3};
final String intJson = "[1,2,3]";
Assert.assertEquals(intJson, JsonBs.serialize(ints));
//[1, 2, 3]
System.out.println(Arrays.toString(JsonBs.deserialize(intJson, int[].class)));
}
public void integerTest() {
Integer[] ints = new Integer[]{1,2,3};
final String json = "[1,2,3]";
Assert.assertEquals(json, JsonBs.serialize(ints));
//[1, 2, 3]
System.out.println(Arrays.toString(JsonBs.deserialize(json, Integer[].class)));
}
public void stringTest() {
List strings = new ArrayList<>();
strings.add("10");
strings.add("20");
strings.add("30");
Class clazz = strings.getClass();
final String json = "[\"10\",\"20\",\"30\"]";
Assert.assertEquals(json, JsonBs.serialize(strings));
Assert.assertEquals(strings, JsonBs.deserialize(json, clazz));
}
public void stringTest() {
Map map = new HashMap<>();
map.put("123", "456");
final String json = "{\"123\":\"456\"}";
Assert.assertEquals(json, JsonBs.serialize(map));
Assert.assertEquals(map, JsonBs.deserialize(json, map.getClass()));
}
public class User {
private String name;
private int age;
private double score;
private char sex;
private Date birthday;
//Getter & Setter
//ToString()
}
public void userTest() {
User user = new User();
user.age(10).name("wiki").birthday(new Date(1568196960491L)).score(123.d).sex('g');
final String json = "{\"name\":\"wiki\",\"age\":10,\"score\":123.0,\"sex\":\"g\",\"birthday\":1568196960491}";
Assert.assertEquals(json, JsonBs.serialize(user));
User user2 = JsonBs.deserialize(json, User.class);
Assert.assertEquals(user.toString(), user2.toString());
}
00-什么是 json
01-模块介绍