Skip to content

Commit e88d44d

Browse files
authored
client: setters for JSON and XML Marshal/Unmarshal (#621)
* resty client JSON and XML Marshal/Unmarshal setters * setters tests
1 parent 1578007 commit e88d44d

File tree

3 files changed

+66
-5
lines changed

3 files changed

+66
-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
@@ -682,6 +682,34 @@ func (c *Client) SetRetryAfter(callback RetryAfterFunc) *Client {
682682
return c
683683
}
684684

685+
// SetJSONMarshaler method sets the JSON marshaler function to marshal the request body.
686+
// By default, Resty uses `encoding/json` package to marshal the request body.
687+
func (c *Client) SetJSONMarshaler(marshaler func(v interface{}) ([]byte, error)) *Client {
688+
c.JSONMarshal = marshaler
689+
return c
690+
}
691+
692+
// SetJSONUnmarshaler method sets the JSON unmarshaler function to unmarshal the response body.
693+
// By default, Resty uses `encoding/json` package to unmarshal the response body.
694+
func (c *Client) SetJSONUnmarshaler(unmarshaler func(data []byte, v interface{}) error) *Client {
695+
c.JSONUnmarshal = unmarshaler
696+
return c
697+
}
698+
699+
// SetXMLMarshaler method sets the XML marshaler function to marshal the request body.
700+
// By default, Resty uses `encoding/xml` package to marshal the request body.
701+
func (c *Client) SetXMLMarshaler(marshaler func(v interface{}) ([]byte, error)) *Client {
702+
c.XMLMarshal = marshaler
703+
return c
704+
}
705+
706+
// SetXMLUnmarshaler method sets the XML unmarshaler function to unmarshal the response body.
707+
// By default, Resty uses `encoding/xml` package to unmarshal the response body.
708+
func (c *Client) SetXMLUnmarshaler(unmarshaler func(data []byte, v interface{}) error) *Client {
709+
c.XMLUnmarshal = unmarshaler
710+
return c
711+
}
712+
685713
// AddRetryCondition method adds a retry condition function to array of functions
686714
// that are checked to determine if the request is retried. The request will
687715
// retry if any of the functions return true and error is nil.

client_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,39 @@ func TestClientNewRequest(t *testing.T) {
509509
assertNotNil(t, request)
510510
}
511511

512+
513+
func TestClientSetJSONMarshaler(t *testing.T) {
514+
m := func (v interface{}) ([]byte, error) { return nil,nil }
515+
c := New().SetJSONMarshaler(m)
516+
p1 := fmt.Sprintf("%p", c.JSONMarshal)
517+
p2 := fmt.Sprintf("%p", m)
518+
assertEqual(t, p1, p2) // functions can not be compared, we only can compare pointers
519+
}
520+
521+
func TestClientSetJSONUnmarshaler(t *testing.T) {
522+
m := func ([]byte, interface{}) error { return nil }
523+
c := New().SetJSONUnmarshaler(m)
524+
p1 := fmt.Sprintf("%p", c.JSONUnmarshal)
525+
p2 := fmt.Sprintf("%p", m)
526+
assertEqual(t, p1, p2) // functions can not be compared, we only can compare pointers
527+
}
528+
529+
func TestClientSetXMLMarshaler(t *testing.T) {
530+
m := func (v interface{}) ([]byte, error) { return nil,nil }
531+
c := New().SetXMLMarshaler(m)
532+
p1 := fmt.Sprintf("%p", c.XMLMarshal)
533+
p2 := fmt.Sprintf("%p", m)
534+
assertEqual(t, p1, p2) // functions can not be compared, we only can compare pointers
535+
}
536+
537+
func TestClientSetXMLUnmarshaler(t *testing.T) {
538+
m := func ([]byte, interface{}) error { return nil }
539+
c := New().SetXMLUnmarshaler(m)
540+
p1 := fmt.Sprintf("%p", c.XMLUnmarshal)
541+
p2 := fmt.Sprintf("%p", m)
542+
assertEqual(t, p1, p2) // functions can not be compared, we only can compare pointers
543+
}
544+
512545
func TestDebugBodySizeLimit(t *testing.T) {
513546
ts := createGetServer(t)
514547
defer ts.Close()

0 commit comments

Comments
 (0)