jsony
jsony copied to clipboard
Makes all `parseHook`s and `dumpHook`s available to all others [fix]
This fixes an issue for me where Option was tried
to be parsed as something else (probably object?),
at the very least.
They are now in the same order as the implementations further down in the code.
This fails without these changes:
type Entry = object
msg: Option[string]
let expected = Entry( msg: some("hello") )
doAssert jsony.fromJson("{\"msg\": \"hello\"}", Entry) == expected
type RefEntry = ref object
msg: Option[string]
let expectedRef = RefEntry( msg: some("hello") )
doAssert jsony.fromJson("{\"msg\": \"hello\"}", RefEntry) == expectedRef
Should we/I put it into tests/test_options.nim?
Your test case has a bug. Ref objects can only == to each other if they are the same object as it does a ptr comparison.
I tried to fix it here:
type Entry2 = object
msg: Option[string]
let expected = Entry2( msg: some("hello") )
doAssert jsony.fromJson("{\"msg\": \"hello\"}", Entry2) == expected
type RefEntry = ref object
msg: Option[string]
let expectedRef = RefEntry( msg: some("hello") )
let secondRef = jsony.fromJson("{\"msg\": \"hello\"}", RefEntry)
doAssert secondRef.msg.isSome()
doAssert secondRef.msg.get() == expectedRef.msg.get()
And that passes with current jsony with no modifications.