Table of Contents

Class JsonConvert

Namespace
Newtonsoft.Json
Assembly
Newtonsoft.Json.dll

Provides methods for converting between common language runtime types and JSON types.

public static class JsonConvert
Inheritance
JsonConvert
Inherited Members

Fields

False

Represents JavaScript's boolean value false as a string. This field is read-only.

public static readonly string False

Field Value

string

NaN

Represents JavaScript's NaN as a string. This field is read-only.

public static readonly string NaN

Field Value

string

NegativeInfinity

Represents JavaScript's negative infinity as a string. This field is read-only.

public static readonly string NegativeInfinity

Field Value

string

Null

Represents JavaScript's null as a string. This field is read-only.

public static readonly string Null

Field Value

string

PositiveInfinity

Represents JavaScript's positive infinity as a string. This field is read-only.

public static readonly string PositiveInfinity

Field Value

string

True

Represents JavaScript's boolean value true as a string. This field is read-only.

public static readonly string True

Field Value

string

Undefined

Represents JavaScript's undefined as a string. This field is read-only.

public static readonly string Undefined

Field Value

string

Methods

DeserializeAnonymousType<T>(string, T)

Deserializes the JSON to the given anonymous type.

public static T DeserializeAnonymousType<T>(string value, T anonymousTypeObject)

Parameters

value string

The JSON to deserialize.

anonymousTypeObject T

The anonymous type object.

Returns

T

The deserialized anonymous type from the JSON string.

Type Parameters

T

The anonymous type to deserialize to. This can't be specified traditionally and must be infered from the anonymous type passed as a parameter.

DeserializeObject(string)

Deserializes the JSON to a .NET object.

public static object DeserializeObject(string value)

Parameters

value string

The JSON to deserialize.

Returns

object

The deserialized object from the Json string.

DeserializeObject(string, JsonSerializerSettings)

Deserializes the JSON to a .NET object.

public static object DeserializeObject(string value, JsonSerializerSettings settings)

Parameters

value string

The JSON to deserialize.

settings JsonSerializerSettings

The JsonSerializerSettings used to deserialize the object. If this is null, default serialization settings will be is used.

Returns

object

The deserialized object from the JSON string.

DeserializeObject(string, Type)

Deserializes the JSON to the specified .NET type.

public static object DeserializeObject(string value, Type type)

Parameters

value string

The JSON to deserialize.

type Type

The Type of object being deserialized.

Returns

object

The deserialized object from the Json string.

DeserializeObject(string, Type, params JsonConverter[])

Deserializes the JSON to the specified .NET type.

public static object DeserializeObject(string value, Type type, params JsonConverter[] converters)

Parameters

value string

The JSON to deserialize.

type Type

The type of the object to deserialize.

converters JsonConverter[]

Converters to use while deserializing.

Returns

object

The deserialized object from the JSON string.

DeserializeObject(string, Type, JsonSerializerSettings)

Deserializes the JSON to the specified .NET type.

public static object DeserializeObject(string value, Type type, JsonSerializerSettings settings)

Parameters

value string

The JSON to deserialize.

type Type

The type of the object to deserialize to.

settings JsonSerializerSettings

The JsonSerializerSettings used to deserialize the object. If this is null, default serialization settings will be is used.

Returns

object

The deserialized object from the JSON string.

DeserializeObject<T>(string)

Deserializes the JSON to the specified .NET type.

public static T DeserializeObject<T>(string value)

Parameters

value string

The JSON to deserialize.

Returns

T

The deserialized object from the Json string.

Type Parameters

T

The type of the object to deserialize to.

DeserializeObject<T>(string, params JsonConverter[])

Deserializes the JSON to the specified .NET type.

public static T DeserializeObject<T>(string value, params JsonConverter[] converters)

Parameters

value string

The JSON to deserialize.

converters JsonConverter[]

Converters to use while deserializing.

Returns

T

The deserialized object from the JSON string.

Type Parameters

T

The type of the object to deserialize to.

DeserializeObject<T>(string, JsonSerializerSettings)

Deserializes the JSON to the specified .NET type.

public static T DeserializeObject<T>(string value, JsonSerializerSettings settings)

Parameters

value string

The object to deserialize.

settings JsonSerializerSettings

The JsonSerializerSettings used to deserialize the object. If this is null, default serialization settings will be is used.

Returns

T

The deserialized object from the JSON string.

Type Parameters

T

The type of the object to deserialize to.

DeserializeXNode(string)

Deserializes the System.Xml.Linq.XNode from a JSON string.

public static XDocument DeserializeXNode(string value)

Parameters

value string

The JSON string.

Returns

XDocument

The deserialized XNode

DeserializeXNode(string, string)

Deserializes the System.Xml.Linq.XNode from a JSON string nested in a root elment.

public static XDocument DeserializeXNode(string value, string deserializeRootElementName)

Parameters

value string

The JSON string.

deserializeRootElementName string

The name of the root element to append when deserializing.

Returns

XDocument

The deserialized XNode

DeserializeXNode(string, string, bool)

Deserializes the System.Xml.Linq.XNode from a JSON string nested in a root elment.

public static XDocument DeserializeXNode(string value, string deserializeRootElementName, bool writeArrayAttribute)

Parameters

value string

The JSON string.

deserializeRootElementName string

The name of the root element to append when deserializing.

writeArrayAttribute bool

A flag to indicate whether to write the Json.NET array attribute. This attribute helps preserve arrays when converting the written XML back to JSON.

Returns

XDocument

The deserialized XNode

DeserializeXmlNode(string)

Deserializes the XmlNode from a JSON string.

public static XmlDocument DeserializeXmlNode(string value)

Parameters

value string

The JSON string.

Returns

XmlDocument

The deserialized XmlNode

DeserializeXmlNode(string, string)

Deserializes the XmlNode from a JSON string nested in a root elment.

public static XmlDocument DeserializeXmlNode(string value, string deserializeRootElementName)

Parameters

value string

The JSON string.

deserializeRootElementName string

The name of the root element to append when deserializing.

Returns

XmlDocument

The deserialized XmlNode

DeserializeXmlNode(string, string, bool)

Deserializes the XmlNode from a JSON string nested in a root elment.

public static XmlDocument DeserializeXmlNode(string value, string deserializeRootElementName, bool writeArrayAttribute)

Parameters

value string

The JSON string.

deserializeRootElementName string

The name of the root element to append when deserializing.

writeArrayAttribute bool

A flag to indicate whether to write the Json.NET array attribute. This attribute helps preserve arrays when converting the written XML back to JSON.

Returns

XmlDocument

The deserialized XmlNode

PopulateObject(string, object)

Populates the object with values from the JSON string.

public static void PopulateObject(string value, object target)

Parameters

value string

The JSON to populate values from.

target object

The target object to populate values onto.

PopulateObject(string, object, JsonSerializerSettings)

Populates the object with values from the JSON string.

public static void PopulateObject(string value, object target, JsonSerializerSettings settings)

Parameters

value string

The JSON to populate values from.

target object

The target object to populate values onto.

settings JsonSerializerSettings

The JsonSerializerSettings used to deserialize the object. If this is null, default serialization settings will be is used.

SerializeObject(object)

Serializes the specified object to a JSON string.

public static string SerializeObject(object value)

Parameters

value object

The object to serialize.

Returns

string

A JSON string representation of the object.

SerializeObject(object, Formatting)

Serializes the specified object to a JSON string.

public static string SerializeObject(object value, Formatting formatting)

Parameters

value object

The object to serialize.

formatting Formatting

Indicates how the output is formatted.

Returns

string

A JSON string representation of the object.

SerializeObject(object, Formatting, params JsonConverter[])

Serializes the specified object to a JSON string using a collection of JsonConverter.

public static string SerializeObject(object value, Formatting formatting, params JsonConverter[] converters)

Parameters

value object

The object to serialize.

formatting Formatting

Indicates how the output is formatted.

converters JsonConverter[]

A collection converters used while serializing.

Returns

string

A JSON string representation of the object.

SerializeObject(object, Formatting, JsonSerializerSettings)

Serializes the specified object to a JSON string using a collection of JsonConverter.

public static string SerializeObject(object value, Formatting formatting, JsonSerializerSettings settings)

Parameters

value object

The object to serialize.

formatting Formatting

Indicates how the output is formatted.

settings JsonSerializerSettings

The JsonSerializerSettings used to serialize the object. If this is null, default serialization settings will be is used.

Returns

string

A JSON string representation of the object.

SerializeObject(object, params JsonConverter[])

Serializes the specified object to a JSON string using a collection of JsonConverter.

public static string SerializeObject(object value, params JsonConverter[] converters)

Parameters

value object

The object to serialize.

converters JsonConverter[]

A collection converters used while serializing.

Returns

string

A JSON string representation of the object.

SerializeXNode(XObject)

Serializes the System.Xml.Linq.XNode to a JSON string.

public static string SerializeXNode(XObject node)

Parameters

node XObject

The node to convert to JSON.

Returns

string

A JSON string of the XNode.

SerializeXNode(XObject, Formatting)

Serializes the System.Xml.Linq.XNode to a JSON string.

public static string SerializeXNode(XObject node, Formatting formatting)

Parameters

node XObject

The node to convert to JSON.

formatting Formatting

Indicates how the output is formatted.

Returns

string

A JSON string of the XNode.

SerializeXNode(XObject, Formatting, bool)

Serializes the System.Xml.Linq.XNode to a JSON string.

public static string SerializeXNode(XObject node, Formatting formatting, bool omitRootObject)

Parameters

node XObject

The node to serialize.

formatting Formatting

Indicates how the output is formatted.

omitRootObject bool

Omits writing the root object.

Returns

string

A JSON string of the XNode.

SerializeXmlNode(XmlNode)

Serializes the XML node to a JSON string.

public static string SerializeXmlNode(XmlNode node)

Parameters

node XmlNode

The node to serialize.

Returns

string

A JSON string of the XmlNode.

SerializeXmlNode(XmlNode, Formatting)

Serializes the XML node to a JSON string.

public static string SerializeXmlNode(XmlNode node, Formatting formatting)

Parameters

node XmlNode

The node to serialize.

formatting Formatting

Indicates how the output is formatted.

Returns

string

A JSON string of the XmlNode.

SerializeXmlNode(XmlNode, Formatting, bool)

Serializes the XML node to a JSON string.

public static string SerializeXmlNode(XmlNode node, Formatting formatting, bool omitRootObject)

Parameters

node XmlNode

The node to serialize.

formatting Formatting

Indicates how the output is formatted.

omitRootObject bool

Omits writing the root object.

Returns

string

A JSON string of the XmlNode.

ToString(bool)

Converts the bool to its JSON string representation.

public static string ToString(bool value)

Parameters

value bool

The value to convert.

Returns

string

A JSON string representation of the bool.

ToString(byte)

Converts the byte to its JSON string representation.

public static string ToString(byte value)

Parameters

value byte

The value to convert.

Returns

string

A JSON string representation of the byte.

ToString(char)

Converts the char to its JSON string representation.

public static string ToString(char value)

Parameters

value char

The value to convert.

Returns

string

A JSON string representation of the char.

ToString(DateTime)

Converts the DateTime to its JSON string representation.

public static string ToString(DateTime value)

Parameters

value DateTime

The value to convert.

Returns

string

A JSON string representation of the DateTime.

ToString(DateTimeOffset)

Converts the DateTimeOffset to its JSON string representation.

public static string ToString(DateTimeOffset value)

Parameters

value DateTimeOffset

The value to convert.

Returns

string

A JSON string representation of the DateTimeOffset.

ToString(decimal)

Converts the decimal to its JSON string representation.

public static string ToString(decimal value)

Parameters

value decimal

The value to convert.

Returns

string

A JSON string representation of the sbyte.

ToString(double)

Converts the double to its JSON string representation.

public static string ToString(double value)

Parameters

value double

The value to convert.

Returns

string

A JSON string representation of the double.

ToString(Enum)

Converts the Enum to its JSON string representation.

public static string ToString(Enum value)

Parameters

value Enum

The value to convert.

Returns

string

A JSON string representation of the Enum.

ToString(Guid)

Converts the Guid to its JSON string representation.

public static string ToString(Guid value)

Parameters

value Guid

The value to convert.

Returns

string

A JSON string representation of the Guid.

ToString(short)

Converts the short to its JSON string representation.

public static string ToString(short value)

Parameters

value short

The value to convert.

Returns

string

A JSON string representation of the short.

ToString(int)

Converts the int to its JSON string representation.

public static string ToString(int value)

Parameters

value int

The value to convert.

Returns

string

A JSON string representation of the int.

ToString(long)

Converts the long to its JSON string representation.

public static string ToString(long value)

Parameters

value long

The value to convert.

Returns

string

A JSON string representation of the long.

ToString(object)

Converts the object to its JSON string representation.

public static string ToString(object value)

Parameters

value object

The value to convert.

Returns

string

A JSON string representation of the object.

ToString(sbyte)

Converts the sbyte to its JSON string representation.

[CLSCompliant(false)]
public static string ToString(sbyte value)

Parameters

value sbyte

The value to convert.

Returns

string

A JSON string representation of the sbyte.

ToString(float)

Converts the float to its JSON string representation.

public static string ToString(float value)

Parameters

value float

The value to convert.

Returns

string

A JSON string representation of the float.

ToString(string)

Converts the string to its JSON string representation.

public static string ToString(string value)

Parameters

value string

The value to convert.

Returns

string

A JSON string representation of the string.

ToString(string, char)

Converts the string to its JSON string representation.

public static string ToString(string value, char delimter)

Parameters

value string

The value to convert.

delimter char

The string delimiter character.

Returns

string

A JSON string representation of the string.

ToString(TimeSpan)

Converts the TimeSpan to its JSON string representation.

public static string ToString(TimeSpan value)

Parameters

value TimeSpan

The value to convert.

Returns

string

A JSON string representation of the TimeSpan.

ToString(ushort)

Converts the ushort to its JSON string representation.

[CLSCompliant(false)]
public static string ToString(ushort value)

Parameters

value ushort

The value to convert.

Returns

string

A JSON string representation of the ushort.

ToString(uint)

Converts the uint to its JSON string representation.

[CLSCompliant(false)]
public static string ToString(uint value)

Parameters

value uint

The value to convert.

Returns

string

A JSON string representation of the uint.

ToString(ulong)

Converts the ulong to its JSON string representation.

[CLSCompliant(false)]
public static string ToString(ulong value)

Parameters

value ulong

The value to convert.

Returns

string

A JSON string representation of the ulong.

ToString(Uri)

Converts the System.Uri to its JSON string representation.

public static string ToString(Uri value)

Parameters

value Uri

The value to convert.

Returns

string

A JSON string representation of the System.Uri.