Booleans (bool
) are serialized as JSON booleans.
false
true
Integers (u8
, u16
, u32
, u64
, s8
, s16
, s32
, s64
) may be serialized as either JSON numbers or JSON strings (base-10).
For the best language interoperability, numbers greater than (253 – 1) or less than -(253 - 1) should be serialized as base-10 strings.
12345
"-9007199254740993"
Floats (float32
, float64
) are serialized as JSON numbers, except for infinities and NaN which are serialized as strings (see below).
3.1415
-1.1e4
"NaN"
"Infinity"
"-Infinity"
A character (char
) is serialized as a JSON string containing a single Unicode Scalar Value.
"x"
"\u4e00"
"☃︎"
A string (string
) is serialized as a JSON string representing a sequence of Unicode Scalar Values.
"hello"
"x\u00d7y"
Records (record
) are serialized as JSON objects, with field names as object keys. A field of type option
may be omitted if its value is none
.
record r {
field-1: u8,
opt: option<u8>,
}
{"field-1": 123}
Tuples (tuple<...>
) are serialized as JSON arrays:
tuple<string, u8>
["str", 123]
Flags (flags
) are serialized as JSON arrays of strings, where the strings are the flag names:
flags permissions {
read,
write,
delete,
}
["read", "write"]
Lists (list<T>
) are serialized as JSON arrays.
list<u8>
[1, 2, 3]
Variants (variant
) are serialized as JSON objects with a single entry. The key of the entry is the variant case name. The value of the entry is the case payload or null
if the case has no payload.
variant filter {
all,
none,
some(list<string>),
}
{"all": null}
{"some": ["a"]}
Enums (enum
) are serialized as JSON strings with the case name.
enum directions {
north,
east,
south,
west,
}
"south"
An option's (option<T>
) serialization depends on its contained type T
:
null
.T
is itself an option<...>
, it is serialized as a JSON object {"value": T}
T
This complexity permits a ideomatic encoding of
option
s in most cases while supporting the (presumably) uncommonoption<option<...>>
pattern.
option<option<u8>>
option
is "none": null
option
is "none": {"value": null}
option
s are "some": {"value": 123}
Results (result<T, E>
) are serialized as JSON objects.
{"result": T}
{"error": E}
If the T
or E
type is omitted, the corresponding value will be null
.
result<u8>
{"result": 123}
{"error": null}
Resource handle (resource
ID, borrow<T>
) serialization is implementation-defined and may generally use any JSON value.
Generic implementations should treat serialized resource handles as opaque data.