package optional import ( "encoding/json" ) func (o *Optional[T]) UnmarshalJSON(data []byte) error { var Buffer *T err := json.Unmarshal(data, &Buffer) if err != nil { return err } if Buffer == nil { var empty T o.Value = empty o.Exists = false return nil } o.Value = *Buffer o.Exists = true return nil } func (o Optional[T]) MarshalJSON() ([]byte, error) { var Buffer *T if o.Exists { Buffer = &o.Value } return json.Marshal(Buffer) }