Copy (Json function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
(Automatically generated page update)
 
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{Template:Json:Copy subtitle}}
{{Template:Json:Copy subtitle}}
 
This function creates a (shallow) copy of a Json object tree/forest.
This page is [[under construction]].
==Syntax==
==Syntax==
{{Template:Json:Copy syntax}}
{{Template:Json:Copy syntax}}
Line 11: Line 10:
</table>
</table>
==Usage notes==
==Usage notes==
<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 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:
<p class="code">%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}
</p>
This prints:
<p class="code">%json=[12,13,25,["Moe","Larry","Curly","Shemp"],1.2020569031]
%jsonCopy=[12,13,25,["Moe","Larry","Curly","Shemp"]]
</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