Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
464 views
in Technique[技术] by (71.8m points)

pointers - How to json unmarshalling with custom attribute type in Go

In my project, I've defined structures so that get data from JSON. I tried to use json.Unmarshal() function. But it did not work for custom type attribute.

There was a structure like this:

type TestModel struct {
    ID   NullInt `json:"id"`
    Name string  `json:"name"`
}

In there, NullInt type was defined with implementations of MarshalJSON() and UnmarshalJSON() functions:

// NullInt ...
type NullInt struct {
    Int   int
    Valid bool
}

// MarshalJSON ...
func (ni NullInt) MarshalJSON() ([]byte, error) {
    if !ni.Valid {
        return []byte("null"), nil
    }
    return json.Marshal(ni.Int)
}

// UnmarshalJSON ...
func (ni NullInt) UnmarshalJSON(b []byte) error {
    fmt.Println("UnmarshalJSON...")
    err := json.Unmarshal(b, &ni.Int)
    ni.Valid = (err == nil)
    fmt.Println("NullInt:", ni)
    return err
}

In main() function, I implemented:

func main() {
    model := new(TestModel)
    JSON := `{
        "id": 1,
        "name": "model" 
    }`
    json.Unmarshal([]byte(JSON), &model)
    fmt.Println("model.ID:", model.ID) 
}

In console, I got:

UnmarshalJSON...
NullInt: {1 true}
model.ID: {0 false}

As you can see, NullInt.UnmarshalJSON() was called and ni was what I expected but model.ID's value. What is the right way to implement UnmarshalJSON() function?

To addition, when I set: JSON := `{"name": "model"}` (without id), console just was:

model.ID: {0 false}

That means, the UnmarshalJSON() function wasn't called then I didn't get model.ID's value in right way.

question from:https://stackoverflow.com/questions/66053344/nested-struct-initialization-nil-pointer

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

UnmarshalJSON() needs to modify the receiver, so you must use pointer receiver:

func (ni *NullInt) UnmarshalJSON(b []byte) error {
    // ...
}

The receiver and all parameters are just copies, and if you don't use a pointer, you may only modify a copy which will be discarded once the method returns. If you use a pointer receiver, that is also just a copy, but the pointed value will be the same, hence you may modify the original (pointed) object.

For consistency, also use pointer receiver for its other methods.

With this change it works and outputs (try it on the Go Playground):

UnmarshalJSON...
NullInt: &{1 true}
model.ID: {1 true}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...