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

Commit f393ad5

Browse files
committed
feat($httpParamSerializerJQLike): honor object.toJSON function if present.
1 parent 344dffb commit f393ad5

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
@@ -111,7 +111,9 @@ function $HttpParamSerializerJQLikeProvider() {
111111
forEach(toSerialize, function(value) {
112112
serialize(value, prefix + '[]');
113113
});
114-
} else if (isObject(toSerialize) && !isDate(toSerialize)) {
114+
} else if (isFunction(toSerialize.toJSON)) {
115+
parts.push(encodeUriQuery(prefix) + '=' + encodeUriQuery(toSerialize.toJSON()));
116+
} else if (isObject(toSerialize)) {
115117
forEachSorted(toSerialize, function(value, key) {
116118
serialize(value, prefix +
117119
(topLevel ? '' : '[') +

test/ng/httpSpec.js

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

2002+
it('should honor toJSON function', function() {
2003+
expect(jqrSer({
2004+
foo: {
2005+
a: 'b',
2006+
toJSON: function() {
2007+
return 'baz';
2008+
}
2009+
},
2010+
bar: {
2011+
c: 'd'
2012+
}
2013+
})).toEqual('bar%5Bc%5D=d&foo=baz');
2014+
});
2015+
20022016
});
20032017

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

0 commit comments

Comments
 (0)