Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Manually implement the java json serialization framework from zero

Shulou Source: shulou.com Published: 2022-06-03 08:16:27 11月23日 Update

Json

Json is the serialized json framework implemented by java.

Characteristics

8 basic types support

Basic types / object arrays / collections / enumerations / object support

Follow-up features of minimalist API

Support for @ Field annotations

Deserialization object integrity support, currently only supports basic deserialization. Why to create to solve the deficiency in fastJson

FastJSON has some limitations on serialization itself. When there is a collection in the object and the collection is still an object, the result is not satisfactory.

Lightweight and efficient

Json tools such as FastJson are rich in features, but they are too thoughtful and compatible.

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

Follow-up expansion

Follow-up serialization related, will use this tool to facilitate self-expansion of learning.

Change log

Change log

Quickly start environmental dependence

JDK 1.7 +

Maven 3.X

Maven relies on the latest version of com.github.houbb json 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

See the following demo for all test code

Test module

String and char test case string @ Testpublic void commonTest () {final String string = "123"; Assert.assertEquals ("\ "123\", JsonBs.serialize (string)); final String json = "\" 123\ ""; Assert.assertEquals (string, JsonBs.deserialize (json, String.class));} @ Testpublic 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 @ Testpublic void escapeTest () {char c ='\''; final String json = "\"\ ""; Assert.assertEquals (json, JsonBs.serialize (c)); assert c = = JsonBs.deserialize (json, char.class) } string @ Testpublic void stringEmptyTest () {String [] strings = new String [] {}; String json = "[]"; Assert.assertEquals (json, JsonBs.serialize (strings)); Assert.assertEquals (strings, JsonBs.deserialize (json, String [] .class)); @ Testpublic 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 [] {1jue 2jue 3}; final String intJson = "[1jue 2pl 3]"; Assert.assertEquals (intJson, JsonBs.serialize (ints)) / / System.out.println (Arrays.toString (JsonBs.deserialize (intJson, int [] .class));} basic object type test public void integerTest () {Integer [] ints = new Integer [] {1meme 2jue 3}; final String json = "[1JI 2J 3]"; Assert.assertEquals (json, JsonBs.serialize (ints)) / [1,2,3] System.out.println (Arrays.toString (JsonBs.deserialize (json, Integer [] .class));} Collection string list test 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));} string Map test 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 ());} object Base object Demo User.javapublic 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") .score (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 ());} expand reading

00-what is json

01-Module introduction

Tags: Tests objects sequences support characters strings types codes features foundations tools arrays logs cases modules lightweight too many demos frameworks as desired Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Xiaomi OPPO Reno NVidia Redmi