| json - JSON handle to copy |
|
Returns a fully independent deep copy of a JSON value as a new owned handle. The copy shares nothing with the original: mutate one, free one, and the other is completely unaffected. That makes CopyJson the tool for backup slots, undo snapshots, and "compare against the last saved state" checks - copy the save-game document before the player starts editing, then use JsonEqual later to see whether anything changed. Both handles are owned by you afterwards, and each must be released separately with FreeJson. Copying counts toward the per-context limits (4095 live handles, memory accounting), so avoid copying a huge document every frame. You rarely need CopyJson just to pass a value into a container: JsonObjectSet, JsonArraySet and JsonArrayAppend already copy their input, and JsonObjectGet / JsonArrayGet already return copies. For the full rules, see the Application Data language reference. See also: FreeJson, JsonEqual, ParseJson, JsonObjectSet. |
; CopyJson Example
; ----------------
q$=Chr(34) ; Double-quote character, for building JSON text
; The current save-game
save=ParseJson("{"+q+"player"+q+":"+q+"Aria"+q+","+q+"gold"+q+":1250}")
; CopyJson makes a fully independent deep copy - a backup slot
backup=CopyJson(save)
; Spend some gold in the live save only
v=CreateJsonInteger(200L)
JsonObjectSet save,"gold",v
FreeJson v
Print "Live save: "+JsonStringify(save,0)
Print "Backup copy: "+JsonStringify(backup,0)
Print ""
Print "The backup kept gold=1250 - the copy shares nothing with the original."
; Both handles are owned and must be freed separately
FreeJson backup
FreeJson save
Print ""
Print "Press any key to close the example"
WaitKey
End