B
Beacon Globe News

Class JsonExtensions | Azure SDK for Net

Author

James Sullivan

Published Feb 16, 2026

Defines extension methods for various JSON.NET types that make it easier to implement a custom JsonConverter.

Inheritance

JsonExtensions

Namespace: Microsoft.Azure.Search.Serialization
Assembly: Microsoft.Azure.Search.Common.dll
Syntax
public static class JsonExtensions

Methods

Advance(JsonReader)

Advances the given JSON reader, or throws a JsonSerializationException if it cannot be advanced.

Declaration
public static void Advance (this Newtonsoft.Json.JsonReader reader);
Parameters
Newtonsoft.Json.JsonReader reader

The JSON reader to advance.

Expect(JsonReader, JsonToken, Object[])

Asserts that the given JSON reader is positioned on a token with the expected type. Optionally asserts that the value of the token matches a given expected value. If any of the assertions fail, this method throws a JsonSerializationException.

Declaration
public static void Expect (this Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.JsonToken expectedToken, params object[] expectedValues);
Parameters
Newtonsoft.Json.JsonReader reader

The JSON reader.

Newtonsoft.Json.JsonToken expectedToken

The JSON token on which the reader is expected to be positioned.

Object[] expectedValues

Optional; The expected possible values of the current JSON token.

Expect<TValue>(JsonReader, JsonToken, Object[])

Asserts that the given JSON reader is positioned on a token with the expected type and retrieves the value of the token, if any. Optionally asserts that the value of the token matches a given expected value. If any of the assertions fail, this method throws a JsonSerializationException.

Declaration
public static TValue Expect<TValue> (this Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.JsonToken expectedToken, params object[] expectedValues);
Parameters
Newtonsoft.Json.JsonReader reader

The JSON reader.

Newtonsoft.Json.JsonToken expectedToken

The JSON token on which the reader is expected to be positioned.

Object[] expectedValues

Optional; The expected possible values of the current JSON token.

Returns
TValue

The value of the current JSON token, or default(TValue) if the current token has no value.

Type Parameters
TValue

The expected type of the value of the current JSON token.

ExpectAndAdvance(JsonReader, JsonToken, Object[])

Asserts that the given JSON reader is positioned on a token with the expected type. Optionally asserts that the value of the token matches a given expected value. If any of the assertions fail, this method throws a JsonSerializationException. Otherwise, this method attempts to advance the JSON reader to the next position.

Declaration
public static void ExpectAndAdvance (this Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.JsonToken expectedToken, params object[] expectedValues);
Parameters
Newtonsoft.Json.JsonReader reader

The JSON reader.

Newtonsoft.Json.JsonToken expectedToken

The JSON token on which the reader is expected to be positioned.

Object[] expectedValues

Optional; The expected possible values of the current JSON token.

ExpectAndAdvance<TValue>(JsonReader, JsonToken, Object[])

Asserts that the given JSON reader is positioned on a token with the expected type and retrieves the value of the token, if any. Optionally asserts that the value of the token matches a given expected value. If any of the assertions fail, this method throws a JsonSerializationException. Otherwise, this method attempts to advance the JSON reader to the next position.

Declaration
public static TValue ExpectAndAdvance<TValue> (this Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.JsonToken expectedToken, params object[] expectedValues);
Parameters
Newtonsoft.Json.JsonReader reader

The JSON reader.

Newtonsoft.Json.JsonToken expectedToken

The JSON token on which the reader is expected to be positioned.

Object[] expectedValues

Optional; The expected possible values of the current JSON token.

Returns
TValue

The value of the JSON token before advancing the reader, or default(TValue) if the token has no value.

Type Parameters
TValue

The expected type of the value of the current JSON token.

IsNumber(JToken)

Indicates whether or not the given JSON token is a numeric literal.

Declaration
public static bool IsNumber (this Newtonsoft.Json.Linq.JToken token);
Parameters
Newtonsoft.Json.Linq.JToken token

The token to check.

Returns
Boolean

true if the given JSON token represents a number, false otherwise.

IsString(JToken, String)

Indicates whether or not the given JSON token matches the expected string.

Declaration
public static bool IsString (this Newtonsoft.Json.Linq.JToken token, string expectedValue);
Parameters
Newtonsoft.Json.Linq.JToken token

The token to check.

String expectedValue

The expected string value.

Returns
Boolean

true if the given JSON token matches the expected string, false otherwise.

IsValid(JObject, IEnumerable<String>, Func<JProperty,Boolean>)

Validates the properties of the given JSON object, enforcing the presence of required properties and ignoring the order of properties.

Declaration
public static bool IsValid (this Newtonsoft.Json.Linq.JObject obj, System.Collections.Generic.IEnumerable<string> requiredProperties, Func<Newtonsoft.Json.Linq.JProperty,bool> isPropertyValid);
Parameters
Newtonsoft.Json.Linq.JObject obj

The JSON object to validate.

IEnumerable<String> requiredProperties

The names of all JSON properties that are expected to be present in the given object.

Func<Newtonsoft.Json.Linq.JProperty,Boolean> isPropertyValid

A predicate that determines whether the name and value of given JProperty are valid.

Returns
Boolean

true if all properties of the given JSON object pass the given validation function and all required properties exist,false otherwise.

ReadObject(JsonReader, IEnumerable<String>, Action<JsonReader,String>)

Reads the properties of JSON objects, enforcing the presence of required properties and ignoring the order of properties.

Declaration
public static void ReadObject (this Newtonsoft.Json.JsonReader reader, System.Collections.Generic.IEnumerable<string> requiredProperties, Action<Newtonsoft.Json.JsonReader,string> readProperty);
Parameters
Newtonsoft.Json.JsonReader reader

The JSON reader to use to read an object.

IEnumerable<String> requiredProperties

The names of all JSON properties that are expected to be present in the parsed object.

Action<Newtonsoft.Json.JsonReader,String> readProperty

A callback that reads a property value with the given name from the given JsonReader. It must advance the reader to the name of the next property, or the end of the object if there are no more properties to read.

ReadObject(JsonReader, IEnumerable<String>, IEnumerable<String>, Action<JsonReader,String>)

Reads the properties of JSON objects, enforcing the presence of required properties and ignoring the order of properties.

Declaration
public static void ReadObject (this Newtonsoft.Json.JsonReader reader, System.Collections.Generic.IEnumerable<string> requiredProperties, System.Collections.Generic.IEnumerable<string> optionalProperties, Action<Newtonsoft.Json.JsonReader,string> readProperty);
Parameters
Newtonsoft.Json.JsonReader reader

The JSON reader to use to read an object.

IEnumerable<String> requiredProperties

The names of all JSON properties that are expected to be present in the parsed object.

IEnumerable<String> optionalProperties

The names of JSON properties besides the required properties that may be present in the parsed object.

Action<Newtonsoft.Json.JsonReader,String> readProperty

A callback that reads a property value with the given name from the given JsonReader. It must advance the reader to the name of the next property, or the end of the object if there are no more properties to read.

ReadObjectAndAdvance(JsonReader, IEnumerable<String>, Action<JsonReader,String>)

Reads the properties of JSON objects, enforcing the presence of required properties and ignoring the order of properties, and then advances the given reader to the next token after the end of the object.

Declaration
public static void ReadObjectAndAdvance (this Newtonsoft.Json.JsonReader reader, System.Collections.Generic.IEnumerable<string> requiredProperties, Action<Newtonsoft.Json.JsonReader,string> readProperty);
Parameters
Newtonsoft.Json.JsonReader reader

The JSON reader to use to read an object.

IEnumerable<String> requiredProperties

The names of all JSON properties that are expected to be present in the parsed object.

Action<Newtonsoft.Json.JsonReader,String> readProperty

A callback that reads a property value with the given name from the given JsonReader. It must advance the reader to the name of the next property, or the end of the object if there are no more properties to read.

Remarks