Copy (Json function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
No edit summary
 
(One intermediate revision by the same user not shown)
Line 12: Line 12:
<ul>
<ul>
<li>The difference between a Copy and a [[DeepCopy (Json function)|DeepCopy]] 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.</li>
<li>The difference between a Copy and a [[DeepCopy (Json function)|DeepCopy]] 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.</li>
</ul>  
<li>The copy of a null object is, of course, null.</li>
</ul>
 
==Examples==
==Examples==
The following makes a 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 the update to the nested array is reflected in the copy because the copy simply copied the reference to that array. However, since the outermost array was actually copied, a change to the source array is not reflected in the target:
The following makes a 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 the update to the nested array is reflected in the copy because the copy simply copied the reference to that array. However, since the outermost array was actually copied, a change to the source array is not reflected in the target:
Line 28: Line 30:
<p class="code">%json=[12,13,25,["Moe","Larry","Curly","Shemp"],1.2020569031]
<p class="code">%json=[12,13,25,["Moe","Larry","Curly","Shemp"],1.2020569031]
%jsonCopy=[12,13,25,["Moe","Larry","Curly","Shemp"]]
%jsonCopy=[12,13,25,["Moe","Larry","Curly","Shemp"]]
</xmp>
</p>
 
==See also==
==See also==
{{Template:Json:Copy footer}}
{{Template:Json:Copy footer}}

Latest revision as of 21:58, 17 February 2015

Copy the JSON object (Json class)

[Introduced in Model 204 7.6]

This function creates a (shallow) copy of a Json object tree/forest.

Syntax

%outJson = json:Copy

Syntax terms

%jsonJson object
Json Json object, which may be Null

Usage notes

  • The difference between a Copy and a DeepCopy 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.

Examples

The following makes a 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 the update to the nested array is reflected in the copy because the copy simply copied the reference to that array. However, since the outermost array was actually copied, a change to the source array is not reflected in the target:

%json is object json %jsonCopy is object json %json = array(12, 13, 25, array("Moe", "Larry", "Curly")) %jsonCopy = %json:copy %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","Shemp"]]

See also