Another serialization Method

Source: Internet
Author: User

Original article: http://james.newtonking.com/archive/2009/12/26/json-net-3-5-release-6-binary-json-bson-support.aspx

Introduction:

Entity serialization is often found in daily work, so we are always looking for an efficient serialization method. Previously viewedJSON. NET and ProtoBuf.Recently I looked at MongiDB and found that they store BSON, a binary Json. I will translate an article here to give you an introduction, and then write an article to describe the efficiency comparison of various serialization methods.

Json. NET 3.5 Release 6 and later support for BSON, which is: bytes.

Bson supports some additional types, such as binary data to be converted to Json.

Bson is significantly better than Json in terms of the serialization speed and serialization speed. It directly stores binary data to avoid the additional overhead of base64 encoding for Json data and faster reading/writing Bson than Json.

Code Product p = new Product ();
P. ExpiryDate = DateTime. Parse ("2009-04-05T14: 45: 00Z ");
P. Name = "Carlos 'Spicy Wieners ";
P. Price = 9.95 m;
P. Sizes = new [] {"Small", "Medium", "Large "};
 
MemoryStream MS = new MemoryStream ();
JsonSerializer serializer = new JsonSerializer ();
 
// Serialize product to BSON
BsonWriter writer = new BsonWriter (MS );
Serializer. Serialize (writer, p );
 
Console. WriteLine (BitConverter. ToString (ms. ToArray ()));
// 7C-00-00-00-02-4E-61-6D-65-00-16-00-00-00-43-61-72-6C-
// 6F-73-27-20-53-70-69-63-79-20-57-69-65-6E-65-72-73-00-
// 09-45-78-70-69-72-79-44-61-74-65-00-E0-51-BD-76-20-01-
// 00-00-01-50-72-69-63-65-00-66-66-66-66-66-E6-23-40-04-
// 53-69-7A-65-73-00-2D-00-00-00-02-30-00-06-00-00-00-53-
// 6D-61-6C-6C-00-02-31-00-07-00-00-00-4D-65-64-69-75-6D-
// 00-02-32-00-06-00-00-00-4C-61-72-67-65-00-00-00
 
 
Ms. Seek (0, SeekOrigin. Begin );
 
// Deserialize product from BSON
BsonReader reader = new BsonReader (MS );
Product deserializedProduct = serializer. Deserialize <Product> (reader );
 
Console. WriteLine (deserializedProduct. Name );
// Carlos 'Spicy Wieners

 

Json.net no longer depends on the Entity Framework, Link to SQL, or third-party assembly. At the same time, it can be run in. net4.0. For the operation method, refer to "worker.

Json. Net currently automatically serializes and deserializes DataSet and DataTable.

Code DataSet ds = new DataSet ();
Ds. Tables. Add (CreateDataTable ("FirstTable", 2 ));
Ds. Tables. Add (CreateDataTable ("SecondTable", 1 ));
 
String json = JsonConvert. SerializeObject (ds, Formatting. Indented, new IsoDateTimeConverter ());
//{
// "FirstTable ":[
//{
// "StringCol": "Item Name ",
// "Int32Col": 1,
// "BooleanCol": true,
// "TimeSpanCol": "10.22: 10: 15.1000000 ",
// "DateTimeCol": "2000-12-29T00: 00: 00Z ",
// "DecimalCol": 64.0021
//},
//{
// "StringCol": "Item Name ",
// "Int32Col": 2,
// "BooleanCol": true,
// "TimeSpanCol": "10.22: 10: 15.1000000 ",
// "DateTimeCol": "2000-12-29T00: 00: 00Z ",
// "DecimalCol": 64.0021
//}
//],
// "SecondTable ":[
//{
// "StringCol": "Item Name ",
// "Int32Col": 1,
// "BooleanCol": true,
// "TimeSpanCol": "10.22: 10: 15.1000000 ",
// "DateTimeCol": "2000-12-29T00: 00: 00Z ",
// "DecimalCol": 64.0021
//}
//]
//}
 
DataSet deserializedDs = JsonConvert. DeserializeObject <DataSet> (json, new IsoDateTimeConverter ());

 

SelectToken provides a statement similar to Xpath to search for data.

String name = (string) o. SelectToken ("Manufacturers [0]. Name ");

 

Code JObject o = JObject. Parse (@"{
"" Stores "":[
"" Lambton Quay "",
"" Willis Street ""
],
"" Manufacturers "":[
{
"" Name "": "Acme Co "",
"" Products "":[
{
"" Name "": "" andevil "",
"" Price ": 50
}
]
},
{
"" Name "": "Contoso "",
"" Products "":[
{
"" Name ":" "Elbow Grease "",
"" Price ": 99.95
},
{
"" Name ":" "Headlight Fluid "",
"" Price ": 4
}
]
}
]
}");
 
String name = (string) o. SelectToken ("Manufacturers [0]. Name ");
// Acme Co
 
Decimal productPrice = (decimal) o. SelectToken ("Manufacturers [0]. Products [0]. Price ");
// 50
 
String productName = (string) o. SelectToken ("Manufacturers [1]. Products [0]. Name ");
// Elbow Grease

 

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.