summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/json.nim22
-rw-r--r--lib/std/jsonutils.nim4
2 files changed, 21 insertions, 5 deletions
diff --git a/lib/pure/json.nim b/lib/pure/json.nim
index 51d4bb815..5feb81457 100644
--- a/lib/pure/json.nim
+++ b/lib/pure/json.nim
@@ -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`.
@@ -1055,8 +1061,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)
diff --git a/lib/std/jsonutils.nim b/lib/std/jsonutils.nim
index bbe6a7cd4..fa61d79db 100644
--- a/lib/std/jsonutils.nim
+++ b/lib/std/jsonutils.nim
@@ -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