DeepCopy (Json function)

From m204wiki
Revision as of 22:04, 17 February 2015 by Alex (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Copy the JSON object (Json class)

[Introduced in Model 204 7.6]

This function creates a deep copy of a Json object tree/forest. A deep copy is one where not only are the contents of the object copied but all objects referenced by the objects and all objects referenced by those and so on.

Syntax

%outJson = json:DeepCopy

Syntax terms

%jsonJson object
Json Json object, which may be Null

Usage notes

  • The difference between a DeepCopy and a Copy is that a Copy of an array or object Json object only copies the references in the array or object and does not copy the objects referenced by the array or object, but a DeepCopy also copies referenced objects and objects they reference and so on.
  • The copy of a null object is, of course, null.
  • When a Json object is passed between a daemon and master or vice versa, the Json object is deep copied.

Examples

The following makes a deep copy of an array and the updates the array and an array referenced by the array, printing the values of the source and copy arrays. Note that neither the update to outer nor the nested array are reflected in the copy because the deep copy made copies of all objects in the tree so the %jsonCopy tree is totally independent of the %json tree. Contrast this with the nearly identical example for Copy:

%json is object json %jsonCopy is object json %json = array(12, 13, 25, array("Moe", "Larry", "Curly")) %jsonCopy = %json:deepCopy %json:add(1.2020569031) %json(4):add("Shemp") printText {~=%json} printText {~=%jsonCopy}

This prints:

%json=[12,13,25,["Moe","Larry","Curly","Shemp"],1.2020569031] %jsonCopy=[12,13,25,["Moe","Larry","Curly"]]

See also