Skip to content

Commit da47720

Browse files
committed
Restructure Object encoding for any layer of nested objects
1 parent 1cd98f8 commit da47720

File tree

4 files changed

+57
-19
lines changed

4 files changed

+57
-19
lines changed

src/ParseObject.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ export default class ParseObject {
231231
return dirty;
232232
}
233233

234-
_toFullJSON(): AttributeMap {
235-
var json: { [key: string]: mixed } = this.toJSON();
234+
_toFullJSON(seen): AttributeMap {
235+
var json: { [key: string]: mixed } = this.toJSON(seen);
236236
json.__type = 'Object';
237237
json.className = this.className;
238238
return json;
@@ -366,15 +366,16 @@ export default class ParseObject {
366366
* @method toJSON
367367
* @return {Object}
368368
*/
369-
toJSON(): AttributeMap {
369+
toJSON(seen): AttributeMap {
370370
var seenEntry = this.id ? this.className + ':' + this.id : this;
371+
var seen = seen || [seenEntry];
371372
var json = {};
372373
var attrs = this.attributes;
373374
for (var attr in attrs) {
374375
if ((attr === 'createdAt' || attr === 'updatedAt') && attrs[attr].toJSON) {
375376
json[attr] = attrs[attr].toJSON();
376377
} else {
377-
json[attr] = encode(attrs[attr], false, false, [seenEntry]);
378+
json[attr] = encode(attrs[attr], false, false, seen);
378379
}
379380
}
380381
var pending = this._getPendingOps();

src/__tests__/ParseObject-test.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,55 @@ describe('ParseObject', () => {
627627
});
628628
});
629629

630+
it('encodes multiple layers of nested objects', () => {
631+
var grandparent = ParseObject.fromJSON({
632+
__type: 'Object',
633+
className: 'Item',
634+
objectId: 'nestedGrand',
635+
child: {
636+
__type: 'Pointer',
637+
className: 'Item',
638+
objectId: 'nestedParent'
639+
}
640+
});
641+
642+
var parent = ParseObject.fromJSON({
643+
__type: 'Object',
644+
className: 'Item',
645+
objectId: 'nestedParent',
646+
child: {
647+
__type: 'Pointer',
648+
className: 'Item',
649+
objectId: 'nestedChild'
650+
}
651+
});
652+
653+
var child = ParseObject.fromJSON({
654+
__type: 'Object',
655+
className: 'Item',
656+
objectId: 'nestedChild',
657+
count: 12
658+
});
659+
660+
expect(grandparent.get('child').id).toBe(parent.id);
661+
expect(grandparent.get('child').get('child').id).toBe(child.id);
662+
663+
expect(grandparent.toJSON()).toEqual({
664+
objectId: 'nestedGrand',
665+
child: {
666+
__type: 'Object',
667+
className: 'Item',
668+
objectId: 'nestedParent',
669+
child: {
670+
__type: 'Object',
671+
className: 'Item',
672+
objectId: 'nestedChild',
673+
count: 12
674+
}
675+
}
676+
});
677+
});
678+
630679
it('updates the existed flag when saved', () => {
631680
var o = new ParseObject('Item');
632681
expect(o.existed()).toBe(false);

src/__tests__/encode-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ mockObject.prototype = {
2727
toJSON() {
2828
return this.attributes;
2929
},
30-
_toFullJSON() {
30+
_toFullJSON(seen) {
3131
var json = {
3232
__type: 'Object',
3333
className: this.className
3434
};
3535
for (var attr in this.attributes) {
36-
json[attr] = this.attributes[attr];
36+
json[attr] = encode(this.attributes[attr], false, false, seen.concat(this));
3737
}
3838
return json;
3939
}

src/encode.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,7 @@ function encode(value: mixed, disallowObjects: boolean, forcePointers: boolean,
3333
return value.toPointer();
3434
}
3535
seen = seen.concat(seenEntry);
36-
var json = encode(value.attributes, disallowObjects, forcePointers, seen);
37-
if (json.createdAt) {
38-
json.createdAt = json.createdAt.iso;
39-
}
40-
if (json.updatedAt) {
41-
json.updatedAt = json.updatedAt.iso;
42-
}
43-
json.className = value.className;
44-
json.__type = 'Object';
45-
if (value.id) {
46-
json.objectId = value.id;
47-
}
48-
return json;
36+
return value._toFullJSON(seen);
4937
}
5038
if (value instanceof Op ||
5139
value instanceof ParseACL ||

0 commit comments

Comments
 (0)