mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-19 22:10:33 +00:00
fix #17383: json.%,to and jsonutils.formJson,toJson now works with uint|uint64 (#17389) [backport:1.2]
* fix #17383: json.%,to and jsonutils.formJson,toJson now works with uint|uint64
* fixup
* fix for js
(cherry picked from commit 895a40d1ac)
This commit is contained in:
@@ -304,7 +304,10 @@ proc `%`*(s: string): JsonNode =
|
||||
|
||||
proc `%`*(n: uint): JsonNode =
|
||||
## Generic constructor for JSON data. Creates a new `JInt JsonNode`.
|
||||
result = JsonNode(kind: JInt, num: BiggestInt(n))
|
||||
if n > cast[uint](int.high):
|
||||
result = newJRawNumber($n)
|
||||
else:
|
||||
result = JsonNode(kind: JInt, num: BiggestInt(n))
|
||||
|
||||
proc `%`*(n: int): JsonNode =
|
||||
## Generic constructor for JSON data. Creates a new `JInt JsonNode`.
|
||||
@@ -312,7 +315,10 @@ proc `%`*(n: int): JsonNode =
|
||||
|
||||
proc `%`*(n: BiggestUInt): JsonNode =
|
||||
## Generic constructor for JSON data. Creates a new `JInt JsonNode`.
|
||||
result = JsonNode(kind: JInt, num: BiggestInt(n))
|
||||
if n > cast[BiggestUInt](BiggestInt.high):
|
||||
result = newJRawNumber($n)
|
||||
else:
|
||||
result = JsonNode(kind: JInt, num: BiggestInt(n))
|
||||
|
||||
proc `%`*(n: BiggestInt): JsonNode =
|
||||
## Generic constructor for JSON data. Creates a new `JInt JsonNode`.
|
||||
@@ -1058,8 +1064,16 @@ when defined(nimFixedForwardGeneric):
|
||||
dst = jsonNode.copy
|
||||
|
||||
proc initFromJson[T: SomeInteger](dst: var T; jsonNode: JsonNode, jsonPath: var string) =
|
||||
verifyJsonKind(jsonNode, {JInt}, jsonPath)
|
||||
dst = T(jsonNode.num)
|
||||
when T is uint|uint64:
|
||||
case jsonNode.kind
|
||||
of JString:
|
||||
dst = T(parseBiggestUInt(jsonNode.str))
|
||||
else:
|
||||
verifyJsonKind(jsonNode, {JInt}, jsonPath)
|
||||
dst = T(jsonNode.num)
|
||||
else:
|
||||
verifyJsonKind(jsonNode, {JInt}, jsonPath)
|
||||
dst = cast[T](jsonNode.num)
|
||||
|
||||
proc initFromJson[T: SomeFloat](dst: var T; jsonNode: JsonNode; jsonPath: var string) =
|
||||
verifyJsonKind(jsonNode, {JInt, JFloat}, jsonPath)
|
||||
|
||||
@@ -187,7 +187,8 @@ proc fromJson*[T](a: var T, b: JsonNode, opt = Joptions()) =
|
||||
of JInt: a = T(b.getBiggestInt())
|
||||
of JString: a = parseEnum[T](b.getStr())
|
||||
else: checkJson false, $($T, " ", b)
|
||||
elif T is Ordinal: a = T(to(b, int))
|
||||
elif T is uint|uint64: a = T(to(b, uint64))
|
||||
elif T is Ordinal: a = cast[T](to(b, int))
|
||||
elif T is pointer: a = cast[pointer](to(b, int))
|
||||
elif T is distinct:
|
||||
when nimvm:
|
||||
@@ -270,6 +271,7 @@ proc toJson*[T](a: T): JsonNode =
|
||||
# in simpler code for `toJson` and `fromJson`.
|
||||
elif T is distinct: result = toJson(a.distinctBase)
|
||||
elif T is bool: result = %(a)
|
||||
elif T is SomeInteger: result = %a
|
||||
elif T is Ordinal: result = %(a.ord)
|
||||
else: result = %a
|
||||
|
||||
|
||||
@@ -2,7 +2,14 @@
|
||||
Note: Macro tests are in tests/stdlib/tjsonmacro.nim
|
||||
]#
|
||||
|
||||
import std/[json,parsejson,strutils,streams]
|
||||
import std/[json,parsejson,strutils]
|
||||
when not defined(js):
|
||||
import std/streams
|
||||
|
||||
proc testRoundtrip[T](t: T, expected: string) =
|
||||
let j = %t
|
||||
doAssert $j == expected, $j
|
||||
doAssert %(j.to(T)) == j
|
||||
|
||||
let testJson = parseJson"""{ "a": [1, 2, 3, 4], "b": "asd", "c": "\ud83c\udf83", "d": "\u00E6"}"""
|
||||
# nil passthrough
|
||||
@@ -235,3 +242,16 @@ doAssert isRefSkipDistinct(MyOtherDistinct)
|
||||
|
||||
let x = parseJson("9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999")
|
||||
doAssert x.kind == JString
|
||||
|
||||
block: # bug #17383
|
||||
testRoundtrip(int32.high): "2147483647"
|
||||
testRoundtrip(uint32.high): "4294967295"
|
||||
when int.sizeof == 4:
|
||||
testRoundtrip(int.high): "2147483647"
|
||||
testRoundtrip(uint.high): "4294967295"
|
||||
else:
|
||||
testRoundtrip(int.high): "9223372036854775807"
|
||||
testRoundtrip(uint.high): "18446744073709551615"
|
||||
when not defined(js):
|
||||
testRoundtrip(int64.high): "9223372036854775807"
|
||||
testRoundtrip(uint64.high): "18446744073709551615"
|
||||
|
||||
@@ -68,6 +68,21 @@ template fn() =
|
||||
doAssert b2.ord == 1 # explains the `1`
|
||||
testRoundtrip(a): """[1,2,3]"""
|
||||
|
||||
block: # bug #17383
|
||||
block:
|
||||
let a = (int32.high, uint32.high)
|
||||
testRoundtrip(a): "[2147483647,4294967295]"
|
||||
when not defined(js):
|
||||
block:
|
||||
let a = (int64.high, uint64.high)
|
||||
testRoundtrip(a): "[9223372036854775807,18446744073709551615]"
|
||||
block:
|
||||
let a = (int.high, uint.high)
|
||||
when int.sizeof == 4:
|
||||
testRoundtrip(a): "[2147483647,4294967295]"
|
||||
else:
|
||||
testRoundtrip(a): "[9223372036854775807,18446744073709551615]"
|
||||
|
||||
block: # case object
|
||||
type Foo = object
|
||||
x0: float
|
||||
|
||||
Reference in New Issue
Block a user