Blitz3D+ Command Reference

JsonBoolean ( json )

Parameters

json - JSON handle of kind JSON_BOOLEAN

Description

Returns the value of a JSON Boolean: 1 for true, 0 for false.

This is the strict getter for flags read back from a save or settings file - difficulty toggles, "tutorial completed", "fullscreen". It accepts only JSON_BOOLEAN handles.

The getters in this family never guess: JsonBoolean will not read a 0/1 integer, parse a "true" string or substitute a default. Calling it on any other kind is a programming error that stops the program. When the data is untrusted - and save files always are - check JsonKind first, or verify the field exists with JsonObjectHas, and fall back to your own default when it is missing or the wrong kind.

Booleans are created with CreateJsonBoolean or parsed from the literals true and false.

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

Requires Extended mode.

See also: CreateJsonBoolean, JsonKind, JsonInteger, JsonNumber, JsonText.

Example

; JsonBoolean Example
; -------------------
; Requires Extended mode.

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

; Load a save-game that stores a difficulty flag
save=ParseJson("{"+q+"player"+q+":"+q+"Aria"+q+","+q+"hardcore"+q+":true}")

; Fetch the flag (child getters return an owned copy)
flag=JsonObjectGet(save,"hardcore")

; Check the kind before using a strict getter on untrusted data -
; JsonBoolean on a non-Boolean is a programming error
If JsonKind(flag)=JSON_BOOLEAN
    Print "hardcore flag = "+JsonBoolean(flag)
    If JsonBoolean(flag) Then Print "Permadeath is ON - good luck, Aria!"
Else
    Print "Save is corrupt: 'hardcore' is not a Boolean"
EndIf

FreeJson flag
FreeJson save

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

End

Index