JsonObjectHas ( json,key$ )
Parameters
|
json - JSON object handle key$ - property name to look for, exact and case-sensitive |
Description
|
Returns true if an object has a property with the given key. The cheap way to probe a save file for an optional field before reading it: no copy is created, no handle needs freeing, and the JSON diagnostics are left untouched whether the key exists or not. Ideal for version checks ("does this old save have the new mount field yet?") and for guarding strict getters. Keys are strict UTF-8, case-sensitive, and compared by exact byte sequence with no normalization - "Gold" and "gold" are different keys. Since a JSON object can never hold duplicate keys, the answer is a simple yes or no. JsonObjectHas only answers existence. To fetch the value, use JsonObjectGet - and note that a field can exist and still be JSON null, which is exactly the "exists but deliberately empty" state that JsonIsNull detects. Calling JsonObjectHas on a non-object is a programming error. For the full rules, see the Application Data language reference. Requires Extended mode. See also: JsonObjectGet, JsonObjectSet, JsonObjectKey, JsonIsNull, CreateJsonObject. |
Example
; JsonObjectHas Example ; --------------------- ; Requires Extended mode. q$=Chr(34) ; Double-quote character, for building JSON text ; A loaded save-game record save=ParseJson("{"+q+"player"+q+":"+q+"Aria"+q+","+q+"gold"+q+":1250}") Print "Save-game: "+JsonStringify(save,0) Print "" ; JsonObjectHas tests for an exact key without creating a copy ; and without disturbing JsonError when the key is absent Print "Has 'player'? "+JsonObjectHas(save,"player") Print "Has 'gold'? "+JsonObjectHas(save,"gold") Print "Has 'mount'? "+JsonObjectHas(save,"mount") Print "" ; Typical use: probe before reading an optional field If JsonObjectHas(save,"mount") Print "Loading the mount..." Else Print "No mount saved - Aria travels on foot." EndIf FreeJson save Print "" Print "Press any key to close the example" WaitKey End
Index