Skip to content

5. Type definitions and processing

This section defines the abstract model that every implementation follows. It does not define a programming interface: function names and signatures are left to each implementation.

ECSON is a serialization for transport. The types defined by ECSON form a model of values; ECSON serializes them without loss, and the plain JSON form (section 5.3) serializes them with loss, for receivers that do not use ECSON. Applications and data stores keep values in their own native types, not as ECSON.

Sections 5.4 and 5.5 name ECMAScript mechanisms: the toJSON method, the static fromJSON method and the behavior of JSON.stringify. Implementations in other host languages use the equivalent mechanism of their language and state which one (CONFORMANCE_DECLARE). The serialized forms are the same in every language, such as $class with ctor and value.

A type definition describes one tag. It consists of:

  • the tag, a string starting with a single $;
  • the serialized form: the JSON value that represents an instance of the type;
  • the validation of the serialized form, which states when a value under that tag is malformed;
  • the plain JSON form: the JSON value used when the value is sent without ECSON (section 5.3).

Each type in sections 3 and 4 is a type definition. The $class form (section 5.4) is also a type definition, defined by this section and outside any type set. Applications MAY add their own type definitions, following the namespace rules in section 6.

TYPEDEF_VALIDATE MUST

When deserializing a typed value whose tag is known, an implementation MUST fail if the serialized form does not match the type definition.

CIRCULAR_FAIL MUST

Serialization MUST fail when the value contains a circular reference, including one reached through the result of a toJSON method.

DEPTH_LIMIT MAY

An implementation MAY limit the nesting depth it serializes or deserializes. The depth is counted in the JSON text of the ECSON form, where each array and object, including the root, is one level, so { "$set": [] } has two levels. When the limit is exceeded, the implementation MUST fail, rather than truncate.

TAG_ALLOWLIST MAY

An implementation MAY let users restrict processing to a list of tags. Typed values with tags outside the list MUST then be handled as unknown tags (section 7) when deserializing, and serializing a native value whose tag would be outside the list MUST fail. Unknown typed values preserved under UNKNOWN_PRESERVE are written back unchanged.

The plain JSON form lets a value be sent to a receiver that reads only JSON. It keeps the data that JSON can carry and drops the type information.

PLAIN_FORM_DEFAULT MUST

The plain JSON form of a typed value MUST be its serialized form, itself in plain JSON form, without the tag, except for the types in the table below.

Tag Plain JSON form
$date the RFC 3339 string of TYPE_DATE_STRING; for years outside 0000 to 9999, the expanded year string of ECMAScript Date.prototype.toISOString (for example +275760-09-13T00:00:00.000Z); null for an invalid date
$bigint a JSON number
$number null, and 0 for negative zero
$undefined omitted as a member of an object; null elsewhere, including at the root and inside $set and $map
$class for a registered class, the plain JSON form of value; for an unregistered identifier, the raw object with ctor and value, as for any unknown tag (CLASS_REGISTERED_ONLY, PLAIN_FORM_UNKNOWN)
$regexp the string /source/flags
$symbol the description string, or the registry key for a registered symbol
$bytes the Base64 string of data
$binary (BSON) the Base64 string of base64
$minKey, $maxKey (BSON) null

PLAIN_FORM_CUSTOM MAY

A type definition outside this specification MAY declare another plain JSON form.

PLAIN_FORM_UNKNOWN MUST

The plain JSON form of an unknown tag (section 7) MUST be its raw serialized value, without the tag: keys inside it are not unescaped and typed values inside it are not converted (UNKNOWN_RAW).

PLAIN_FORM_NO_ESCAPE MUST

In the plain JSON form, keys of plain objects MUST NOT be escaped (section 2): a key $ref is written as $ref.

PLAIN_FORM_READ MUST

A JSON text received in the plain JSON form MUST be read as plain JSON: keys starting with $ are plain keys, and no object is interpreted as a typed value.

Applications that own a type define it with a namespaced tag (sections 5.1 and 6), such as $acme.money, and register it in the implementation. For instances of application classes that have no registered tag, ECSON defines a generic form based on toJSON and fromJSON.

CLASS_FORM MUST

An instance of a registered class MUST be serialized under the tag $class as an object with two members: ctor, the class identifier, and value, the result of toJSON() serialized as ECSON. A registered class is a class without a registered tag, with a toJSON method and a static fromJSON method, that the application registered under a class identifier.

CLASS_IDENTIFIER MUST

The class identifier MUST be declared by the application when it registers the class, and MUST NOT be taken from the runtime name of the class. It MUST follow TAG_GRAMMAR without the leading $: a namespace, a . and a class name (for example acme.Money).

CLASS_EXACT MUST

The $class form MUST apply only to instances of the registered class itself, not of its subclasses. An instance of a subclass that is not registered itself goes on to the next steps of SERIALIZE_ORDER: the type sets, when it is an instance of one of their types, or else the toJSON step, through the inherited toJSON. Either way it loses its class.

CLASS_READ_FORM MUST

When deserializing, a $class value whose ctor does not follow the grammar of CLASS_IDENTIFIER, without value, or with members other than ctor and value, MUST fail (TYPEDEF_VALIDATE).

CLASS_REGISTERED_ONLY MUST

When deserializing a $class value, an implementation MUST call the static fromJSON method with the deserialized value only for classes registered by the application under that identifier, and MUST NOT look up classes by name in any other way. A $class value with an unregistered identifier MUST be handled as an unknown tag (section 7).

A value that is not JSON, not a known type and not a plain object (an object whose prototype is Object.prototype or null) is outside the model: for example a function, a WeakMap or an instance of an application class that has no registered tag and is not a registered class (section 5.4). SERIALIZE_ORDER decides how each one is written.

SERIALIZE_ORDER MUST

Serialization MUST try, in order: (1) the registered application types; (2) the $class form (section 5.4); (3) the core type set and the extension type sets the implementation supports; (4) for a value that does not meet CLASS_FORM, the result of its toJSON method, except for functions (FUNCTION_NOT_SERIALIZED); and (5), for any other value, the behavior of ECMAScript JSON.stringify (OUT_OF_MODEL_JSON).

CLASS_OVER_ERROR MUST

An instance of a registered class (CLASS_FORM) MUST be serialized in the $class form, even when it is also an array or an instance of a type of the core type set or of an extension type set, such as a subclass of Array, Map, Date or Error. An instance of such a type that is not a registered class keeps the form of its type set.

SERIALIZE_ORDER_SCOPE MUST

The toJSON step MUST NOT apply to plain objects and arrays, which are in the model.

TOJSON_CALL MUST

Every call to toJSON, in the $class form and in the toJSON step, MUST pass the key as JSON.stringify does: the member name, the array index as a string, and the empty string at the root. Inside $set and $map, which JSON.stringify does not serialize, the key MUST be the empty string.

TOJSON_RESULT MUST

The result of toJSON MUST be serialized as ECSON, so that a Date inside it becomes $date, but it MUST NOT go through the $class and toJSON steps again: its own toJSON is never called. Its members go through the whole order.

OUT_OF_MODEL_JSON MUST

Values that reach the last step of SERIALIZE_ORDER MUST be serialized as JSON.stringify does. A Number, String, Boolean or BigInt object is unboxed, and the primitive is serialized as ECSON (so Object(1n) becomes $bigint). Any other object becomes a plain object with its own enumerable string-keyed properties, whose values are serialized as ECSON, so that a Date inside it becomes $date. A function, or a symbol or undefined in an implementation that does not support $symbol or $undefined, is omitted as a member of an object and becomes null elsewhere, including at the root and inside $set and $map.

OUT_OF_MODEL_REPORT MAY

An implementation MAY report the values handled by OUT_OF_MODEL_JSON, with their path and reason, without failing.

FUNCTION_NOT_SERIALIZED MUST NOT

The source code of a function MUST NOT be serialized, and a toJSON method of a function MUST NOT be called.

CODE_NOT_EVALUATED MUST NOT

When deserializing, an implementation MUST NOT evaluate code received in the data.

Source: lingodata/ecson@fe27835 · spec/sections/05-type-definitions.md · ecson 0.2.0 (draft)

LingoData specifications, reference implementations and this site are licensed under the Apache License 2.0. Copyright 2026 Gustavo Gondim.