JsonValueAsNumber Property

KGy SOFT JSON Libraries Help
Gets the numeric value of this JsonValue instance if it has Number Type; or , if its Type is not Number. The returned value is a double to be conform with JSON Number type. To retrieve the actual stored raw value use the AsLiteral property. To retrieve the value as .NET numeric types use the methods in the JsonValueExtensions class.
See the Remarks section for details.

Namespace:  KGySoft.Json
Assembly:  KGySoft.Json (in KGySoft.Json.dll) Version: 2.0.1

public double? AsNumber { get; }

Property Value

Type: NullableDouble

Caution note Caution
The JavaScript Number type is always a double-precision 64-bit binary format IEEE 754 value, which is the equivalent of the double type in C#. It is not recommended to store C# long and decimal types as JavaScript numbers because their precision might be lost silently if the JSON is processed by JavaScript. If you still want to do so use the ToJson extension methods or the CreateNumberUnchecked method.

When getting this property the stored underlying string is converted to a double so it has the same behavior as a JavaScript Number.

If this JsonValue was created from a C# long or decimal value (see the ToJson overloads), then this property may return a different value due to loss of precision. This is how JavaScript also behaves. To get the value as specific .NET numeric types use the extension methods in the JsonValueExtensions class.

To retrieve the stored actual raw value without any conversion you can use the AsLiteral property.

This property may return if this instance was created by the CreateNumberUnchecked method and contains an invalid number.

This property can also return when a NaN or Infinity/-Infinity was parsed, which are not valid in JSON. But even such values can be retrieved as a double by the AsDouble extension method.

See Also