Skip to main content

Ramson, part 1

· 2 min read

Ramson is a JSON schema that serializes and deserializes JS/ECMAScript objects precisely as they were in memory.

For example, consider the following Javascript code:

var state = { foo: 1 };
var example = { a: state, b: state };

root.a and root.b refer to the same object, such that setting root.a.foo and setting root.b.foo are equivalent operations.

However, if we were to serialize and deserialize example using JSON.stringify and JSON.parse, root.a and root.b would no longer be the same object, and setting root.a.foo would no longer also set root.b.foo.

JSMemON solves this by by assigning tags to each value. Specifically, for each value, v, that's being serialized,

  • if v, is string, number, boolean, undefined, or null, serialize it as is,
  • if v, has no reftag, assign one, and serialize the value including an extra $$def_reftag field 1,
  • if v, has a reftag, serialize only a reference to the reftag as $$ref_reftag, eluding the actual value.

A reftag (reference tag) can be any string or numerical identifier that uniquely identifies an instance of an object. For example, one could use the actual memory address of the value in the JS interpreters memory, a hash of the value, or just an incremental counter.

Example

For example, the example object from the aforementioned example could become:

{ "a": { "$$def_reftag": 1, "foo": 1 }, "b": { "$$ref_reftag": 1 } }

Recursive Objects

Consider that we also set example.self_ref = example. Whereas JSON.stringify would throw a "Converting circular structure to JSON" error, Ramson would serialize it as

{ "$$def_reftag": 1, "a": { "$$def_reftag": 2, "foo": 1 }, "b": { "$$ref_reftag": 2 }, "self_ref": { "$$ref_reftag": 1 } }

Further work

This initial draft only covers strings, numbers, booleans, undefined, null, and objects. Given this, arrays are also trivial, but require finetuning the output schema. Functions, classes, and other exotic JS objects such instances of Promise, ArrayBuffer, and others will be explored in a future post.


  1. This assumes all values that are not string, number, boolean, undefined, or null are objects, but there are arrays too...