Skip to content

Commit c9ebcc9

Browse files
GRbitjeevatkm
authored andcommitted
resty client JSON and XML Marshal/Unmarshal setters
1 parent 9e2a9b7 commit c9ebcc9

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -369,13 +369,13 @@ import jsoniter "github.com/json-iterator/go"
369369

370370
json := jsoniter.ConfigCompatibleWithStandardLibrary
371371

372-
client := resty.New()
373-
client.JSONMarshal = json.Marshal
374-
client.JSONUnmarshal = json.Unmarshal
372+
client := resty.New().
373+
SetJSONMarshaler(json.Marshal).
374+
SetJSONUnmarshaler(json.Unmarshal)
375375

376376
// similarly user could do for XML too with -
377-
client.XMLMarshal
378-
client.XMLUnmarshal
377+
client.SetXMLMarshaler(xml.Marshal).
378+
SetXMLUnmarshaler(xml.Unmarshal)
379379
```
380380

381381
### Multipart File(s) upload

client.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,34 @@ func (c *Client) SetRetryAfter(callback RetryAfterFunc) *Client {
649649
return c
650650
}
651651

652+
// SetJSONMarshaler method sets the JSON marshaler function to marshal the request body.
653+
// By default, Resty uses `encoding/json` package to marshal the request body.
654+
func (c *Client) SetJSONMarshaler(marshaler func(v interface{}) ([]byte, error)) *Client {
655+
c.JSONMarshal = marshaler
656+
return c
657+
}
658+
659+
// SetJSONUnmarshaler method sets the JSON unmarshaler function to unmarshal the response body.
660+
// By default, Resty uses `encoding/json` package to unmarshal the response body.
661+
func (c *Client) SetJSONUnmarshaler(unmarshaler func(data []byte, v interface{}) error) *Client {
662+
c.JSONUnmarshal = unmarshaler
663+
return c
664+
}
665+
666+
// SetXMLMarshaler method sets the XML marshaler function to marshal the request body.
667+
// By default, Resty uses `encoding/xml` package to marshal the request body.
668+
func (c *Client) SetXMLMarshaler(marshaler func(v interface{}) ([]byte, error)) *Client {
669+
c.XMLMarshal = marshaler
670+
return c
671+
}
672+
673+
// SetXMLUnmarshaler method sets the XML unmarshaler function to unmarshal the response body.
674+
// By default, Resty uses `encoding/xml` package to unmarshal the response body.
675+
func (c *Client) SetXMLUnmarshaler(unmarshaler func(data []byte, v interface{}) error) *Client {
676+
c.XMLUnmarshal = unmarshaler
677+
return c
678+
}
679+
652680
// AddRetryCondition method adds a retry condition function to array of functions
653681
// that are checked to determine if the request is retried. The request will
654682
// retry if any of the functions return true and error is nil.

0 commit comments

Comments
 (0)