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 JSValueRemarks
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
| Name | Description |
|---|---|
| Null | Gets the JavaScript null value. |
| Undefined | Gets the JavaScript undefined value. |
Null
JSValue.csUndefined
JSValue.csMethods
| Name | Description |
|---|---|
| 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.csCreates a JavaScript BigInt.
public static JSValue BigInt(BigInteger value)Boolean(bool)
JSValue.csDate overloads
Date(DateTime)
JSValue.csCreates a JavaScript Date. JavaScript dates have a precision of one millisecond, so the remaining ticks are truncated.
Parameters
valueDateTime- The date. A
DateTimeKind.LocalorDateTimeKind.Unspecifiedvalue is a local time, as forDateTimeOffset.
Returns
Date(DateTimeOffset)
JSValue.csCreates a JavaScript Date. JavaScript dates have a precision of one millisecond, so the remaining ticks are truncated.
public static JSValue Date(DateTimeOffset value)Number overloads
Number(byte)
JSValue.csNumber(decimal)
JSValue.csCreates a JavaScript number, which is the nearest double-precision value. Use JSValue.BigInt to preserve large integers exactly.
Number(double)
JSValue.csCreates a JavaScript number. Double.NaN, infinities, and negative zero are preserved.
Number(Half)
JSValue.csCreates a JavaScript number. Double.NaN, infinities, and negative zero are preserved.
Number(Int128)
JSValue.csCreates a JavaScript number, which is the nearest double-precision value. Use JSValue.BigInt to preserve large integers exactly.
Number(short)
JSValue.csNumber(int)
JSValue.csNumber(long)
JSValue.csCreates a JavaScript number, which is the nearest double-precision value. Use JSValue.BigInt to preserve large integers exactly.
Number(sbyte)
JSValue.csNumber(float)
JSValue.csCreates a JavaScript number. Double.NaN, infinities, and negative zero are preserved.
Number(UInt128)
JSValue.csCreates a JavaScript number, which is the nearest double-precision value. Use JSValue.BigInt to preserve large integers exactly.
Number(ushort)
JSValue.csNumber(uint)
JSValue.csNumber(ulong)
JSValue.csCreates a JavaScript number, which is the nearest double-precision value. Use JSValue.BigInt to preserve large integers exactly.
String(string)
JSValue.csToJsonNode()
JSValue.csConverts the value to a JsonNode, to use it as an argument or in a JsonObject or JsonArray argument.
public JsonNode ToJsonNode()Returns
ToString()
JSValue.csUint8Array(ReadOnlyMemory<byte>)
JSValue.csCreates a JavaScript Uint8Array containing a copy of the data.
public static JSValue Uint8Array(ReadOnlyMemory<byte> value)Parameters
valueReadOnlyMemory<byte>- The data. It is read when the call is sent, so it must not be modified before.
Returns
Operators
| Name | Description |
|---|---|
| implicit operator JsonNode?(JSValue?) | Converts the value to a JsonNode, to use it as an argument or in a JsonObject or JsonArray argument. |