We consider how we might offer plugins the option of structured user editing of item json in place of unstructured editing of the item's text field.
We imagine a text-like editing experience similar to programming editors with field completion and auto indenting. We could optionally add grammatical restrictions to ensure that the editing result meets the plugin's expectation.
# Grammar
A plugin can specify a grammar when invoking the json editor. The grammar is expressed in a variant of PEG with some lexical enhancements.
Rules for string, number, and boolean.
Tokens for square and curly brackets, comma and colon.
Words match literal strings unless defined otherwise.
The grammar would appear on the plugin's about page.
# Edit
The json tree would be represented as text with some additional formatting preserved from the serialized form.
Newlines and indentation will optionally substitute for json punctuation. Likewise, quotes will be omitted while editing single words.
Arrow keys will move the selection within the tree which will be cast with a tinted background except for the selection. Keystrokes will add to the current selection. Enter will add and select a new element after the selection.
Editing will be restricted to elements allowed by the optional grammar. When selection bumps into grammatical restrictions prompts for allowed forms will appear.
Grammar rules will be annotated with their about page item id so that should that page be open the rule bearing on the selection can be highlighted.
# Example
Imagine an orbital simulation where dots representing masses are animated from an initial condition specified in json markup.
We have several choices for expressing vectors.
vec = {x: number, y: number}
vec = [ number, number ]
An object has mass, position, velocity and acceleration.
obj = { mass: number (position|velocity|acceleration): vec }
An orbital simulation then is just an array of objects.
sim = [ obj ]
This grammar would guide and validate this markup for an earth-moon simulation.
earth: mass: 100000 position: [0,0] moon: position: [0,100] velocity: [100,0]
# Precedent
Paul Graham suggests optional parentheses for lisp. Python and YAML provide variations.
Chris Crawford suggests generating prompts while parsing. post
Wiki's own schema has been usefully described in PEG style grammatical rules. An interesting test case would be to edit whole pages as text within the constraints and affordances this grammar provides. See Json Schema
There is something called json-schema that uses json to describe json. This requires introduction of $ref nodes and "define": clauses to resues schema elements. page
# Experiment
We will test this idea by writing a peg.js grammar for the orbital simulation example which is divided into two sections, one derived from the sim, the other from lexical conventions of json including those we intend to relax.
// SIM sim = sql obj* sqr obj = cul ( 'mass' col number com / ('position' /'velocity' /'acceleration' ) col vec )* cur vec = sql number com number sqr // JSON sql = '[' _ / ind sqr = ']' _ cul = '{' _ / & key cur = '}' _ col = ':' _ com = ',' _ / '\n' key = word col word = [a-z]+ _ number = [0-9]+ _ ind = ' ' _ = ' '?