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

Commit d4b331a

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

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
@@ -2049,6 +2049,20 @@ describe('$http param serializers', function() {
20492049
expect(jqrSer({someDate: new Date('2014-07-15T17:30:00.000Z')})).toEqual('someDate=2014-07-15T17:30:00.000Z');
20502050
});
20512051

2052+
it('should honor toJSON function', function() {
2053+
expect(jqrSer({
2054+
foo: {
2055+
a: 'b',
2056+
toJSON: function() {
2057+
return 'baz';
2058+
}
2059+
},
2060+
bar: {
2061+
c: 'd'
2062+
}
2063+
})).toEqual('bar%5Bc%5D=d&foo=baz');
2064+
});
2065+
20522066
});
20532067

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

0 commit comments

Comments
 (0)