jsony icon indicating copy to clipboard operation
jsony copied to clipboard

Makes all `parseHook`s and `dumpHook`s available to all others [fix]

Open hoijui opened this issue 2 years ago • 2 comments

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.

hoijui avatar Nov 30 '23 16:11 hoijui

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?

hoijui avatar Dec 01 '23 11:12 hoijui

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.

treeform avatar Dec 09 '23 15:12 treeform