You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Dec 15, 2025. It is now read-only.
Repository navigation
This repository was archived by the owner on Dec 15, 2025. It is now read-only.
Unable to unmarshal date using json-iterator. #313
I am trying to use a custom struct which implements the json.Unmarshaler interface as a key in a map(map[MyCustomType]bool). This fails when I am trying to use jsoniter but works correctly if I use the standard library json package. Minimal code example to reproduce the issue:
@aafreensheikh96 wow, jsoniter doesn't call Date.UnmarshalJSON at all, whereas encoding/json calls it three times, as expected.
(dlv) bp
Breakpoint unrecovered-panic at 0x102e760 for runtime.fatalpanic() /usr/local/Cellar/go/1.11.1/libexec/src/runtime/panic.go:681 (1)
print runtime.curg._panic.arg
Breakpoint 1 at 0x11b8a3b for main.(*Date).UnmarshalJSON() ./main.go:15 (3)
Breakpoint jsoniter at 0x11b908d for main.main() ./main.go:45 (1)
Using Delve with your main.go in the current directory:
$ dlv debug
Type 'help' for list of commands.
(dlv) break Date.UnmarshalJSON
Breakpoint 1 set at 0x11b8a3b for main.(*Date).UnmarshalJSON() ./main.go:15
(dlv) break jsoniter main.go:45
Breakpoint jsoniter set at 0x11b908d for main.main() ./main.go:45
Then running it:
(dlv) continue
> main.(*Date).UnmarshalJSON() ./main.go:15 (hits goroutine(1):1 total:1) (PC: 0x11b8a3b)
10:
11: type Date struct {
12: time.Time
13: }
14:
=> 15: func (d *Date) UnmarshalJSON(b []byte) error {
16: dateStr := string(b) // something like `"2017-08-20"`
17:
18: if dateStr == "null" {
19: return nil
20: }
(dlv) continue
> main.(*Date).UnmarshalJSON() ./main.go:15 (hits goroutine(1):2 total:2) (PC: 0x11b8a3b)
10:
11: type Date struct {
12: time.Time
13: }
14:
=> 15: func (d *Date) UnmarshalJSON(b []byte) error {
16: dateStr := string(b) // something like `"2017-08-20"`
17:
18: if dateStr == "null" {
19: return nil
20: }
(dlv) continue
> main.(*Date).UnmarshalJSON() ./main.go:15 (hits goroutine(1):3 total:3) (PC: 0x11b8a3b)
10:
11: type Date struct {
12: time.Time
13: }
14:
=> 15: func (d *Date) UnmarshalJSON(b []byte) error {
16: dateStr := string(b) // something like `"2017-08-20"`
17:
18: if dateStr == "null" {
19: return nil
20: }
(dlv) continue
Unmarshalled value using standard library: map[2018-12-13 00:00:00 +0000 UTC:true 2018-12-14 00:00:00 +0000 UTC:true 2018-12-12 00:00:00 +0000 UTC:true]
> [jsoniter] main.main() ./main.go:45 (hits goroutine(1):1 total:1) (PC: 0x11b908d)
40: panic(fmt.Errorf("standard library unmarshal: %+v", err))
41: }
42:
43: fmt.Printf("Unmarshalled value using standard library: %+v\n", v)
44:
=> 45: if err := jsoniter.Unmarshal(data, &v); err != nil {
46: panic(fmt.Errorf("jsoniter unmarshal: %+v", err))
47: }
48:
49: fmt.Printf("%#v\n", v)
50: }
(dlv) continue
> [unrecovered-panic] runtime.fatalpanic() /usr/local/Cellar/go/1.11.1/libexec/src/runtime/panic.go:681 (hits goroutine(1):1 total:1) (PC: 0x102e760)
Warning: debugging optimized function
runtime.curg._panic.arg: interface {}(string) "jsoniter unmarshal: textUnmarshalerDecoder: parsing time \"2018-1...+214 more"
676: // fatalpanic implements an unrecoverable panic. It is like fatalthrow, except
677: // that if msgs != nil, fatalpanic also prints panic messages and decrements
678: // runningPanicDefers once main is blocked from exiting.
679: //
680: //go:nosplit
=> 681: func fatalpanic(msgs *_panic) {
682: pc := getcallerpc()
683: sp := getcallersp()
684: gp := getg()
685: var docrash bool
686: // Switch to the system stack to avoid any stack growth, which
Activity
aafreensheikh96 commentedon Oct 16, 2018
Doesn't still work on the latest version = "1.1.5"
shatil commentedon Oct 19, 2018
@aafreensheikh96 wow,
jsoniterdoesn't callDate.UnmarshalJSONat all, whereasencoding/jsoncalls it three times, as expected.Using Delve with your
main.goin the current directory:Then running it:
sudo-suhas commentedon Oct 24, 2018
@shatil Please note that unmarshaling works correctly when the
Datedata type is used as a value like so:sudo-suhas commentedon Oct 27, 2018
Thank you @taowen
fix json-iterator#313 support json marshaller type as map key