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

encoding/gjson: UnmarshalJSON of dst not called #3769

Closed
wlynxg opened this issue Sep 12, 2024 · 1 comment
Closed

encoding/gjson: UnmarshalJSON of dst not called #3769

wlynxg opened this issue Sep 12, 2024 · 1 comment
Labels

Comments

@wlynxg
Copy link
Contributor

wlynxg commented Sep 12, 2024

Go version

go version go1.22.4 windows/amd64

GoFrame version

v2.7.3

Can this bug be reproduced with the latest release?

Option Yes

What did you do?

package main

import (
	"encoding/json"
	"fmt"

	"github.com/gogf/gf/v2/encoding/gjson"
)

type User struct {
	Id string
}

func (u *User) UnmarshalJSON(data []byte) error {
	var um map[string]string
	err := json.Unmarshal(data, &um)
	if err != nil {
		return err
	}

	if v, ok := um["uid"]; ok {
		u.Id = v
	}
	return nil
}

func main() {
	data := `{"uid": "123", "uname": "456"}`
	var u User
	err := gjson.New(data).Scan(&u)
	if err != nil {
		panic(err)
	}
	fmt.Println("user", u)
}

What did you see happen?

user {}

What did you expect to see?

user {123}
@wlynxg wlynxg added the bug It is confirmed a bug, but don't worry, we'll handle it. label Sep 12, 2024
@gqcn gqcn added question and removed bug It is confirmed a bug, but don't worry, we'll handle it. labels Sep 12, 2024
@gqcn
Copy link
Member

gqcn commented Sep 12, 2024

@wlynxg Hi, the gjson does not support json.Unmarshaler as the json string you passed to gjson.New was already converted to map[string]interface{} for hierarchical access feature.

@gqcn gqcn closed this as completed Sep 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants