goccy / go-json

Fast JSON encoder/decoder compatible with encoding/json for Go
MIT License
3.12k stars 148 forks source link

[]*time.Time | panic: value method time.Time.MarshalJSON called using nil *Time pointer #523

Open mirecl opened 1 month ago

mirecl commented 1 month ago

Example code with panic in go-json, but in encoding/json all good:

package main

import (
    "encoding/json"
    "fmt"
    "time"

    gojson "github.com/goccy/go-json"
)

func main() {
    data := []*time.Time{nil}

    b, _ := json.Marshal(data)
    fmt.Printf("std: %s\n", string(b))

    b, _ = gojson.Marshal(data)
    fmt.Printf("go-json: %s\n", string(b))
}

Output:

std: [null]
panic: value method time.Time.MarshalJSON called using nil *Time pointer

goroutine 1 [running]:
time.(*Time).MarshalJSON(0xc89e610?)
        <autogenerated>:1 +0x3a
github.com/goccy/go-json/internal/encoder.AppendMarshalJSON(0xc00007e8f0, 0xc000134300, {0xc000130000, 0x1, 0x400}, {0xc7c8e40?, 0x0?})
        /Users/mirecl/go/pkg/mod/github.com/goccy/go-json@v0.10.3/internal/encoder/encoder.go:430 +0x276
github.com/goccy/go-json/internal/encoder/vm.appendMarshalJSON(0x0?, 0x0?, {0xc000130000?, 0x0?, 0x0?}, {0xc7c8e40?, 0x0?})
        /Users/mirecl/go/pkg/mod/github.com/goccy/go-json@v0.10.3/internal/encoder/vm/util.go:152 +0x25
github.com/goccy/go-json/internal/encoder/vm.Run(0xc00007e8f0, {0xc000130000?, 0x0?, 0x400?}, 0xc00012e150?)
        /Users/mirecl/go/pkg/mod/github.com/goccy/go-json@v0.10.3/internal/encoder/vm/vm.go:271 +0x217e
github.com/goccy/go-json.encodeRunCode(0xc00007e8f0?, {0xc000130000?, 0xc000104dc8?, 0xc00007c0c0?}, 0xc000130400?)
        /Users/mirecl/go/pkg/mod/github.com/goccy/go-json@v0.10.3/encode.go:310 +0x56
github.com/goccy/go-json.encode(0xc00007e8f0, {0xc7a8320, 0xc000010090})
        /Users/mirecl/go/pkg/mod/github.com/goccy/go-json@v0.10.3/encode.go:235 +0x205
github.com/goccy/go-json.marshal({0xc7a8320, 0xc000010090}, {0x0, 0x0, 0x1?})
        /Users/mirecl/go/pkg/mod/github.com/goccy/go-json@v0.10.3/encode.go:150 +0xb9
github.com/goccy/go-json.MarshalWithOption(...)
        /Users/mirecl/go/pkg/mod/github.com/goccy/go-json@v0.10.3/json.go:185
github.com/goccy/go-json.Marshal({0xc7a8320?, 0xc000010090?})
        /Users/mirecl/go/pkg/mod/github.com/goccy/go-json@v0.10.3/json.go:170 +0x25
main.main()
        /Users/mirecl/Desktop/mapping/untitled folder/main.go:17 +0xb6
exit status 2