Skip to content

Commit b9f1790

Browse files
committed
Breaking: ReflectionObject#toJSON properly omits explicit undefined values
1 parent 6493f52 commit b9f1790

File tree

16 files changed

+84
-92
lines changed

16 files changed

+84
-92
lines changed

src/enum.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ Enum.fromJSON = function fromJSON(name, json) {
7474
* @returns {EnumDescriptor} Enum descriptor
7575
*/
7676
Enum.prototype.toJSON = function toJSON() {
77-
return {
78-
options : this.options,
79-
values : this.values
80-
};
77+
return util.toObject([
78+
"options" , this.options,
79+
"values" , this.values
80+
]);
8181
};
8282

8383
/**

src/field.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -235,13 +235,13 @@ Field.prototype.setOption = function setOption(name, value, ifNotSet) {
235235
* @returns {FieldDescriptor} Field descriptor
236236
*/
237237
Field.prototype.toJSON = function toJSON() {
238-
return {
239-
rule : this.rule !== "optional" && this.rule || undefined,
240-
type : this.type,
241-
id : this.id,
242-
extend : this.extend,
243-
options : this.options
244-
};
238+
return util.toObject([
239+
"rule" , this.rule !== "optional" && this.rule || undefined,
240+
"type" , this.type,
241+
"id" , this.id,
242+
"extend" , this.extend,
243+
"options" , this.options
244+
]);
245245
};
246246

247247
/**

src/mapfield.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ MapField.fromJSON = function fromJSON(name, json) {
7979
* @returns {MapFieldDescriptor} Map field descriptor
8080
*/
8181
MapField.prototype.toJSON = function toJSON() {
82-
return {
83-
keyType : this.keyType,
84-
type : this.type,
85-
id : this.id,
86-
extend : this.extend,
87-
options : this.options
88-
};
82+
return util.toObject([
83+
"keyType" , this.keyType,
84+
"type" , this.type,
85+
"id" , this.id,
86+
"extend" , this.extend,
87+
"options" , this.options
88+
]);
8989
};
9090

9191
/**

src/method.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,14 @@ Method.fromJSON = function fromJSON(name, json) {
115115
* @returns {MethodDescriptor} Method descriptor
116116
*/
117117
Method.prototype.toJSON = function toJSON() {
118-
return {
119-
type : this.type !== "rpc" && /* istanbul ignore next */ this.type || undefined,
120-
requestType : this.requestType,
121-
requestStream : this.requestStream,
122-
responseType : this.responseType,
123-
responseStream : this.responseStream,
124-
options : this.options
125-
};
118+
return util.toObject([
119+
"type" , this.type !== "rpc" && /* istanbul ignore next */ this.type || undefined,
120+
"requestType" , this.requestType,
121+
"requestStream" , this.requestStream,
122+
"responseType" , this.responseType,
123+
"responseStream" , this.responseStream,
124+
"options" , this.options
125+
]);
126126
};
127127

128128
/**

src/namespace.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,10 @@ Object.defineProperty(Namespace.prototype, "nestedArray", {
131131
* @returns {NamespaceBaseDescriptor} Namespace descriptor
132132
*/
133133
Namespace.prototype.toJSON = function toJSON() {
134-
return {
135-
options : this.options,
136-
nested : arrayToJSON(this.nestedArray)
137-
};
134+
return util.toObject([
135+
"options" , this.options,
136+
"nested" , arrayToJSON(this.nestedArray)
137+
]);
138138
};
139139

140140
/**

src/oneof.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ OneOf.fromJSON = function fromJSON(name, json) {
6666
* @returns {OneOfDescriptor} Oneof descriptor
6767
*/
6868
OneOf.prototype.toJSON = function toJSON() {
69-
return {
70-
oneof : this.oneof,
71-
options : this.options
72-
};
69+
return util.toObject([
70+
"options" , this.options,
71+
"oneof" , this.oneof
72+
]);
7373
};
7474

7575
/**

src/service.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ Service.fromJSON = function fromJSON(name, json) {
6868
*/
6969
Service.prototype.toJSON = function toJSON() {
7070
var inherited = Namespace.prototype.toJSON.call(this);
71-
return {
72-
options : inherited && inherited.options || undefined,
73-
methods : Namespace.arrayToJSON(this.methodsArray) || /* istanbul ignore next */ {},
74-
nested : inherited && inherited.nested || undefined
75-
};
71+
return util.toObject([
72+
"options" , inherited && inherited.options || undefined,
73+
"methods" , Namespace.arrayToJSON(this.methodsArray) || /* istanbul ignore next */ {},
74+
"nested" , inherited && inherited.nested || undefined
75+
]);
7676
};
7777

7878
/**

src/type.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -281,15 +281,15 @@ Type.fromJSON = function fromJSON(name, json) {
281281
*/
282282
Type.prototype.toJSON = function toJSON() {
283283
var inherited = Namespace.prototype.toJSON.call(this);
284-
return {
285-
options : inherited && inherited.options || undefined,
286-
oneofs : Namespace.arrayToJSON(this.oneofsArray),
287-
fields : Namespace.arrayToJSON(this.fieldsArray.filter(function(obj) { return !obj.declaringField; })) || {},
288-
extensions : this.extensions && this.extensions.length ? this.extensions : undefined,
289-
reserved : this.reserved && this.reserved.length ? this.reserved : undefined,
290-
group : this.group || undefined,
291-
nested : inherited && inherited.nested || undefined
292-
};
284+
return util.toObject([
285+
"options" , inherited && inherited.options || undefined,
286+
"oneofs" , Namespace.arrayToJSON(this.oneofsArray),
287+
"fields" , Namespace.arrayToJSON(this.fieldsArray.filter(function(obj) { return !obj.declaringField; })) || {},
288+
"extensions" , this.extensions && this.extensions.length ? this.extensions : undefined,
289+
"reserved" , this.reserved && this.reserved.length ? this.reserved : undefined,
290+
"group" , this.group || undefined,
291+
"nested" , inherited && inherited.nested || undefined
292+
]);
293293
};
294294

295295
/**

src/util.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ util.toArray = function toArray(object) {
3434
return array;
3535
};
3636

37+
/**
38+
* Converts an array of keys immediately followed by their respective value to an object, omitting undefined values.
39+
* @param {Array.<*>} array Array to convert
40+
* @returns {Object.<string,*>} Converted object
41+
*/
42+
util.toObject = function toObject(array) {
43+
var object = {};
44+
for (var i = 0; i < array.length; i += 2) {
45+
var key = array[i ],
46+
val = array[i + 1];
47+
if (val !== undefined)
48+
object[key] = val;
49+
}
50+
return object;
51+
};
52+
3753
var safePropBackslashRe = /\\/g,
3854
safePropQuoteRe = /"/g;
3955

tests/api_enum.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,11 @@ tape.test("reflected enums", function(test) {
6868
}, "should no longer expose any removed values by id");
6969

7070
test.same(enm.toJSON(), {
71-
options: undefined,
7271
values: {
7372
a: 1,
7473
c: 3
7574
}
76-
}, "should export options and values to JSON");
75+
}, "should export values to JSON");
7776

7877
enm_allow_alias.add( 'b', 0 );
7978
test.same( enm_allow_alias.values, {

0 commit comments

Comments
 (0)