Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Cannot Unmarshal null fields to UUID #689

Open
farisekananda opened this issue Nov 29, 2023 · 1 comment
Open

Bug: Cannot Unmarshal null fields to UUID #689

farisekananda opened this issue Nov 29, 2023 · 1 comment

Comments

@farisekananda
Copy link

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
Copy link

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants