Don't crash on nil in get* procs in the JSON module.
This commit is contained in:
parent
da4b0d994d
commit
4a7c1d5fd7
1 changed files with 12 additions and 12 deletions
|
|
@ -608,29 +608,29 @@ proc newJArray*(): JsonNode =
|
||||||
proc getStr*(n: JsonNode, default: string = ""): string =
|
proc getStr*(n: JsonNode, default: string = ""): string =
|
||||||
## Retrieves the string value of a `JString JsonNode`.
|
## Retrieves the string value of a `JString JsonNode`.
|
||||||
##
|
##
|
||||||
## Returns ``default`` if ``n`` is not a ``JString``.
|
## Returns ``default`` if ``n`` is not a ``JString``, or if ``n`` is nil.
|
||||||
if n.kind != JString: return default
|
if n.isNil or n.kind != JString: return default
|
||||||
else: return n.str
|
else: return n.str
|
||||||
|
|
||||||
proc getNum*(n: JsonNode, default: BiggestInt = 0): BiggestInt =
|
proc getNum*(n: JsonNode, default: BiggestInt = 0): BiggestInt =
|
||||||
## Retrieves the int value of a `JInt JsonNode`.
|
## Retrieves the int value of a `JInt JsonNode`.
|
||||||
##
|
##
|
||||||
## Returns ``default`` if ``n`` is not a ``JInt``.
|
## Returns ``default`` if ``n`` is not a ``JInt``, or if ``n`` is nil.
|
||||||
if n.kind != JInt: return default
|
if n.isNil or n.kind != JInt: return default
|
||||||
else: return n.num
|
else: return n.num
|
||||||
|
|
||||||
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``.
|
## Returns ``default`` if ``n`` is not a ``JFloat``, or if ``n`` is nil.
|
||||||
if n.kind != JFloat: return default
|
if n.isNil or n.kind != JFloat: return default
|
||||||
else: return n.fnum
|
else: return n.fnum
|
||||||
|
|
||||||
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`.
|
||||||
##
|
##
|
||||||
## Returns ``default`` if ``n`` is not a ``JBool``.
|
## Returns ``default`` if ``n`` is not a ``JBool``, or if ``n`` is nil.
|
||||||
if n.kind != JBool: return default
|
if n.isNil or n.kind != JBool: return default
|
||||||
else: return n.bval
|
else: return n.bval
|
||||||
|
|
||||||
proc getFields*(n: JsonNode,
|
proc getFields*(n: JsonNode,
|
||||||
|
|
@ -638,15 +638,15 @@ proc getFields*(n: JsonNode,
|
||||||
seq[tuple[key: string, val: JsonNode]] =
|
seq[tuple[key: string, val: JsonNode]] =
|
||||||
## Retrieves the key, value pairs of a `JObject JsonNode`.
|
## Retrieves the key, value pairs of a `JObject JsonNode`.
|
||||||
##
|
##
|
||||||
## Returns ``default`` if ``n`` is not a ``JObject``.
|
## Returns ``default`` if ``n`` is not a ``JObject``, or if ``n`` is nil.
|
||||||
if n.kind != JObject: return default
|
if n.isNil or n.kind != JObject: return default
|
||||||
else: return n.fields
|
else: return n.fields
|
||||||
|
|
||||||
proc getElems*(n: JsonNode, default: seq[JsonNode] = @[]): seq[JsonNode] =
|
proc getElems*(n: JsonNode, default: seq[JsonNode] = @[]): seq[JsonNode] =
|
||||||
## Retrieves the int value of a `JArray JsonNode`.
|
## Retrieves the int value of a `JArray JsonNode`.
|
||||||
##
|
##
|
||||||
## Returns ``default`` if ``n`` is not a ``JArray``.
|
## Returns ``default`` if ``n`` is not a ``JArray``, or if ``n`` is nil.
|
||||||
if n.kind != JArray: return default
|
if n.isNil or n.kind != JArray: return default
|
||||||
else: return n.elems
|
else: return n.elems
|
||||||
|
|
||||||
proc `%`*(s: string): JsonNode =
|
proc `%`*(s: string): JsonNode =
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue