Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed a method result into JSON output when marshalling a type?

I'm seeking a clean approach to embed the return value of a method into the JSON marshalling of a type/value. It would be great if I don't need to write custom JSON marshaller.

For example, if the User type has FirstName and LastName fields and a FullName() method, how can I easily embed a full_name field into JSON output?

 type User struct {
     FirstName string `json:"first_name"`
     LastName  string `json:"last_name"`
 }

 func (u User) FullName() string {
     return fmt.Sprintf("%s %s", u.FirstName, u.LastName)
 }

Expected JSON:

 {
     "first_name": "John",
     "last_name":  "Smith",
     "full_name":  "John Smith"
 }
like image 855
tomodian Avatar asked Dec 05 '25 04:12

tomodian


1 Answers

This cannot be easily handled without providing some marshaller. I understand you don't want to write a MarshalJSON and do everything manually, but you can try to extend your structure in the custom marshaller and than rely on the default one. Proof of concept:

type User struct {
    FirstName string `json:"first_name"`
    LastName  string `json:"last_name"`
}

func (u *User) FullName() string {
    return fmt.Sprintf("%s %s", u.FirstName, u.LastName)
}

func (u User) MarshalJSON() ([]byte, error) {
    type rawUser User // raw struct, without methods (and marshaller)       
    // Marshal rawUser with an extension
    return json.Marshal(struct {
        rawUser
        FullName string `json:"full_name"`
    }{rawUser(u), u.FullName()})
}

[play]

You need to cast User to rawUser to strip all methods – otherwise you would have infinite loop of MarshalJSON. Also I've chosen MarshalJSON to operate on copy rather than pointer to make sure json.Marshal(user) will yield the same result as json.Marshal(&user).

This is not a one liner, but hides the complexity behind a standard interface, so you don't need to remember there's a special, non-standard way of converting your structure to JSON.

like image 167
tomasz Avatar answered Dec 07 '25 19:12

tomasz