Skip to content

Commit ca45baa

Browse files
committed
Have goog.json.serialize return 'null' for functions. This conforms with
JSON.stringify, and allows these serializations to be consumed by other standard server/client-side parsers. Fixes google#491 ------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=99202470
1 parent fc52dfe commit ca45baa

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

closure/goog/json/json.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,7 @@ goog.json.Serializer.prototype.serializeInternal = function(object, sb) {
238238
sb.push(object);
239239
break;
240240
case 'function':
241-
// Skip functions.
242-
// TODO(user) Should we return something here?
241+
sb.push('null');
243242
break;
244243
default:
245244
throw Error('Unknown type: ' + typeof object);
@@ -347,7 +346,6 @@ goog.json.Serializer.prototype.serializeObject_ = function(obj, sb) {
347346
if (Object.prototype.hasOwnProperty.call(obj, key)) {
348347
var value = obj[key];
349348
// Skip functions.
350-
// TODO(ptucker) Should we return something for function properties?
351349
if (typeof value != 'function') {
352350
sb.push(sep);
353351
this.serializeString_(key, sb);

closure/goog/json/json_test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,15 @@ function testArraySerialize() {
105105
assertSerialize('[1,2]', [1, 2]);
106106
assertSerialize('[1,2,3]', [1, 2, 3]);
107107
assertSerialize('[[]]', [[]]);
108+
assertSerialize('[null,null]', [function() {}, function() {}]);
108109

109110
assertNotEquals('{length:0}', goog.json.serialize({length: 0}), '[]');
110111
}
111112

113+
function testFunctionSerialize() {
114+
assertSerialize('null', function() {});
115+
}
116+
112117
function testObjectSerialize_emptyObject() {
113118
assertSerialize('{}', {});
114119
}
@@ -134,7 +139,7 @@ function testSerializeSkipFunction() {
134139
i: 100,
135140
f: function() { var x = 'x'; }
136141
};
137-
assertSerialize('', object.f);
142+
assertSerialize('null', object.f);
138143
assertSerialize('{"s":"string value","b":true,"i":100}', object);
139144
}
140145

0 commit comments

Comments
 (0)