. Net 3.5: JSON serialization using the datacontractjsonserializer

Source: Internet
Author: User

In ASP. net Ajax extensions V1.0 for ASP. NET 2.0 there is the javascriptserializer class that provides JSON serialization and deserialization functionality. however, in.. Net 3.5 The specified criptserializer has been marked obsolete. the new object to use for JSON serialization in. network 3.5 is the datacontractjsonserliaizer object. i'm still new to the datacontractjsonserializer, but here's a summary of what I 've learned so far...

Object to serialize

Here's a simple person object with first name and last name properties.

Public class person
{
Public Person (){}
Public Person (string firstname, string lastname)
{
This. firstname = firstname;
This. lastname = lastname;
}
Public String firstname {Get; set ;}
Public String lastname {Get; set ;}
}

Now in order to serialize our object to JSON using the datacontractjsonserializer we must either mark it with the serializable attribute or the datacontract attribute. if we mark the class with the datacontract attribute, then we must mark each property we want serialized with the datamember attribute.

/// Marked with the serializable attribute
[Serializable]
Public class person
{
Public Person (){}
Public Person (string firstname, string lastname)
{
This. firstname = firstname;
This. lastname = lastname;
}
Public String firstname {Get; set ;}
Public String lastname {Get; set ;}

}

/// Marked with the datacontact attribute
[Datacontract]
Public class person
{
Public Person (){}
Public Person (string firstname, string lastname)
{
This. firstname = firstname;
This. lastname = lastname;
}

[Datamember]
Public String firstname {Get; set ;}

[Datamember]
Public String lastname {Get; set ;}
}

[/Code]

Serialization code

Jere's the most basic code to serialize our object to JSON:

Person myperson = new person ("Chris", "pietschmann ");

/// Serialize to JSON
System. runtime. serialization. JSON. datacontractjsonserializer serializer = new system. runtime. serialization. JSON. datacontractjsonserializer (myperson. GetType ());
Memorystream MS = new memorystream ();
Serializer. writeobject (MS, myperson );
String JSON = encoding. Default. getstring (Ms. toarray ());

Our resulting JSON looks like this:

/// Result of person class marked as serializable
{"<Firstname> K _ backingfield": "Chris", "<lastname> K _ backingfield": "pietschmann "}

/// Result of person class marked as datacontract
/// Each property marked as datamember

{"Firstname": "Chris", "lastname": "pietschmann "}

As you can see the first serialization with the class marked with the serializable attribute isn' t quite what we were expecting, but is still JSON. this serialization actually isn' t compatible with the client-side JSON serializer in ASP. net Ajax.

As you can see the second serialization with the class marked with the datacontract attribute is exactly what we were expecting, and is the same JSON that the old character criptserializer object wowould have generated. this is the method of JSON serialization using the datacontractjsonserializer that you'll need to do if you are going to pass the resulting JSON down to the client to be consumed with ASP. net Ajax.

Deserialization code

Here's the most basic code to deserialize our object from JSON:

Person myperson = new person ();
Memorystream MS = new memorystream (encoding. Unicode. getbytes (JSON ));
System. runtime. serialization. JSON. datacontractjsonserializer serializer = new system. runtime. serialization. JSON. datacontractjsonserializer (myperson. GetType ());
Myperson = serializer. readobject (MS) as person;
Ms. Close ();

Controlling the property names in the resulting JSON

When using the datacontract and datamember attributes, you can tell the datamember attribute the specific name you want that property to have within the JSON serialization by setting its "name" named parameter.

Here's an example that will give the name of "first" to the "firstname" property within the JSON serialization:

[Datamember (name = "first")]
Public String firstname {Get; set ;}

The resulting JSON looks like this:

{"First": "Chris", "lastname": "pietschmann "}

Here's the code for some helper Methods Using Generics to do all the dirty work for you

Using system. runtime. serialization;
Using system. runtime. serialization. JSON;

Public class jsonhelper
{
Public static string serialize <t> (t obj)
{
System. runtime. serialization. JSON. datacontractjsonserializer serializer = new system. runtime. serialization. JSON. datacontractjsonserializer (obj. GetType ());
Memorystream MS = new memorystream ();
Serializer. writeobject (MS, OBJ );
String retval = encoding. Default. getstring (Ms. toarray ());
Return retval;
}

Public static t deserialize <t> (string JSON)
{
T OBJ = activator. createinstance <t> ();
Memorystream MS = new memorystream (encoding. Unicode. getbytes (JSON ));
System. runtime. serialization. JSON. datacontractjsonserializer serializer = new system. runtime. serialization. JSON. datacontractjsonserializer (obj. GetType ());
OBJ = (t) serializer. readobject (MS );
Ms. Close ();
Return OBJ;
}
}

/// Our person object to serialize/deserialize to JSON
[Datacontract]
Public class person
{
Public Person (){}
Public Person (string firstname, string lastname)
{
This. firstname = firstname;
This. lastname = lastname;
}

[Datamember]
Public String firstname {Get; set ;}

[Datamember]
Public String lastname {Get; set ;}
}

/// Sample code using the above helper Methods
/// To serialize and deserialize the person object

Person myperson = new person ("Chris", "pietschmann ");

// Serialize
String JSON = jsonhelper. serialize <person> (myperson );

// Deserialize
Myperson = jsonhelper. deserialize <person> (JSON );

What assembly references does my application need for this?

From the namespace that contains datacontractjsonserializer you can probably tell that you need to add a reference toSystem. runtime. serializationAssembly. However, you also need to add a reference toSystem. servicemodel. WebAssembly.

 

From: http://pietschsoft.com/post/2008/02/NET-35-JSON-Serialization-using-the-DataContractJsonSerializer.aspx

 

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.