parent
f28dc2c61e
commit
cda443ee68
5 changed files with 43 additions and 57 deletions
|
|
@ -908,14 +908,13 @@ proc parseJson*(s: Stream, filename: string = ""; rawIntegers = false, rawFloats
|
||||||
|
|
||||||
when defined(js):
|
when defined(js):
|
||||||
from std/math import `mod`
|
from std/math import `mod`
|
||||||
import std/jsffi
|
from std/jsffi import JSObject, `[]`, to
|
||||||
|
from std/private/jsutils import getProtoName
|
||||||
|
|
||||||
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): JsonNodeKind =
|
||||||
result = JNull
|
result = JNull
|
||||||
proc getProtoName(y: JSObject): cstring
|
|
||||||
{.importjs: "Object.prototype.toString.call(#)".}
|
|
||||||
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
|
||||||
of "[object Object]": return JObject
|
of "[object Object]": return JObject
|
||||||
|
|
|
||||||
|
|
@ -36,3 +36,4 @@ when defined(js):
|
||||||
proc hasBigUint64Array*(): bool =
|
proc hasBigUint64Array*(): bool =
|
||||||
asm """`result` = typeof BigUint64Array != 'undefined'"""
|
asm """`result` = typeof BigUint64Array != 'undefined'"""
|
||||||
|
|
||||||
|
proc getProtoName*[T](a: T): cstring {.importjs: "Object.prototype.toString.call(#)".}
|
||||||
|
|
|
||||||
|
|
@ -1,22 +0,0 @@
|
||||||
discard """
|
|
||||||
targets: "c js"
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
import std/json
|
|
||||||
|
|
||||||
type
|
|
||||||
Foo = object
|
|
||||||
ii*: int
|
|
||||||
data*: JsonNode
|
|
||||||
|
|
||||||
block:
|
|
||||||
const jt = """{"ii": 123, "data": ["some", "data"]}"""
|
|
||||||
let js = parseJson(jt)
|
|
||||||
discard js.to(Foo)
|
|
||||||
|
|
||||||
block:
|
|
||||||
const jt = """{"ii": 123}"""
|
|
||||||
let js = parseJson(jt)
|
|
||||||
doAssertRaises(KeyError):
|
|
||||||
echo js.to(Foo)
|
|
||||||
|
|
@ -244,3 +244,43 @@ when defined(js): # xxx fixme
|
||||||
doAssert x.kind == JInt
|
doAssert x.kind == JInt
|
||||||
else:
|
else:
|
||||||
doAssert x.kind == JString
|
doAssert x.kind == JString
|
||||||
|
|
||||||
|
block: # bug #15835
|
||||||
|
type
|
||||||
|
Foo = object
|
||||||
|
ii*: int
|
||||||
|
data*: JsonNode
|
||||||
|
|
||||||
|
block:
|
||||||
|
const jt = """{"ii": 123, "data": ["some", "data"]}"""
|
||||||
|
let js = parseJson(jt)
|
||||||
|
discard js.to(Foo)
|
||||||
|
|
||||||
|
block:
|
||||||
|
const jt = """{"ii": 123}"""
|
||||||
|
let js = parseJson(jt)
|
||||||
|
doAssertRaises(KeyError):
|
||||||
|
echo js.to(Foo)
|
||||||
|
|
||||||
|
type
|
||||||
|
ContentNodeKind* = enum
|
||||||
|
P,
|
||||||
|
Br,
|
||||||
|
Text,
|
||||||
|
ContentNode* = object
|
||||||
|
case kind*: ContentNodeKind
|
||||||
|
of P: pChildren*: seq[ContentNode]
|
||||||
|
of Br: nil
|
||||||
|
of Text: textStr*: string
|
||||||
|
|
||||||
|
let mynode = ContentNode(kind: P, pChildren: @[
|
||||||
|
ContentNode(kind: Text, textStr: "mychild"),
|
||||||
|
ContentNode(kind: Br)
|
||||||
|
])
|
||||||
|
|
||||||
|
doAssert $mynode == """(kind: P, pChildren: @[(kind: Text, textStr: "mychild"), (kind: Br)])"""
|
||||||
|
|
||||||
|
let jsonNode = %*mynode
|
||||||
|
doAssert $jsonNode == """{"kind":"P","pChildren":[{"kind":"Text","textStr":"mychild"},{"kind":"Br"}]}"""
|
||||||
|
doAssert $jsonNode.to(ContentNode) == """(kind: P, pChildren: @[(kind: Text, textStr: "mychild"), (kind: Br)])"""
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,32 +0,0 @@
|
||||||
discard """
|
|
||||||
targets: "c js"
|
|
||||||
output: '''
|
|
||||||
Original: (kind: P, pChildren: @[(kind: Text, textStr: "mychild"), (kind: Br)])
|
|
||||||
jsonNode: {"kind":"P","pChildren":[{"kind":"Text","textStr":"mychild"},{"kind":"Br"}]}
|
|
||||||
Reversed: (kind: P, pChildren: @[(kind: Text, textStr: "mychild"), (kind: Br)])
|
|
||||||
'''
|
|
||||||
"""
|
|
||||||
|
|
||||||
import std/json
|
|
||||||
|
|
||||||
type
|
|
||||||
ContentNodeKind* = enum
|
|
||||||
P,
|
|
||||||
Br,
|
|
||||||
Text,
|
|
||||||
ContentNode* = object
|
|
||||||
case kind*: ContentNodeKind
|
|
||||||
of P: pChildren*: seq[ContentNode]
|
|
||||||
of Br: nil
|
|
||||||
of Text: textStr*: string
|
|
||||||
|
|
||||||
let mynode = ContentNode(kind: P, pChildren: @[
|
|
||||||
ContentNode(kind: Text, textStr: "mychild"),
|
|
||||||
ContentNode(kind: Br)
|
|
||||||
])
|
|
||||||
|
|
||||||
echo "Original: " & $mynode
|
|
||||||
|
|
||||||
let jsonNode = %*mynode
|
|
||||||
echo "jsonNode: " & $jsonNode
|
|
||||||
echo "Reversed: " & $jsonNode.to(ContentNode)
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue