Blitz3D+ Command Reference

CreateJsonObject ( )

Parameters

None.

Description

Creates an owned empty, insertion-ordered JSON object.

An object maps keys to values - the natural container for a save-game record, a settings block or a level descriptor. It starts empty; fill it with JsonObjectSet, read fields with JsonObjectGet, probe with JsonObjectHas, and walk it in order with JsonCount and JsonObjectKey.

Objects preserve insertion order: keys come back, and are serialized, in the order you added them, so your save files stay tidy and diff-friendly. Replacing an existing key keeps its position; removing a key and adding it again places it at the end. Keys are case-sensitive UTF-8 compared exactly, and duplicate keys cannot exist.

The setters copy their input value, so free each temporary handle right after setting it. The returned handle is owned by you: release it with FreeJson, and save the finished document with SaveJsonAtomic.

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

Requires Extended mode.

See also: JsonObjectSet, JsonObjectGet, JsonObjectKey, CreateJsonArray, SaveJsonAtomic, FreeJson.

Example

; CreateJsonObject Example
; ------------------------
; Requires Extended mode.

; CreateJsonObject makes an empty, insertion-ordered JSON object -
; the natural container for a save-game record.
save=CreateJsonObject()
Print "New object, fields: "+JsonCount(save)

; Fill in the save-game fields (setters copy their input,
; so each temporary handle is freed right after use)
v=CreateJsonString("Aria")
JsonObjectSet save,"player",v
FreeJson v

v=CreateJsonInteger(7L)
JsonObjectSet save,"level",v
FreeJson v

v=CreateJsonInteger(1250L)
JsonObjectSet save,"gold",v
FreeJson v

Print "After filling, fields: "+JsonCount(save)
Print ""

; Keys come back in the order they were inserted
Print "Finished save-game record:"
Print JsonStringify(save,2)

FreeJson save

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

End

Index