Skip to content
nornagon edited this page Dec 22, 2012 · 3 revisions

JSON2 is a revamp of the JSON OT spec, which is hopefully cleaner and more useful than the original.

The current JSON OT implementation is ill-suited to some particular applications, making them difficult or impossible to build with ShareJS. The particular application which motivated this rethinking is editing a tree of todo-list items, a la Workflowy.

Key ideas:

  • The invertibility requirement has been dropped. Invertibility was a large cost in implementation for little benefit to applications. JSON2 is non-invertible.
  • Arrays and objects are distinguished not by separate op types, but by the last component of an op's path. If it's a number, the op is applied to an array, otherwise it's applied to an object. This means no more od/ld, just 'd'.
  • Text operations are represented as 'suboperations', complete operations which are passed untouched to the text2 type. This opens the door to embedded documents (though we only support the text2 type for now).

With that in mind, here are the operations available in JSON2:

In the following, k(path) refers to the final element of the path (k([1,2,'a']) == 'a') and e(path) refers to the list containing all but the final elements of path (e([1,2,'a']) == [1,2]).

op function
{i:o, at:[path]} inserts o at [path]. If [path] refers to a list, o will be inserted at position k([path]) in the list. If [path] refers to an object, the key k([path]) of the object at e([path]) will be set to o.
{d:[path]} removes the object at [path].
{x:o, at:[path]} replaces the object at [path] with o.
{m:[old], to:[new]} moves the object at [old] to [new]. This is similar to deleting the object from [old] and inserting it at [new].
{s:op, at:[path]} applies the text2 op op to the string at [path].
{+:n, at:[path]} adds n to the number at [path].
Clone this wiki locally