简体   繁体   中英

How to ignore JSON fields when marshalling not unmarshalling

Assume I have a password field in a User struct.

type User struct{
   UserName string `json:"username"`
   Password string `json:"-"`
}

My clients register their users by posting username and password together. So if I decode JSON to above struct, it ignores password. It's expected. But I wondered is there any way to ignore fields when only marshalling. I checked go official documentation page but couldn't find anything.

https://golang.org/pkg/encoding/json/

I can add an extra field into the struct but I need to know first is it possible to do that with JSON lib.

As with any custom marshaling/unmarshaling requirements in Go, your best bet is to look at implementing json.Marshaler / json.Unmarshaler interface on a custom type.

In this case, you can do it for the password field:

// Use an explicit password type.
type password string

type User struct{
    UserName string   `json:"username"`
    Password password `json:"password"`
}

// Marshaler ignores the field value completely.
func (password) MarshalJSON() ([]byte, error) {
    return []byte(`""`), nil
}

Note that by not implementing json.Unmarshaler , type password retains the default behavior of its underlying type ( string ), which allows the struct to be unmarshalled with the Password value.

Working example: https://play.golang.org/p/HZQoCKm0vN

One common approach is to use a temporary type or variable, with same structure, but different json tags or even different structure:

type User struct {
    UserName string `json:"username"`
    Password string `json:"password"`
}

func (usr User) MarshalJSON() ([]byte, error) {
    var tmp struct {
        UserName string `json:"username"`
    }
    tmp.UserName = usr.UserName
    return json.Marshal(&tmp)
}

I prefer type composition. For example:

type User struct {
    UserName string `json:"username"`
}

type UserWithPassword struct {
    *User
    Password string `json:"password"`
}

use UserWithPassword when receiving data (eg. user creation / modification) and User when returning it (authentication).

IF you want to know how to ignore JSON fields when BOTH Marshalling and Unmarshalling:

    Ulimit struct {
        Soft uint64 `json:"-"`    // this attribute will be ignored
        Hard uint64 `json:"hard"`
    } `json:"ulimit"`

PS: I know this doesn't answer this exact question but Google search query takes devs here for "how to ignore JSON fields in Go" so I hope this helps. Landed here myself searching for this answer few minutes ago.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM