Blitz3D+ Command Reference

JsonArraySet json,index,value

Parameters

json - JSON array handle to modify

index - position of the element to replace, 0 to JsonCount(json)-1

value - JSON handle whose value is copied over the element

Description

Replaces an existing array element with a copy of a JSON value.

The in-place upgrade: the rusty sword in inventory slot 0 becomes a flame sword, a high-score entry is overwritten, a checkpoint in a list is updated. The array keeps its length and order - only the addressed element changes.

JsonArraySet copies the value, so the caller still owns the value handle afterwards and must free it separately; the usual pattern is create, set, free. Copies of the old element fetched earlier with JsonArrayGet stay valid - they are independent.

It can only replace, never grow: the index must address an existing element, and anything outside 0 to JsonCount(json)-1 is a programming error that stops the program. To add elements, use JsonArrayAppend or JsonArrayInsert. Calling JsonArraySet on a non-array or with a stale value handle is likewise a programming error. If the copy cannot be allocated, the array is left unchanged.

For the full rules, see the Application Data language reference.

Requires Extended mode.

See also: JsonArrayGet, JsonArrayAppend, JsonArrayInsert, JsonArrayRemove, FreeJson.

Example

; JsonArraySet Example
; --------------------
; Requires Extended mode.

q$=Chr(34)  ; Double-quote character, for building JSON text

; The hero's inventory before visiting the blacksmith
inventory=ParseJson("["+q+"sword"+q+","+q+"shield"+q+","+q+"potion"+q+"]")
Print "Before: "+JsonStringify(inventory,0)

; JsonArraySet copies the new value over an existing element -
; the rusty sword in slot 0 becomes a flame sword
v=CreateJsonString("flame sword")
JsonArraySet inventory,0,v
FreeJson v

Print "After:  "+JsonStringify(inventory,0)
Print ""
Print "Slot 0 was replaced in place; slots 1 and 2 are untouched."

FreeJson inventory

Print ""
Print "Press any key to close the example"
WaitKey

End

Index