I did a bit of searching for similar posts, but Go JSON unmarshalling is a hot topic and I couldn't see anything specifically for my question among all the other posts.
Is there a way to add/register JSON unmarshalling logic for an existing type -- one defined by an external library?
Example:
import (
"go.mongodb.org/mongo-driver/bson/primitive"
)
type SomeDBModel struct {
Created primitive.DateTime
}
# NOTE: primitive.DateTime is an int64 and has implemented MarshalJSON,
# but not UnmarshalJSON.
# It marshals into an RFC 3339 datetime string; I'd like to be able to
# also unmarshal from RFC 3339 strings.
Is there some way I can register an unmarshalling function for primitive.DateTime
objects to Go's default JSON unmarshaller? I'd rather not embed primitive.DateTime
into a wrapper struct.