JSON Preprocessing

A settings file often repeats itself, for example when a project has several robots or objects that differ in a few values only. LumiScan VGR expands these macros when it reads the file, before the settings are checked: $ref and $parents, and the older $import, which it still reads.

$ref

An object with a $ref key is replaced by the value that the JSON pointer names. The whole object is replaced; other keys next to $ref are not kept, and an editor that checks the file against its schema marks them.

{
    "obj1": "val1",
    "obj2": { "$ref": "/obj1" }
}

expands to

{
    "obj1": "val1",
    "obj2": "val1"
}

The pointer can also name a value in another file, written <file>#/<pointer>: { "$ref": "common.json#/gripper" }. From v2.0.0, this is how an entry is taken from another file; a recipe kept in a file of its own is read this way (see Recipe Settings).

$import

Deprecated from v2.0.0. Write one $ref to the other file instead (see $ref). LumiScan VGR still reads $import, and an editor that checks the file against its schema marks it as deprecated.

An object with an $import key is replaced by the whole content of the file it names. Together with $ref, only the value that the pointer names is taken from that file:

other.json:
{ "otherObj": 1 }

main.json:
{ "obj": { "$import": "other.json", "$ref": "/otherObj" } }

main.json expands to { "obj": 1 }. Written with $ref alone, the same entry is { "obj": { "$ref": "other.json#/otherObj" } }.

A file that imports itself, directly or through other files, is refused.

$parents

$parents makes objects into templates. Its value is an array of JSON pointers, and the object that contains it takes the values of the objects they name:

{
    "base": { "a": 1, "nested": { "x": 1, "y": 2 }, "list": [1, 2] },
    "obj": { "$parents": ["/base"], "a": 5, "nested": { "y": 3 }, "list": [9] }
}

obj expands to { "a": 5, "nested": { "x": 1, "y": 3 }, "list": [9] }:

  • A value of the object itself wins over the parents' value.

  • A key that the object does not have is taken from the parents.

  • Nested objects are merged key by key, by the same rules.

  • An array is a single value: it is taken whole and never merged.

Do not set the same key in two parents of one object; keep each parent to keys of its own. $ref is expanded before $parents.

File Paths

A relative path in a $ref to another file, or in $import, is relative to the folder of the file that contains it. LumiScan VGR reads the settings file from its own folder, so a path in the settings file itself is relative to the folder of the settings file.