You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
When decoding models that support both JSON and YAML encoding, it's convenient that we already support json tags for YAML, but when using json.RawMessagego-yaml does not handle it.
Due to that there's no native (from go-yaml perspective) way to handle such models.
Converting from YAML to JSON is not an option for me as I want to retain separate syntax errors for JSON and YAML.
Describe the solution you'd like
We should either handle json.RawMessage type or introduce a new type, like yaml.RawMessage which could implement both json.(Un)marshaler and yaml.Bytes(Un)marshaler interfaces.
Describe alternatives you've considered
Currently I've implemented my own type for that.
Having to redefine it in every project I work with though is not ideal.
package serdeutil
import"github.com/pkg/errors"// RawMessage is a raw encoded JSON or YAML value.// It implements:// - [json.Marshaler] and [json.Unmarshaler]// - [yaml.BytesMarshaler] and [yaml.BytesUnmarshaler]// It can be used to delay JSON/YAML decoding or precompute a JSON/YAML encoding.typeRawMessage []bytefunc (mRawMessage) MarshalJSON() ([]byte, error) { returnm.marshal() }
func (mRawMessage) MarshalYAML() ([]byte, error) { returnm.marshal() }
func (mRawMessage) marshal() ([]byte, error) {
ifm==nil {
return []byte("null"), nil
}
returnm, nil
}
func (m*RawMessage) UnmarshalJSON(data []byte) error { returnm.unmarshal(data) }
func (m*RawMessage) UnmarshalYAML(data []byte) error { returnm.unmarshal(data) }
func (m*RawMessage) unmarshal(data []byte) error {
ifm==nil {
returnerrors.New("RawMessage: unmarshal on nil pointer")
}
*m=append((*m)[0:0], data...)
returnnil
}
The text was updated successfully, but these errors were encountered:
Is your feature request related to a problem? Please describe.
When decoding models that support both JSON and YAML encoding, it's convenient that we already support
json
tags for YAML, but when usingjson.RawMessage
go-yaml
does not handle it.Due to that there's no native (from
go-yaml
perspective) way to handle such models.Converting from YAML to JSON is not an option for me as I want to retain separate syntax errors for JSON and YAML.
Describe the solution you'd like
We should either handle
json.RawMessage
type or introduce a new type, likeyaml.RawMessage
which could implement bothjson.(Un)marshaler
andyaml.Bytes(Un)marshaler
interfaces.Describe alternatives you've considered
Currently I've implemented my own type for that.
Having to redefine it in every project I work with though is not ideal.
The text was updated successfully, but these errors were encountered: