Merge pull request #3337 from yglukhov/json-coerce-int-to-float
Transparently coerce int to float in JsonNode.getFNum
This commit is contained in:
commit
432884861e
1 changed files with 6 additions and 3 deletions
|
|
@ -622,9 +622,12 @@ proc getNum*(n: JsonNode, default: BiggestInt = 0): BiggestInt =
|
||||||
proc getFNum*(n: JsonNode, default: float = 0.0): float =
|
proc getFNum*(n: JsonNode, default: float = 0.0): float =
|
||||||
## Retrieves the float value of a `JFloat JsonNode`.
|
## Retrieves the float value of a `JFloat JsonNode`.
|
||||||
##
|
##
|
||||||
## Returns ``default`` if ``n`` is not a ``JFloat``, or if ``n`` is nil.
|
## Returns ``default`` if ``n`` is not a ``JFloat`` or ``JInt``, or if ``n`` is nil.
|
||||||
if n.isNil or n.kind != JFloat: return default
|
if n.isNil: return default
|
||||||
else: return n.fnum
|
case n.kind
|
||||||
|
of JFloat: return n.fnum
|
||||||
|
of JInt: return float(n.num)
|
||||||
|
else: return default
|
||||||
|
|
||||||
proc getBVal*(n: JsonNode, default: bool = false): bool =
|
proc getBVal*(n: JsonNode, default: bool = false): bool =
|
||||||
## Retrieves the bool value of a `JBool JsonNode`.
|
## Retrieves the bool value of a `JBool JsonNode`.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue