Using Jsonobject to serialize and deserialize the depth of serialization

Source: Internet
Author: User

Jsonobject and Jsonarray are the most commonly used in Json-lib.jar two classes, respectively, can be serialized and deserialized objects and arrays (collections), the structure of clear life, easy to use, powerful, high efficiency, use has been more respected, Although there are still many features have not yet been fully understood, let's learn to remember, to make induction, precipitation.

First look at two classes:

Student class:

public class Student {private String name;private string gerder;private string Address;public string GetName () {return Nam e;} public void SetName (String name) {this.name = name;} Public String Getgerder () {return gerder;} public void Setgerder (String gerder) {this.gerder = Gerder;} Public String getaddress () {return address;} public void setaddress (String address) {this.address = address;}}

Classes class:

public class Classes {private string Clsnum;private string Claname;private string way;private list<student> Student S;private Student student;public Student getstudent () {return Student;} public void Setstudent (Student Student) {this.student = Student;} Public String Getclsnum () {return clsnum;} public void Setclsnum (String clsnum) {this.clsnum = Clsnum;} Public String Getclaname () {return claname;} public void Setclaname (String claname) {this.claname = Claname;} Public String Getway () {return to;} public void Setway (String) {this.way = by;} Public list<student> getstudents () {return students;} public void Setstudents (list<student> students) {this.students = students;}}

It is worth noting that the classes class contains both the Student object and the List<student> collection, so that I can have more depth in the next operation:

Test class: MyTest

public class MyTest {public static void main (string[] args) {Student Student = new Student (); Student.setname ("ZXL"); Studen T.setgerder ("M"); Student.setaddress ("Beijing"); Classes cs = new Classes (), Cs.setclaname ("Computer 1"), Cs.setclsnum ("07060341"); Cs.setway ("Wentaoyuan1"); list<student> list = new arraylist<student> (); List.add (Student); cs.setstudents (list); Cs.setstudent ( student);}}
Our CS object is now encapsulated and then serialized:

SYSTEM.OUT.PRINTLN (Jsonobject.fromobject (CS). ToString ());

Result: {"claname": "Computer 1", "Clsnum": "07060341", "student": {"Address": "Beijing", "Gerder": "M", "name": "ZXL"}, "Students": [{"Address": "Beijing", "Gerder": "M", "name": "ZXL"}], "I": "Wentaoyuan1"}
To see that serialization is not a problem with depth, all fields including nested (so called) objects are also well-known as "thorough serialization."

Next, look at the deserialization:

Jsonobject Jo = Jsonobject.fromobject ("{' clsnum ': ' 123 ', ' claname ': ' Computer 1 ', ' student ': {' address ': ' Guangzhou ', ' Gerder ') : ' F ', ' name ': ' ZXL '}, ' students ': [{' Address ': ' Shanghai ', ' gerder ': ' M ', ' name ': ' ZXL '}], ' The ' ' to ': ' Wentaoyuan1 '} '; Classes cls = (Classes) Jsonobject.tobean (Jo, Classes.class); System.out.println (Cls.getclsnum () + "= =" + cls.getstudent (). getaddress ());

Results: 123==guangzhou

There's no problem.

Note: When there are more nested objects, look at writing a class map to indicate the various classpath of deserialization, such as:

map<string, class<?>> clazz = new hashmap<string, class<?>> (); Clazz.put ("Student", Student.class); Clazz.put ("Students", Student.class);
You can set clazz by using the overloaded method of Tobean or by Jsonconfig Setclassmap (Clazz);

If I have such a requirement, on the basis of the object CS I want to encapsulate Jo's data to it, this time will use another overloaded method of Tobean:

map<string, class<?>> clazz = new hashmap<string, class<?>> (); Clazz.put ("Student", Student.class); Clazz.put ("Students", Student.class); Jsonobject Jo = Jsonobject.fromobject ("{' clsnum ': ' 123 ', ' claname ': ' Computer 1 ', ' student ': {' address ': ' Guangzhou ', ' Gerder ') : ' F ', ' name ': ' ZXL '}, ' students ': [{' Address ': ' Shanghai ', ' gerder ': ' M ', ' name ': ' ZXL '}], ' The ' ' to ': ' Wentaoyuan1 '} '; Jsonconfig JC = new Jsonconfig (); Jc.setclassmap (Clazz); Classes cls = (Classes) Jsonobject.tobean (Jo, CS, JC);

When there is a problem, what happens when the attribute Clsnum in Jo does not exist?

The experiment proves that when a property does not exist, the value of the property is not overwritten, which is equal to the original value of CS.

When the student attribute in Jo, address does not exist, will it be the case above?


Jsonobject Jo = Jsonobject.fromobject ("{' clsnum ': ' 123 ', ' claname ': ' Computer 1 ', ' student ': {' gerder ': ' F ', ' name ': ' ZXL '}, ' Students ': [{' Address ': ' Shanghai ', ' gerder ': ' M ', ' name ': ' ZXL '}], ' The ' ' to ': ' Wentaoyuan1 '} '; Jsonconfig JC = new Jsonconfig (); Jc.setclassmap (Clazz); Classes cls = (Classes) Jsonobject.tobean (Jo, CS, JC); System.out.println (Cls.getclsnum () + "= =" + cls.getstudent (). getaddress ());
Printing results:

123==null

Conclusion: If a property is not present in a deep object, it will be overwritten.

Can you make it like a primary attribute without overwriting? I think it should be possible, the problem of course to landed on the jsonconfig, there are many settings, most of which I have not read, so I do not know how to do? But if this feature is designed by me, I will definitely set such a switch, which overrides the deep-seated properties by default, and of course can be adjusted back.

Please all netizens to give enlighten, how to solve this problem?

Other articles about Json-lib will be presented in the next blog post.

Using Jsonobject to serialize and deserialize the depth of serialization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.