Use ServiceStack. Text to serialize the json implementation code

Source: Internet
Author: User
Tags tojson

I believe that. net developers will often encounter such a need for json serialization. Today I will post an article to summarize my own use of ServiceStack. Text to serialize json. It is much faster than Newtonsoft. Json and found to be faster than fastJson during testing.

First, we have the following two classes: one is the Staff class and the other is the Contact class:

Copy codeThe Code is as follows: public class Staff
{
Public long ID {get; set ;}
Public string Name {get; set ;}
Public int Age {get; set ;}
}

Public class Contact
{
Public long StaffID {get; set ;}
Public string Email {get; set ;}
}

First, we add two employees:

Copy codeThe Code is as follows: List <Staff> listStaff = new List <Staff> ();
ListStaff. Add (new Staff () {ID = 2, Name = "Xiao Li "});
ListStaff. Add (new Staff () {ID = 3, Name = "John "});

Json serialization by many people in the past:

Copy codeThe Code is as follows: var result = "[";
Foreach (var staff in listStaff)
{
Result + = "{\" ID \ ": \" "+ staff. ID + "\", \ "Name \": \ "" + staff. name + "\"},";
}
Result = result. Substring (0, result. Length-1 );
Result + = "]";

The final json is as follows:Copy codeThe Code is as follows :[
{
"ID": "2 ",
"Name": "Xiao Li"
},
{
"ID": "3 ",
"Name": "Xiao Wang"
}
]

This method can be used, but there are several disadvantages: 1. The code is messy and error-prone. 2. Escape special characters, such as double quotation marks. Otherwise, json serialization will fail. Next let's see how to use ServiceStack. Text to serialize json.

We need to download ServiceStack. Text. dll, reference it to our project, and reference ServiceStack. Text namespace. Next let's take a look at the serialization of a single class object:

Copy codeThe Code is as follows: Staff staff = new Staff () {ID = 1, Name = "xiaozhao "};
Var result = staff. ToJson ();

The json format is as follows:

Copy codeThe Code is as follows:

{
"ID": 1,
"Name": "xiaozhao ",
"Age": 0
}

Careful friends will find that we do not intend to use the Age attribute in the output, but here we output the Age. To solve this problem, we have the following methods.

1. Use the JsonObject class, which inherits from Dictionary <string, string>, so that we can output the desired attributes as shown in the following figure.

Copy codeThe Code is as follows: Staff staff = new Staff () {ID = 1, Name = "xiaozhao "};

JsonObject json = new JsonObject ();
Json. Add ("ID", staff. ID. ToString ());
Json. Add ("Name", staff. Name );

Var result = json. ToJson ();

In this way, we can get the following result [result 1], which achieves our goal.Copy codeThe Code is as follows :{
"ID": 1,
"Name": "xiaozhao"
}

2. Add System. Runtime. Serialization to the project and reference the namespace System. Runtime. Serialization.

We can specify the property to be serialized in the attribute, as shown below:

Copy codeThe Code is as follows: [DataContract]
Public class Staff
{
[DataMember]
Public long ID {get; set ;}
[DataMember]
Public string Name {get; set ;}
Public int Age {get; set ;}
}

Or:

Copy codeThe Code is as follows: public class Staff
{
Public long ID {get; set ;}
Public string Name {get; set ;}
[IgnoreDataMember]
Public int Age {get; set ;}
}

In this way, the output result of the following code becomes the same as [result 1.

Copy codeThe Code is as follows: Staff staff = new Staff () {ID = 1, Name = "xiaozhao "};
Var result = staff. ToJson ();

Finally, let's take a look at the serialization of the class object set. The contact information for adding employees is as follows:

Copy codeThe Code is as follows: List <Contact> listContact = new List <Contact> ();
ListContact. Add (new Contact () {StaffID = 3, Email = "xiaowang@163.com "});
ListContact. Add (new Contact () {StaffID = 4, Email = "xiaoli@163.com "});

Each employee may correspond to a contact method. The following code is provided if some employees do not have contact information:

Copy codeThe Code is as follows: List <string> list = new List <string> ();
Foreach (var staff in listStaff)
{
JsonObject json = new JsonObject ();
Json. Add ("ID", staff. ID. ToString ());
Json. Add ("Name", staff. Name );

// Contact information
Var contact = listContact. FirstOrDefault (m => m. StaffID = staff. ID );
If (contact! = Null)
{
JsonObject jsonContact = new JsonObject ();
JsonContact. Add ("Email", contact. Email );
// Note that the serialized json string of the Contact object is added to the json object.
Json. Add ("Contact", contact. ToJson ());
}
// Serialize the json object and add it to the list
List. Add (json. ToJson ());
}
// Obtain the final json string
Var result = string. Format ("[{0}]", string. Join (",", list ));

Json:

Copy codeThe Code is as follows :[
{
"ID": 2,
"Name": "Xiao Li"
},
{
"ID": 3,
"Name": "Xiao Wang ",
"Contact ":{
"StaffID": 3,
"Email": "xiaowang@163.com"
}
}
]

In deserialization, use FromJson:

Var staff = result. FromJson <List <Staff> ();
This article briefly introduces how to use ServiceStack. Text to serialize json. It is helpful to friends who have not used it.

Author: Dong Kui

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.