Share via


How to: Serialize and Deserialize JSON Data

JSON (JavaScript Object Notation) is an efficient data encoding format that enables fast exchanges of small amounts of data between client browsers and AJAX-enabled Web services.

This topic demonstrates how to serialize .NET type objects into JSON-encoded data and then deserialize data in the JSON format back into instances of .NET types using the DataContractJsonSerializer. This example uses a data contract to demonstrate serialization and deserialization of a user-defined Person type.

Normally, JSON serialization and deserialization is handled automatically by Windows Communication Foundation (WCF) when you use data contract types in service operations that are exposed over AJAX-enabled endpoints. However, in some cases you may need to work with JSON data directly - this is the scenario that this topic demonstrates.

Bb412179.note(en-us,VS.85).gifNote:
If an error occurs during serialization of an outgoing reply on the server or the reply operation throws an exception for some other reason, it may not get returned to the client as a fault.

This topic is based on the JSON Serialization sample.

To define the data contract for a Person

  1. Define the data contract for Person by attaching the DataContractAttribute to the class and DataMemberAttribute attribute to the members you want to serialize. For more information about data contracts, see Designing Service Contracts.

    [DataContract]
        internal class Person
        {
            [DataMember]
            internal string name;
    
            [DataMember]
            internal int age;
        }
    

To serialize an instance of type Person to JSON

  1. Create an instance of the Person type.

    Person p = new Person();
    p.name = "John";
    p.age = 42;
    
  2. Serialize the Person object to a memory stream using the DataContractJsonSerializer.

    MemoryStream stream1 = new MemoryStream();
    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Person));
    
  3. Use the WriteObject method to write JSON data to the stream.

    ser.WriteObject(stream1, p);
    
  4. Show the JSON output.

    stream1.Position = 0;
    StreamReader sr = new StreamReader(stream1);
    Console.Write("JSON form of Person object: ");
    Console.WriteLine(sr.ReadToEnd());
    

To deserialize an instance of type Person from JSON

  1. Deserialize the JSON-encoded data into a new instance of Person by using the ReadObject method of the DataContractJsonSerializer.

    stream1.Position = 0;
    Person p2 = (Person)ser.ReadObject(stream1);
    
  2. Show the results.

    Console.Write("Deserialized back, got name=");
    Console.Write(p2.name);
    Console.Write(", age=");
    Console.WriteLine(p2.age);
    

Example

Bb412179.note(en-us,VS.85).gifNote:
The JSON serializer throws a serialization exception for data contracts that have multiple members with the same name, as shown in the following sample code.

[DataContract]
public class TestDuplicateDataBase
{
    [DataMember]
    public int field1 = 123;
}
[DataContract]
public class TestDuplicateDataDerived : TestDuplicateDataBase
{
    [DataMember]
    public new int field1 = 999;
}

See Also

Concepts

Stand-Alone JSON Serialization
Support for JSON and Other Data Transfer Formats


© 2007 Microsoft Corporation. All rights reserved.
Build Date: 2009-08-07