JSValue Class

A JavaScript value passed as an argument of a call, including values that JSON cannot represent, such as undefined or a BigInt.

public sealed class JSValue

Remarks

A JSValue converts implicitly to a JsonNode, so it can be used as an argument, or nested in a JsonObject or a JsonArray argument.

It cannot be serialized in any other way: the resulting JsonNode cannot be written as JSON, and a JSValue cannot be a member of an object serialized using reflection.

Example

await using var node = await NodeJsHost.StartAsync();

var types = await node.EvaluateAsync(
    "return [typeof args[0], typeof args[1], args[2].date instanceof Date];",
    [JSValue.Undefined, JSValue.BigInt(long.MaxValue), new JsonObject { ["date"] = JSValue.Date(DateTimeOffset.UtcNow) }]);

Assert.Equal("undefined", types[0].GetString());
Assert.Equal("bigint", types[1].GetString());
Assert.True(types[2].GetBoolean());

Properties

NameDescription
NullGets the JavaScript null value.
UndefinedGets the JavaScript undefined value.

Gets the JavaScript null value.

public static JSValue Null { get; }

Property value

JSValue

Undefined

JSValue.cs

Gets the JavaScript undefined value.

public static JSValue Undefined { get; }

Property value

JSValue

Methods

NameDescription
BigInt(BigInteger)Creates a JavaScript BigInt.
Boolean(bool)Creates a JavaScript boolean.
Date(DateTime)Creates a JavaScript Date. JavaScript dates have a precision of one millisecond, so the remaining ticks are truncated.
Date(DateTimeOffset)Creates a JavaScript Date. JavaScript dates have a precision of one millisecond, so the remaining ticks are truncated.
Number(byte)Creates a JavaScript number.
Number(decimal)Creates a JavaScript number, which is the nearest double-precision value. Use JSValue.BigInt to preserve large integers exactly.
Number(double)Creates a JavaScript number. Double.NaN, infinities, and negative zero are preserved.
Number(Half)Creates a JavaScript number. Double.NaN, infinities, and negative zero are preserved.
Number(Int128)Creates a JavaScript number, which is the nearest double-precision value. Use JSValue.BigInt to preserve large integers exactly.
Number(short)Creates a JavaScript number.
Number(int)Creates a JavaScript number.
Number(long)Creates a JavaScript number, which is the nearest double-precision value. Use JSValue.BigInt to preserve large integers exactly.
Number(sbyte)Creates a JavaScript number.
Number(float)Creates a JavaScript number. Double.NaN, infinities, and negative zero are preserved.
Number(UInt128)Creates a JavaScript number, which is the nearest double-precision value. Use JSValue.BigInt to preserve large integers exactly.
Number(ushort)Creates a JavaScript number.
Number(uint)Creates a JavaScript number.
Number(ulong)Creates a JavaScript number, which is the nearest double-precision value. Use JSValue.BigInt to preserve large integers exactly.
String(string)Creates a JavaScript string.
ToJsonNode()Converts the value to a JsonNode, to use it as an argument or in a JsonObject or JsonArray argument.
ToString()Returns the JavaScript representation of the value (e.g. undefined, 123n, NaN). A string is returned as is.
Uint8Array(ReadOnlyMemory<byte>)Creates a JavaScript Uint8Array containing a copy of the data.

BigInt(BigInteger)

JSValue.cs

Creates a JavaScript BigInt.

public static JSValue BigInt(BigInteger value)

Parameters

value BigInteger

Returns

JSValue

Boolean(bool)

JSValue.cs

Creates a JavaScript boolean.

public static JSValue Boolean(bool value)

Parameters

value bool

Returns

JSValue

Date overloads

Date(DateTime)

JSValue.cs

Creates a JavaScript Date. JavaScript dates have a precision of one millisecond, so the remaining ticks are truncated.

public static JSValue Date(DateTime value)
Parameters
value DateTime
The date. A DateTimeKind.Local or DateTimeKind.Unspecified value is a local time, as for DateTimeOffset.
Returns

JSValue

Date(DateTimeOffset)

JSValue.cs

Creates a JavaScript Date. JavaScript dates have a precision of one millisecond, so the remaining ticks are truncated.

public static JSValue Date(DateTimeOffset value)
Parameters
value DateTimeOffset
Returns

JSValue

Number overloads

Number(byte)

JSValue.cs

Creates a JavaScript number.

public static JSValue Number(byte value)
Parameters
value byte
Returns

JSValue

Number(decimal)

JSValue.cs

Creates a JavaScript number, which is the nearest double-precision value. Use JSValue.BigInt to preserve large integers exactly.

public static JSValue Number(decimal value)
Parameters
value decimal
Returns

JSValue

Number(double)

JSValue.cs

Creates a JavaScript number. Double.NaN, infinities, and negative zero are preserved.

public static JSValue Number(double value)
Parameters
value double
Returns

JSValue

Number(Half)

JSValue.cs

Creates a JavaScript number. Double.NaN, infinities, and negative zero are preserved.

public static JSValue Number(Half value)
Parameters
value Half
Returns

JSValue

Number(Int128)

JSValue.cs

Creates a JavaScript number, which is the nearest double-precision value. Use JSValue.BigInt to preserve large integers exactly.

public static JSValue Number(Int128 value)
Parameters
value Int128
Returns

JSValue

Number(short)

JSValue.cs

Creates a JavaScript number.

public static JSValue Number(short value)
Parameters
value short
Returns

JSValue

Number(int)

JSValue.cs

Creates a JavaScript number.

public static JSValue Number(int value)
Parameters
value int
Returns

JSValue

Number(long)

JSValue.cs

Creates a JavaScript number, which is the nearest double-precision value. Use JSValue.BigInt to preserve large integers exactly.

public static JSValue Number(long value)
Parameters
value long
Returns

JSValue

Number(sbyte)

JSValue.cs

Creates a JavaScript number.

public static JSValue Number(sbyte value)
Parameters
value sbyte
Returns

JSValue

Number(float)

JSValue.cs

Creates a JavaScript number. Double.NaN, infinities, and negative zero are preserved.

public static JSValue Number(float value)
Parameters
value float
Returns

JSValue

Number(UInt128)

JSValue.cs

Creates a JavaScript number, which is the nearest double-precision value. Use JSValue.BigInt to preserve large integers exactly.

public static JSValue Number(UInt128 value)
Parameters
value UInt128
Returns

JSValue

Number(ushort)

JSValue.cs

Creates a JavaScript number.

public static JSValue Number(ushort value)
Parameters
value ushort
Returns

JSValue

Number(uint)

JSValue.cs

Creates a JavaScript number.

public static JSValue Number(uint value)
Parameters
value uint
Returns

JSValue

Number(ulong)

JSValue.cs

Creates a JavaScript number, which is the nearest double-precision value. Use JSValue.BigInt to preserve large integers exactly.

public static JSValue Number(ulong value)
Parameters
value ulong
Returns

JSValue

String(string)

JSValue.cs

Creates a JavaScript string.

public static JSValue String(string value)

Parameters

value string

Returns

JSValue

ToJsonNode()

JSValue.cs

Converts the value to a JsonNode, to use it as an argument or in a JsonObject or JsonArray argument.

public JsonNode ToJsonNode()

Returns

JsonNode

ToString()

JSValue.cs

Returns the JavaScript representation of the value (e.g. undefined, 123n, NaN). A string is returned as is.

public override string ToString()

Returns

string

Uint8Array(ReadOnlyMemory<byte>)

JSValue.cs

Creates a JavaScript Uint8Array containing a copy of the data.

public static JSValue Uint8Array(ReadOnlyMemory<byte> value)

Parameters

value ReadOnlyMemory<byte>
The data. It is read when the call is sent, so it must not be modified before.

Returns

JSValue

Operators

NameDescription
implicit operator JsonNode?(JSValue?)Converts the value to a JsonNode, to use it as an argument or in a JsonObject or JsonArray argument.

implicit operator JsonNode?(JSValue?)

JSValue.cs

Converts the value to a JsonNode, to use it as an argument or in a JsonObject or JsonArray argument.

public static implicit operator JsonNode?(JSValue? value)

Parameters

value JSValue?

Returns

JsonNode?