Home JsonElementExtensions Class
JsonElementExtensions Class
Cancel

JsonElementExtensions Class

Namespace: Json.More

Inheritance: JsonElementExtensions 🡒 object

Provides extension functionality for System.Text.Json.JsonElement.

Methods

AsJsonElement(this long value)

Converts a System.Int64 to a System.Text.Json.JsonElement.

Declaration

1
public static JsonElement AsJsonElement(this long value)
ParameterTypeDescription
valuelongThe value to convert.

Returns

A System.Text.Json.JsonElement representing the value.

Remarks

This is a workaround for lack of native support in the System.Text.Json namespace.

AsJsonElement(this int value)

Converts a System.Int32 to a System.Text.Json.JsonElement.

Declaration

1
public static JsonElement AsJsonElement(this int value)
ParameterTypeDescription
valueintThe value to convert.

Returns

A System.Text.Json.JsonElement representing the value.

Remarks

This is a workaround for lack of native support in the System.Text.Json namespace.

AsJsonElement(this short value)

Converts a System.Int16 to a System.Text.Json.JsonElement.

Declaration

1
public static JsonElement AsJsonElement(this short value)
ParameterTypeDescription
valueshortThe value to convert.

Returns

A System.Text.Json.JsonElement representing the value.

Remarks

This is a workaround for lack of native support in the System.Text.Json namespace.

AsJsonElement(this bool value)

Converts a System.Boolean to a System.Text.Json.JsonElement.

Declaration

1
public static JsonElement AsJsonElement(this bool value)
ParameterTypeDescription
valueboolThe value to convert.

Returns

A System.Text.Json.JsonElement representing the value.

Remarks

This is a workaround for lack of native support in the System.Text.Json namespace.

AsJsonElement(this decimal value)

Converts a System.Int64 to a System.Text.Json.JsonElement.

Declaration

1
public static JsonElement AsJsonElement(this decimal value)
ParameterTypeDescription
valuedecimalThe value to convert.

Returns

A System.Text.Json.JsonElement representing the value.

Remarks

This is a workaround for lack of native support in the System.Text.Json namespace.

AsJsonElement(this double value)

Converts a System.Double to a System.Text.Json.JsonElement.

Declaration

1
public static JsonElement AsJsonElement(this double value)
ParameterTypeDescription
valuedoubleThe value to convert.

Returns

A System.Text.Json.JsonElement representing the value.

Remarks

This is a workaround for lack of native support in the System.Text.Json namespace.

AsJsonElement(this float value)

Converts a System.Single to a System.Text.Json.JsonElement.

Declaration

1
public static JsonElement AsJsonElement(this float value)
ParameterTypeDescription
valuefloatThe value to convert.

Returns

A System.Text.Json.JsonElement representing the value.

Remarks

This is a workaround for lack of native support in the System.Text.Json namespace.

AsJsonElement(this string value)

Converts a System.String to a System.Text.Json.JsonElement. Can also be used to get a null element.

Declaration

1
public static JsonElement AsJsonElement(this string value)
ParameterTypeDescription
valuestringThe value to convert.

Returns

A System.Text.Json.JsonElement representing the value.

Remarks

This is a workaround for lack of native support in the System.Text.Json namespace.

AsJsonElement(this IEnumerable<JsonElement> values)

Converts a System.Int64 to a System.Text.Json.JsonElement.

Declaration

1
public static JsonElement AsJsonElement(this IEnumerable<JsonElement> values)
ParameterTypeDescription
valuesIEnumerable<JsonElement>The array of values to convert.

Returns

A System.Text.Json.JsonElement representing the value.

Remarks

This is a workaround for lack of native support in the System.Text.Json namespace.

AsJsonElement(this IDictionary<string, JsonElement> values)

Converts a System.Int64 to a System.Text.Json.JsonElement.

Declaration

1
public static JsonElement AsJsonElement(this IDictionary<string, JsonElement> values)
ParameterTypeDescription
valuesIDictionary<string, JsonElement>The value to convert.

Returns

A System.Text.Json.JsonElement representing the value.

Remarks

This is a workaround for lack of native support in the System.Text.Json namespace.

AsNode(this JsonElement element)

Converts a System.Text.Json.JsonElement to a System.Text.Json.Nodes.JsonNode.

Declaration

1
public static JsonNode AsNode(this JsonElement element)
ParameterTypeDescription
elementJsonElementThe element.

Returns

An equivalent node.

Remarks

This provides a single point of conversion as one is not provided by .Net. See https://github.com/dotnet/runtime/issues/70427 for more information.

GetEquivalenceHashCode(this JsonElement element, int maxHashDepth)

Generate a consistent JSON-value-based hash code for the element.

Declaration

1
public static int GetEquivalenceHashCode(this JsonElement element, int maxHashDepth)
ParameterTypeDescription
elementJsonElementThe element.
maxHashDepthintMaximum depth to calculate. Default is -1 which utilizes the entire structure without limitation.

Returns

The hash code.

Remarks

See the following for discussion on why the default implementation is insufficient:

  • https://github.com/json-everything/json-everything/issues/76
  • https://github.com/dotnet/runtime/issues/33388

IsEquivalentTo(this JsonElement a, JsonElement b)

Determines JSON-compatible equivalence.

Declaration

1
public static bool IsEquivalentTo(this JsonElement a, JsonElement b)
ParameterTypeDescription
aJsonElementThe first element.
bJsonElementThe second element.

Returns

true if the element are equivalent; false otherwise.

ToJsonString(this JsonElement element)

Just a shortcut for calling JsonSerializer.Serialize() because .ToString() doesn’t do what you might expect.

Declaration

1
public static string ToJsonString(this JsonElement element)
ParameterTypeDescription
elementJsonElementThe value to convert.

Returns

A JSON string.

Remarks

See https://github.com/dotnet/runtime/issues/42502

Contents