0 out of 2 rated this helpful Rate this topic

DataContractJsonSerializer Class

Serializes objects to the JavaScript Object Notation (JSON) and deserializes JSON data to objects. This class cannot be inherited.

System.Object
  System.Runtime.Serialization.XmlObjectSerializer
    System.Runtime.Serialization.Json.DataContractJsonSerializer

Namespace:  System.Runtime.Serialization.Json
Assembly:  System.Runtime.Serialization (in System.Runtime.Serialization.dll)
public sealed class DataContractJsonSerializer : XmlObjectSerializer

The DataContractJsonSerializer type exposes the following members.

  Name Description
Public method DataContractJsonSerializer(Type) Initializes a new instance of the DataContractJsonSerializer class to serialize or deserialize an object of the specified type.
Public method DataContractJsonSerializer(Type, IEnumerable(Of Type)) Initializes a new instance of the DataContractJsonSerializer class to serialize or deserialize an object of the specified type, with a collection of known types that may be present in the object graph.
Public method DataContractJsonSerializer(Type, String) Initializes a new instance of the DataContractJsonSerializer class to serialize or deserialize an object of a specified type using the XML root element specified by a parameter.
Public method DataContractJsonSerializer(Type, XmlDictionaryString) Initializes a new instance of the DataContractJsonSerializer class to serialize or deserialize an object of a specified type using the XML root element specified by a parameter of type XmlDictionaryString.
Public method DataContractJsonSerializer(Type, String, IEnumerable(Of Type)) Initializes a new instance of the DataContractJsonSerializer class to serialize or deserialize an object of a specified type using the XML root element specified by a parameter, with a collection of known types that may be present in the object graph.
Public method DataContractJsonSerializer(Type, XmlDictionaryString, IEnumerable(Of Type)) Initializes a new instance of the DataContractJsonSerializer class to serialize or deserialize an object of a specified type using the XML root element specified by a parameter of type XmlDictionaryString, with a collection of known types that may be present in the object graph.
Public method DataContractJsonSerializer(Type, IEnumerable(Of Type), Int32, Boolean, IDataContractSurrogate, Boolean) Initializes a new instance of the DataContractJsonSerializer class to serialize or deserialize an object of the specified type. This method also specifies a list of known types that may be present in the object graph, the maximum number of graph items to serialize or deserialize, whether to ignore unexpected data or emit type information, and a surrogate for custom serialization.
Public method DataContractJsonSerializer(Type, String, IEnumerable(Of Type), Int32, Boolean, IDataContractSurrogate, Boolean) Initializes a new instance of the DataContractJsonSerializer class to serialize or deserialize an object of the specified type. This method also specifies the root name of the XML element, a list of known types that may be present in the object graph, the maximum number of graph items to serialize or deserialize, whether to ignore unexpected data or emit type information, and a surrogate for custom serialization.
Public method DataContractJsonSerializer(Type, XmlDictionaryString, IEnumerable(Of Type), Int32, Boolean, IDataContractSurrogate, Boolean) Initializes a new instance of the DataContractJsonSerializer class to serialize or deserialize an object of the specified type. This method also specifies the root name of the XML element, a list of known types that may be present in the object graph, the maximum number of graph items to serialize or deserialize, whether to ignore unexpected data or emit type information, and a surrogate for custom serialization.
Top
  Name Description
Public property DataContractSurrogate Gets a surrogate type that is currently active for a given IDataContractSurrogate instance. Surrogates can extend the serialization or deserialization process.
Public property IgnoreExtensionDataObject Gets a value that specifies whether unknown data is ignored on deserialization and whether the IExtensibleDataObject interface is ignored on serialization.
Public property KnownTypes Gets a collection of types that may be present in the object graph serialized using this instance of the DataContractJsonSerializer.
Public property MaxItemsInObjectGraph Gets the maximum number of items in an object graph that the serializer serializes or deserializes in one read or write call.
Top
  Name Description
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method IsStartObject(XmlDictionaryReader) Gets a value that specifies whether the XmlDictionaryReader is positioned over an XML element that represents an object the serializer can deserialize from. (Overrides XmlObjectSerializer.IsStartObject(XmlDictionaryReader).)
Public method IsStartObject(XmlReader) Determines whether the XmlReader is positioned on an object that can be deserialized. (Overrides XmlObjectSerializer.IsStartObject(XmlReader).)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ReadObject(Stream) Reads a document stream in the JSON (JavaScript Object Notation) format and returns the deserialized object. (Overrides XmlObjectSerializer.ReadObject(Stream).)
Public method ReadObject(XmlDictionaryReader) Reads the XML document mapped from JSON (JavaScript Object Notation) with an XmlDictionaryReader and returns the deserialized object. (Overrides XmlObjectSerializer.ReadObject(XmlDictionaryReader).)
Public method ReadObject(XmlReader) Reads the XML document mapped from JSON (JavaScript Object Notation) with an XmlReader and returns the deserialized object. (Overrides XmlObjectSerializer.ReadObject(XmlReader).)
Public method ReadObject(XmlDictionaryReader, Boolean) Reads the XML document mapped from JSON with an XmlDictionaryReader and returns the deserialized object; it also enables you to specify whether the serializer should verify that it is positioned on an appropriate element before attempting to deserialize. (Overrides XmlObjectSerializer.ReadObject(XmlDictionaryReader, Boolean).)
Public method ReadObject(XmlReader, Boolean) Reads an XML document mapped from JSON with an XmlReader and returns the deserialized object; it also enables you to specify whether the serializer should verify that it is positioned on an appropriate element before attempting to deserialize. (Overrides XmlObjectSerializer.ReadObject(XmlReader, Boolean).)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Public method WriteEndObject(XmlDictionaryWriter) Writes the closing XML element to an XML document, using an XmlDictionaryWriter, which can be mapped to JavaScript Object Notation (JSON). (Overrides XmlObjectSerializer.WriteEndObject(XmlDictionaryWriter).)
Public method WriteEndObject(XmlWriter) Writes the closing XML element to an XML document, using an XmlWriter, which can be mapped to JavaScript Object Notation (JSON). (Overrides XmlObjectSerializer.WriteEndObject(XmlWriter).)
Public method WriteObject(Stream, Object) Serializes a specified object to JavaScript Object Notation (JSON) data and writes the resulting JSON to a stream. (Overrides XmlObjectSerializer.WriteObject(Stream, Object).)
Public method WriteObject(XmlDictionaryWriter, Object) Serializes an object to XML that may be mapped to JavaScript Object Notation (JSON). Writes all the object data, including the starting XML element, content, and closing element, with an XmlDictionaryWriter. (Overrides XmlObjectSerializer.WriteObject(XmlDictionaryWriter, Object).)
Public method WriteObject(XmlWriter, Object) Serializes an object to XML that may be mapped to JavaScript Object Notation (JSON). Writes all the object data, including the starting XML element, content, and closing element, with an XmlWriter. (Overrides XmlObjectSerializer.WriteObject(XmlWriter, Object).)
Public method WriteObjectContent(XmlDictionaryWriter, Object) Writes the XML content that can be mapped to JavaScript Object Notation (JSON) using an XmlDictionaryWriter. (Overrides XmlObjectSerializer.WriteObjectContent(XmlDictionaryWriter, Object).)
Public method WriteObjectContent(XmlWriter, Object) Writes the XML content that can be mapped to JavaScript Object Notation (JSON) using an XmlWriter. (Overrides XmlObjectSerializer.WriteObjectContent(XmlWriter, Object).)
Public method WriteStartObject(XmlDictionaryWriter, Object) Writes the opening XML element for serializing an object to XML that can be mapped to JavaScript Object Notation (JSON) using an XmlDictionaryWriter. (Overrides XmlObjectSerializer.WriteStartObject(XmlDictionaryWriter, Object).)
Public method WriteStartObject(XmlWriter, Object) Writes the opening XML element for serializing an object to XML that can be mapped to JavaScript Object Notation (JSON) using an XmlWriter. (Overrides XmlObjectSerializer.WriteStartObject(XmlWriter, Object).)
Top

Use the DataContractJsonSerializer class to serialize instances of a type into a JSON document and to deserialize a JSON document into an instance of a type. For example, you can create a type named Person with properties that contain essential data, such as a name and address. You can then create and manipulate an instance of the Person class and write all of its property values in a JSON document for later retrieval. This JSON document can later be deserialized into the Person class or another class with an equivalent data contract.

If an error occurs during the 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.

.NET Framework

Supported in: 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(2000 characters remaining)
Community Content Add
Annotations FAQ
Referenced Assembly is wrong for Silverlight 5
You have to reference System.ServiceModel.Web to get access to DataContractJsonSerializer
Referenced Assembly is wrong in Silverlight 5
You have to reference System.ServiceModel.Web to get access to the DataContractJsonSerializer class.
Character Encoding Used by DataContractJSONSerializer
This class serializes JSON in UTF8 encoding (confirmed by examining the IL).
Handy info for JSON serialization in Silverlight
Base Class Mismatch
In Silverlight 4, the class inherits from System.Object (as documented on the Silverlight version of this page http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer(v=VS.95).aspx). In .Net Framework 4, the class inherits from XmlObjectSerializer as documented.
Sample code
Example here: How to: Serialize and Deserialize JSON Data http://msdn.microsoft.com/en-us/library/bb412179.aspx
Silverlight users
For Silverlight (4) users you'll find this in System.ServiceModel.Web.dll.
Assembly mismatch
Regarding the problem mentioned earlier, i want to say this article is right: this class resides in System.Runtime.Serialization dll for the .NET Framework 4. The problem lies in the assembly version: although the class is contained in the version 4.0.0.0 assembly, you won't find it in earlier versions. This class was migrated from System.ServiceModel.Web to System.Runtime.Serialization in .NET 4.

Thus, if you are using .NET 3.5 SP1 or earlier, you should look for this class in System.ServiceModel.Web dll.
The assembly mentioned in the documentation is not correct
No, the page is correct. It's System.Runtime.Serialization.dll.
The assembly mentioned in the documentation is not correct
The assembly mentioned in the documentation is not correct. It is not  System.Runtime.Serialization.dll but System.ServiceModel.Web. surprisingly.$0