The JSON type representation of a JSON-incompatible object is an equivalent JavaScript object with properties encoded such that the information can be serialized to JSON. This typically has the same properties as the original object for compatible data types, while incompatible properties are converted/serialized to compatible types. For example, buffer properties in the original object might be base64url -encoded to strings in the JSON-type representation.
An object that cannot automatically be serialized to JSON using the
JSON.stringify()
method can define an instance method named toJSON()
that returns the JSON-type representation
of the original object.
JSON.stringify()
will then use toJSON()
to get the object to stringify, instead of the original object.
PublicKeyCredential.toJSON()
and
Performance.toJSON()
are examples of this approach.
A JSON string serialized in this way can be deserialized back to the JSON-type representation
object using
JSON.parse()
.
It is common to provide a converter method, such as
PublicKeyCredential.parseCreationOptionsFromJSON()
, to convert the JSON-type representation
back to the original object.