parent
8ccde68f13
commit
c015ecdc37
2 changed files with 21 additions and 4 deletions
|
|
@ -927,7 +927,7 @@ when defined(js):
|
||||||
|
|
||||||
proc parseNativeJson(x: cstring): JSObject {.importjs: "JSON.parse(#)".}
|
proc parseNativeJson(x: cstring): JSObject {.importjs: "JSON.parse(#)".}
|
||||||
|
|
||||||
proc getVarType(x: JSObject): JsonNodeKind =
|
proc getVarType(x: JSObject, isRawNumber: var bool): JsonNodeKind =
|
||||||
result = JNull
|
result = JNull
|
||||||
case $getProtoName(x) # TODO: Implicit returns fail here.
|
case $getProtoName(x) # TODO: Implicit returns fail here.
|
||||||
of "[object Array]": return JArray
|
of "[object Array]": return JArray
|
||||||
|
|
@ -937,6 +937,7 @@ when defined(js):
|
||||||
if isSafeInteger(x):
|
if isSafeInteger(x):
|
||||||
return JInt
|
return JInt
|
||||||
else:
|
else:
|
||||||
|
isRawNumber = true
|
||||||
return JString
|
return JString
|
||||||
else:
|
else:
|
||||||
return JFloat
|
return JFloat
|
||||||
|
|
@ -946,13 +947,13 @@ when defined(js):
|
||||||
else: assert false
|
else: assert false
|
||||||
|
|
||||||
proc len(x: JSObject): int =
|
proc len(x: JSObject): int =
|
||||||
assert x.getVarType == JArray
|
|
||||||
asm """
|
asm """
|
||||||
`result` = `x`.length;
|
`result` = `x`.length;
|
||||||
"""
|
"""
|
||||||
|
|
||||||
proc convertObject(x: JSObject): JsonNode =
|
proc convertObject(x: JSObject): JsonNode =
|
||||||
case getVarType(x)
|
var isRawNumber = false
|
||||||
|
case getVarType(x, isRawNumber)
|
||||||
of JArray:
|
of JArray:
|
||||||
result = newJArray()
|
result = newJArray()
|
||||||
for i in 0 ..< x.len:
|
for i in 0 ..< x.len:
|
||||||
|
|
@ -973,7 +974,12 @@ when defined(js):
|
||||||
result = newJFloat(x.to(float))
|
result = newJFloat(x.to(float))
|
||||||
of JString:
|
of JString:
|
||||||
# Dunno what to do with isUnquoted here
|
# Dunno what to do with isUnquoted here
|
||||||
result = newJString($x.to(cstring))
|
if isRawNumber:
|
||||||
|
var value: cstring
|
||||||
|
{.emit: "`value` = `x`.toString();".}
|
||||||
|
result = newJRawNumber($value)
|
||||||
|
else:
|
||||||
|
result = newJString($x.to(cstring))
|
||||||
of JBool:
|
of JBool:
|
||||||
result = newJBool(x.to(bool))
|
result = newJBool(x.to(bool))
|
||||||
of JNull:
|
of JNull:
|
||||||
|
|
|
||||||
|
|
@ -300,3 +300,14 @@ block: # bug #17383
|
||||||
when not defined(js):
|
when not defined(js):
|
||||||
testRoundtrip(int64.high): "9223372036854775807"
|
testRoundtrip(int64.high): "9223372036854775807"
|
||||||
testRoundtrip(uint64.high): "18446744073709551615"
|
testRoundtrip(uint64.high): "18446744073709551615"
|
||||||
|
|
||||||
|
|
||||||
|
block:
|
||||||
|
let a = "18446744073709551615"
|
||||||
|
let b = a.parseJson
|
||||||
|
doAssert b.kind == JString
|
||||||
|
let c = $b
|
||||||
|
when defined(js):
|
||||||
|
doAssert c == "18446744073709552000"
|
||||||
|
else:
|
||||||
|
doAssert c == "18446744073709551615"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue