Implementing java json serialization framework from zero manually

Keywords: Java JSON Maven Junit

json

json Is a serialized json framework implemented by java.

Characteristic

  • Eight basic types of support
  • Basic Type/Object Array/Collection/Enumeration/Object Support
  • Minimalist API

Subsequent characteristics

  • @ Support for Field annotations
  • Integrity support for deserialized objects currently supports only basic deserialization.

Why create

  • Solving the shortcomings in fastJson

FastJSON has some limitations in serialization itself. When there is a set in an object, or an object in a set, the result is unsatisfactory.

  • Lightweight and efficient

json tools such as FastJson have rich features, but they are too compatible and too considerate.

It is hoped that by strictly controlling serialization and deserialization, the code will become more efficient and lightweight, and more suitable for its own use scenarios.

  • Subsequent expansion

Subsequent serialization related, will use this tool to achieve, facilitate self-expanding learning.

Change log

Change log

Quick start

Environmental dependence

JDK 1.7+

Maven 3.X

maven dependence

<dependency>
    <groupId>com.github.houbb</groupId>
    <artifactId>json</artifactId>
    <version>Latest edition</version>
</dependency>

Basic example

import JsonBs;
import org.junit.Assert;
import org.junit.Test;

/**
 * null test
 * @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 case

For the following demonstration of all test code, see

test module

Strings and char test cases

Character string

 @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));
}

char type

@Test
public void escapeTest() {
    char c = '\'';
    final String json = "\"'\"";

    Assert.assertEquals(json, JsonBs.serialize(c));
    assert c == JsonBs.deserialize(json, char.class);
}

Support for arrays

Character string

@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));
}

Basic type testing

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)));
}

Basic Object Type Testing

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)));
}

aggregate

String List Test

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));
}

String Map Test

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()));
}

object

Demonstration of Basic Objects

  • User.java
public class User {

    private String name;

    private int age;

    private double score;

    private char sex;

    private Date birthday;

    //Getter & Setter
    //ToString()
}
  • Test code
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());
}

Expanding reading

00 - What is json

01-Module Introduction

Posted by IronCannibal on Wed, 11 Sep 2019 23:09:48 -0700