Custom (de)serializers #89
-
I would be nice to be able to support alternative JSON libraries, like #87 and #88. JSON is just one kind of serialization. There's also XML, a la #79 / #82, and presumably other options like binary (Protobuf?) and whatnot. What if we add a new API like type Serializer = func(v any) ([]byte, error)
type Deserializer = func([]byte, v any) error
func (rb *Builder) Serialize(s Serializer, v any)
func (rb *Builder) Deserialize(d Deserializer, v any)
// Used if s or d are nil
var (
DefaultSerializer = json.Marshal
DefaultDeserializer = json.Unmarshal
)
// Usage
func init() {
requests.DefaultSerializer = gojson.Marshal
requests.DefaultDeserializer = gojson.Unmarshal
}
err := requests.
URL(url).
Serialize(nil, req).
Deserialize(nil, res).
Fetch(ctx) @EvilBorsch, what do you think of this idea? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 9 replies
-
It should probably also have a string param for (default) content type. |
Beta Was this translation helpful? Give feedback.
-
I think it looks good architecturally, but bad from a ux point of view. It is convenient and understandable for developers to use ToJSON, if server return for example xml, most convient way is to find ToXML, not to call Serialize with parameters. It seems that for complete happiness it is only necessary to give the opportunity to speed up toJSON method (keep in mind that 90%+ usecases will return JSON). Moreover, it seems to me, that this should not be within the framework of conditional FastToJSON, but in the form of a GLOBAL override of the serializer/deserializer for json especially. Because memorize that in each request we need to override serialize is quite hard. |
Beta Was this translation helpful? Give feedback.
I started working on #91.