Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Manually implement the java json serialization framework from zero

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report