json-iterator / go

A high-performance 100% compatible drop-in replacement of "encoding/json"
http://jsoniter.com/migrate-from-go-std.html
MIT License
13.33k stars 1.02k forks source link

Bug: Cannot Unmarshal null fields to UUID #689

Open farisekananda opened 9 months ago

farisekananda commented 9 months ago

Cannot unmarshal null fields to UUID, even though I already uses ConfigCompatibleWithStandardLibrary which ensures 100% compatibility.

It also fails with the NewDecoder.Decode function.

Sample Program:

package main

import (
    "bytes"
    "fmt"

    "encoding/json"

    "github.com/google/uuid"
    jsoniterlib "github.com/json-iterator/go"
)

type TestStruct struct {
    SomeUUID uuid.UUID `json:"someUUID"`
    NilUUID  uuid.UUID `json:"nilUUID"`
}

var jsoniter = jsoniterlib.ConfigCompatibleWithStandardLibrary

func main() {
    fmt.Println("jsoniter fails to decode null field to UUID")
    var err error
    var test TestStruct
    err = jsoniter.Unmarshal(bytes.NewBufferString(`{"someUUID":"fa0e715a-7234-4110-aed0-ecc3de305e62","nilUUID":null}`).Bytes(), &test)
    fmt.Println(test)
    fmt.Println(err)

    fmt.Println("\nexpected results:")
    var err2 error
    var test2 TestStruct
    err2 = json.Unmarshal(bytes.NewBufferString(`{"someUUID":"fa0e715a-7234-4110-aed0-ecc3de305e62","nilUUID":null}`).Bytes(), &test2)
    fmt.Println(test2)
    fmt.Println(err2)
}

Results:

jsoniter fails to decode null field to UUID
{fa0e715a-7234-4110-aed0-ecc3de305e62 00000000-0000-0000-0000-000000000000}
main.TestStruct.NilUUID: textUnmarshalerDecoder: invalid UUID length: 0, error found in #10 byte of ...|UUID":null}|..., bigger context ...|0e715a-7234-4110-aed0-ecc3de305e62","nilUUID":null}|...

expected results:
{fa0e715a-7234-4110-aed0-ecc3de305e62 00000000-0000-0000-0000-000000000000}
<nil>

Ran on golang 1.21.0

gallone2000 commented 2 months ago

@farisekananda I made some checks in my local einvironment (go 1.20.14) with the latest version available for this pkg and no errors have been raised