This commit is contained in:
flywind 2020-11-02 17:22:26 +08:00 • committed by GitHub
commit 919593395c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 1 deletions

View file

@ -1120,6 +1120,9 @@ when defined(nimFixedForwardGeneric):
proc initFromJson[T](dst: var Option[T]; jsonNode: JsonNode; jsonPath: var string) = proc initFromJson[T](dst: var Option[T]; jsonNode: JsonNode; jsonPath: var string) =
if jsonNode != nil and jsonNode.kind != JNull: if jsonNode != nil and jsonNode.kind != JNull:
when T is ref:
dst = some(new(T))
else:
dst = some(default(T)) dst = some(default(T))
initFromJson(dst.get, jsonNode, jsonPath) initFromJson(dst.get, jsonNode, jsonPath)

16
tests/stdlib/toptions.nim Normal file
View file

@ -0,0 +1,16 @@
discard """
output: '''{"foo":{"test":"123"}}'''
"""
import json, options
type
Foo = ref object
test: string
Test = object
foo: Option[Foo]
let js = """{"foo": {"test": "123"}}"""
let parsed = parseJson(js)
let a = parsed.to(Test)
echo $(%*a)