[JSON] getBVal -> getBool; getFNum -> getFloat
This commit is contained in:
parent
1dc6caea6b
commit
e29e6ec76e
5 changed files with 30 additions and 15 deletions
|
|
@ -8,3 +8,5 @@
|
||||||
- Arrays of char cannot be converted to ``cstring`` anymore, pointers to
|
- Arrays of char cannot be converted to ``cstring`` anymore, pointers to
|
||||||
arrays of char can! This means ``$`` for arrays can finally exist
|
arrays of char can! This means ``$`` for arrays can finally exist
|
||||||
in ``system.nim`` and do the right thing.
|
in ``system.nim`` and do the right thing.
|
||||||
|
- JSON: Deprecated `getBVal`, `getFNum`, and `getNum` in favour to
|
||||||
|
`getBool`, `getFloat`, `getBiggestInt`. Also `getInt` procedure was added.
|
||||||
|
|
@ -37,15 +37,15 @@
|
||||||
## Retrieving the value of a JSON node can then be achieved using one of the
|
## Retrieving the value of a JSON node can then be achieved using one of the
|
||||||
## helper procedures, which include:
|
## helper procedures, which include:
|
||||||
##
|
##
|
||||||
## * ``getNum``
|
## * ``getInt``
|
||||||
## * ``getFNum``
|
## * ``getFloat``
|
||||||
## * ``getStr``
|
## * ``getStr``
|
||||||
## * ``getBVal``
|
## * ``getBool``
|
||||||
##
|
##
|
||||||
## To retrieve the value of ``"key"`` you can do the following:
|
## To retrieve the value of ``"key"`` you can do the following:
|
||||||
##
|
##
|
||||||
## .. code-block:: Nim
|
## .. code-block:: Nim
|
||||||
## doAssert jsonNode["key"].getFNum() == 3.14
|
## doAssert jsonNode["key"].getFloat() == 3.14
|
||||||
##
|
##
|
||||||
## The ``[]`` operator will raise an exception when the specified field does
|
## The ``[]`` operator will raise an exception when the specified field does
|
||||||
## not exist. If you wish to avoid this behaviour you can use the ``{}``
|
## not exist. If you wish to avoid this behaviour you can use the ``{}``
|
||||||
|
|
@ -681,14 +681,23 @@ proc getStr*(n: JsonNode, default: string = ""): string =
|
||||||
if n.isNil or 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 getInt*(n: JsonNode, default: int = 0): int =
|
||||||
## Retrieves the int value of a `JInt JsonNode`.
|
## Retrieves the int value of a `JInt JsonNode`.
|
||||||
##
|
##
|
||||||
## Returns ``default`` if ``n`` is not a ``JInt``, or if ``n`` is nil.
|
## Returns ``default`` if ``n`` is not a ``JInt``, or if ``n`` is nil.
|
||||||
if n.isNil or n.kind != JInt: return default
|
if n.isNil or n.kind != JInt: return default
|
||||||
|
else: return int(n.num)
|
||||||
|
|
||||||
|
proc getBiggestInt*(n: JsonNode, default: BiggestInt = 0): BiggestInt =
|
||||||
|
## Retrieves the BiggestInt value of a `JInt JsonNode`.
|
||||||
|
##
|
||||||
|
## Returns ``default`` if ``n`` is not a ``JInt``, or if ``n`` is nil.
|
||||||
|
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 =
|
{.deprecated: [getNum: getBiggestInt].}
|
||||||
|
|
||||||
|
proc getFloat*(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 ``JInt``, or if ``n`` is nil.
|
## Returns ``default`` if ``n`` is not a ``JFloat`` or ``JInt``, or if ``n`` is nil.
|
||||||
|
|
@ -698,13 +707,17 @@ proc getFNum*(n: JsonNode, default: float = 0.0): float =
|
||||||
of JInt: return float(n.num)
|
of JInt: return float(n.num)
|
||||||
else: return default
|
else: return default
|
||||||
|
|
||||||
proc getBVal*(n: JsonNode, default: bool = false): bool =
|
{.deprecated: [getFNum: getFloat].}
|
||||||
|
|
||||||
|
proc getBool*(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``, or if ``n`` is nil.
|
## Returns ``default`` if ``n`` is not a ``JBool``, or if ``n`` is nil.
|
||||||
if n.isNil or n.kind != JBool: return default
|
if n.isNil or n.kind != JBool: return default
|
||||||
else: return n.bval
|
else: return n.bval
|
||||||
|
|
||||||
|
{.deprecated: [getBVal: getBool].}
|
||||||
|
|
||||||
proc getFields*(n: JsonNode,
|
proc getFields*(n: JsonNode,
|
||||||
default = initOrderedTable[string, JsonNode](4)):
|
default = initOrderedTable[string, JsonNode](4)):
|
||||||
OrderedTable[string, JsonNode] =
|
OrderedTable[string, JsonNode] =
|
||||||
|
|
@ -1342,7 +1355,7 @@ proc getEnum(node: JsonNode, ast: string, T: typedesc): T =
|
||||||
# TODO: I shouldn't need this proc.
|
# TODO: I shouldn't need this proc.
|
||||||
proc convert[T](x: BiggestInt): T = T(x)
|
proc convert[T](x: BiggestInt): T = T(x)
|
||||||
verifyJsonKind(node, {JInt}, ast)
|
verifyJsonKind(node, {JInt}, ast)
|
||||||
return convert[T](node.getNum())
|
return convert[T](node.getBiggestInt())
|
||||||
else:
|
else:
|
||||||
verifyJsonKind(node, {JString}, ast)
|
verifyJsonKind(node, {JString}, ast)
|
||||||
return parseEnum[T](node.getStr())
|
return parseEnum[T](node.getStr())
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,7 @@ proc createLoadProc(typeName: NimIdent, identDefs: seq[NimNode]): NimNode =
|
||||||
`cfgIdent`.`fieldNameIdent` = `objIdent`[`fieldName`].getStr
|
`cfgIdent`.`fieldNameIdent` = `objIdent`[`fieldName`].getStr
|
||||||
of "int":
|
of "int":
|
||||||
body.add quote do:
|
body.add quote do:
|
||||||
`cfgIdent`.`fieldNameIdent` = `objIdent`[`fieldName`].getNum().int
|
`cfgIdent`.`fieldNameIdent` = `objIdent`[`fieldName`].getInt().int
|
||||||
else:
|
else:
|
||||||
doAssert(false, "Not Implemented")
|
doAssert(false, "Not Implemented")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -87,8 +87,8 @@ when isMainModule:
|
||||||
|
|
||||||
result = to(node, TestVariant)
|
result = to(node, TestVariant)
|
||||||
doAssert result.name == node["name"].getStr()
|
doAssert result.name == node["name"].getStr()
|
||||||
doAssert result.age == node["age"].getNum().uint8
|
doAssert result.age == node["age"].getInt().uint8
|
||||||
doAssert result.other == node["other"].getNum()
|
doAssert result.other == node["other"].getBiggestInt()
|
||||||
|
|
||||||
# TODO: Test object variant with set in of branch.
|
# TODO: Test object variant with set in of branch.
|
||||||
# TODO: Should we support heterogenous arrays?
|
# TODO: Should we support heterogenous arrays?
|
||||||
|
|
|
||||||
|
|
@ -53,7 +53,7 @@ proc processSupporters(supporters: JsonNode) =
|
||||||
echo("Found ", supporters.elems.len, " named sponsors.")
|
echo("Found ", supporters.elems.len, " named sponsors.")
|
||||||
|
|
||||||
supporters.elems.sort(
|
supporters.elems.sort(
|
||||||
(x, y) => cmp(y["alltime_amount"].getFNum, x["alltime_amount"].getFNum)
|
(x, y) => cmp(y["alltime_amount"].getFloat, x["alltime_amount"].getFloat)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -113,7 +113,7 @@ when isMainModule:
|
||||||
if url.len > 0 and not url.startsWith("http"):
|
if url.len > 0 and not url.startsWith("http"):
|
||||||
url = "http://" & url
|
url = "http://" & url
|
||||||
|
|
||||||
let amount = supporter["monthly_amount"].getFNum()
|
let amount = supporter["monthly_amount"].getFloat()
|
||||||
# Only show URL when user donated at least $5.
|
# Only show URL when user donated at least $5.
|
||||||
if amount < 5:
|
if amount < 5:
|
||||||
url = ""
|
url = ""
|
||||||
|
|
@ -126,10 +126,10 @@ when isMainModule:
|
||||||
discard # TODO
|
discard # TODO
|
||||||
|
|
||||||
let sponsor = Sponsor(name: name, url: url, logo: logo, amount: amount,
|
let sponsor = Sponsor(name: name, url: url, logo: logo, amount: amount,
|
||||||
allTime: supporter["alltime_amount"].getFNum(),
|
allTime: supporter["alltime_amount"].getFloat(),
|
||||||
since: parse(supporter["created_at"].getStr, "yyyy-MM-dd'T'hh:mm:ss")
|
since: parse(supporter["created_at"].getStr, "yyyy-MM-dd'T'hh:mm:ss")
|
||||||
)
|
)
|
||||||
if supporter["monthly_amount"].getFNum > 0.0:
|
if supporter["monthly_amount"].getFloat > 0.0:
|
||||||
activeSponsors.add(sponsor)
|
activeSponsors.add(sponsor)
|
||||||
else:
|
else:
|
||||||
inactiveSponsors.add(sponsor)
|
inactiveSponsors.add(sponsor)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue