json 是 java 實現的序列化 json 框架。java
8 大基本類型支持git
基本類型/對象數組/集合/枚舉/對象 支持github
極簡的 APIjson
@Field
註解的支持數組
反序列化對象的完整性支持,目前只支持基礎的反序列化。框架
FastJSON 在序列化自己存在必定限制。當對象中有集合,集合中仍是對象時,結果不盡如人意。maven
FastJson 等 json 工具特性豐富,可是考慮過多,也兼容過多。工具
但願經過嚴格控制序列化和反序列,從而使得代碼變得高效輕量,更加適合本身的使用場景,學習
後續序列化相關,將使用這個工具實現,方便自行拓展學習。測試
JDK 1.7+
Maven 3.X
<dependency>
<groupId>com.github.houbb</groupId>
<artifactId>json</artifactId>
<version>最新版本</version>
</dependency>
複製代碼
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
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<String> 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<String, String> 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());
}
複製代碼