Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit b57bfae

Browse files
committed
feat($httpParamSerializerJQLike): honor object.toJSON function if present.
1 parent 8088194 commit b57bfae

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/ng/http.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ function $HttpParamSerializerJQLikeProvider() {
117117
forEach(toSerialize, function(value, index) {
118118
serialize(value, prefix + '[' + (isObject(value) ? index : '') + ']');
119119
});
120-
} else if (isObject(toSerialize) && !isDate(toSerialize)) {
120+
} else if (isFunction(toSerialize.toJSON)) {
121+
parts.push(encodeUriQuery(prefix) + '=' + encodeUriQuery(toSerialize.toJSON()));
122+
} else if (isObject(toSerialize)) {
121123
forEachSorted(toSerialize, function(value, key) {
122124
serialize(value, prefix +
123125
(topLevel ? '' : '[') +

test/ng/httpSpec.js

+14
Original file line numberDiff line numberDiff line change
@@ -2030,6 +2030,20 @@ describe('$http param serializers', function() {
20302030
expect(jqrSer({someDate: new Date('2014-07-15T17:30:00.000Z')})).toEqual('someDate=2014-07-15T17:30:00.000Z');
20312031
});
20322032

2033+
it('should honor toJSON function', function() {
2034+
expect(jqrSer({
2035+
foo: {
2036+
a: 'b',
2037+
toJSON: function() {
2038+
return 'baz';
2039+
}
2040+
},
2041+
bar: {
2042+
c: 'd'
2043+
}
2044+
})).toEqual('bar%5Bc%5D=d&foo=baz');
2045+
});
2046+
20332047
});
20342048

20352049
describe('default array serialization', function() {

0 commit comments

Comments
 (0)