Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client: setters for JSON and XML Marshal/Unmarshal #621

Merged
merged 2 commits into from
Mar 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Expand Up @@ -369,13 +369,13 @@ import jsoniter "github.com/json-iterator/go"

json := jsoniter.ConfigCompatibleWithStandardLibrary

client := resty.New()
client.JSONMarshal = json.Marshal
client.JSONUnmarshal = json.Unmarshal
client := resty.New().
SetJSONMarshaler(json.Marshal).
SetJSONUnmarshaler(json.Unmarshal)

// similarly user could do for XML too with -
client.XMLMarshal
client.XMLUnmarshal
client.SetXMLMarshaler(xml.Marshal).
SetXMLUnmarshaler(xml.Unmarshal)
```

### Multipart File(s) upload
Expand Down
28 changes: 28 additions & 0 deletions client.go
Expand Up @@ -649,6 +649,34 @@ func (c *Client) SetRetryAfter(callback RetryAfterFunc) *Client {
return c
}

// SetJSONMarshaler method sets the JSON marshaler function to marshal the request body.
// By default, Resty uses `encoding/json` package to marshal the request body.
func (c *Client) SetJSONMarshaler(marshaler func(v interface{}) ([]byte, error)) *Client {
c.JSONMarshal = marshaler
return c
}

// SetJSONUnmarshaler method sets the JSON unmarshaler function to unmarshal the response body.
// By default, Resty uses `encoding/json` package to unmarshal the response body.
func (c *Client) SetJSONUnmarshaler(unmarshaler func(data []byte, v interface{}) error) *Client {
c.JSONUnmarshal = unmarshaler
return c
}

// SetXMLMarshaler method sets the XML marshaler function to marshal the request body.
// By default, Resty uses `encoding/xml` package to marshal the request body.
func (c *Client) SetXMLMarshaler(marshaler func(v interface{}) ([]byte, error)) *Client {
c.XMLMarshal = marshaler
return c
}

// SetXMLUnmarshaler method sets the XML unmarshaler function to unmarshal the response body.
// By default, Resty uses `encoding/xml` package to unmarshal the response body.
func (c *Client) SetXMLUnmarshaler(unmarshaler func(data []byte, v interface{}) error) *Client {
c.XMLUnmarshal = unmarshaler
return c
}

// AddRetryCondition method adds a retry condition function to array of functions
// that are checked to determine if the request is retried. The request will
// retry if any of the functions return true and error is nil.
Expand Down
33 changes: 33 additions & 0 deletions client_test.go
Expand Up @@ -508,6 +508,39 @@ func TestClientNewRequest(t *testing.T) {
assertNotNil(t, request)
}


func TestClientSetJSONMarshaler(t *testing.T) {
m := func (v interface{}) ([]byte, error) { return nil,nil }
c := New().SetJSONMarshaler(m)
p1 := fmt.Sprintf("%p", c.JSONMarshal)
p2 := fmt.Sprintf("%p", m)
assertEqual(t, p1, p2) // functions can not be compared, we only can compare pointers
}

func TestClientSetJSONUnmarshaler(t *testing.T) {
m := func ([]byte, interface{}) error { return nil }
c := New().SetJSONUnmarshaler(m)
p1 := fmt.Sprintf("%p", c.JSONUnmarshal)
p2 := fmt.Sprintf("%p", m)
assertEqual(t, p1, p2) // functions can not be compared, we only can compare pointers
}

func TestClientSetXMLMarshaler(t *testing.T) {
m := func (v interface{}) ([]byte, error) { return nil,nil }
c := New().SetXMLMarshaler(m)
p1 := fmt.Sprintf("%p", c.XMLMarshal)
p2 := fmt.Sprintf("%p", m)
assertEqual(t, p1, p2) // functions can not be compared, we only can compare pointers
}

func TestClientSetXMLUnmarshaler(t *testing.T) {
m := func ([]byte, interface{}) error { return nil }
c := New().SetXMLUnmarshaler(m)
p1 := fmt.Sprintf("%p", c.XMLUnmarshal)
p2 := fmt.Sprintf("%p", m)
assertEqual(t, p1, p2) // functions can not be compared, we only can compare pointers
}

func TestDebugBodySizeLimit(t *testing.T) {
ts := createGetServer(t)
defer ts.Close()
Expand Down