I have a Profile{}
struct with an ID
property of the type uuid.UUID
. When I marshal this, I am converting the UUID
to a string
, like this:
type Profile struct {
Id uuid.UUID
}
func (profile *Profile) MarshalJSON() ([]byte, error) {
type Alias Profile
return json.Marshal(&struct {
Id string
*Alias
}{
Id: profile.Id.String(),
Alias: (*Alias)(profile),
})
}
However, when I want to unmarshal this JSON, it complains that the id
is a string. So I need to initialise a UUID
struct when unmarshalling, like this: uuid.New([]byte(jsonId))
Is this possible to do without altering the UUID
implementation, and if so, how?