Blitz3D+ Command Reference

JsonNumber:Double ( json )

Parameters

json - JSON handle of kind JSON_NUMBER or JSON_INTEGER

Description

Returns a JSON number as a Double; also accepts a JSON integer, converting it.

The getter for fractional values read back from a save file - play-time in hours, multipliers, lap times. It is the one deliberately flexible getter in the family: because JSON writers disagree about whether 7 is "an integer" or "a number", JsonNumber accepts both JSON_NUMBER and JSON_INTEGER, so numeric fields read cleanly either way.

Reading an integer converts Long to Double using the language's standard conversion - exact up to 2^53, after which huge values can round. When you need the exact 64-bit value of a JSON_INTEGER, use JsonInteger instead.

Every other kind - strings, Booleans, null, containers - is a programming error that stops the program; no getter in this family parses a numeric string or substitutes a default. Check JsonKind first on untrusted data.

Numbers are created with CreateJsonNumber or parsed from tokens with a decimal point or exponent.

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

Requires Extended mode.

See also: CreateJsonNumber, JsonInteger, JsonKind, JsonBoolean, JsonText.

Example

; JsonNumber Example
; ------------------
; Requires Extended mode.

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

; A save-game with a fractional play-time and a whole-number level
save=ParseJson("{"+q+"play_time_hours"+q+":12.75,"+q+"level"+q+":7}")

; JsonNumber returns a JSON_NUMBER as a Double
hours=JsonObjectGet(save,"play_time_hours")
Print "play_time_hours kind is JSON_NUMBER: "+(JsonKind(hours)=JSON_NUMBER)
Print "JsonNumber(hours) = "+JsonNumber(hours)
Print ""

; It also accepts a JSON_INTEGER, converting Long to Double
level=JsonObjectGet(save,"level")
Print "level kind is JSON_INTEGER: "+(JsonKind(level)=JSON_INTEGER)
Print "JsonNumber(level) = "+JsonNumber(level)+" (converted from integer)"

FreeJson level
FreeJson hours
FreeJson save

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

End

Index