Skip to content

Commit

Permalink
Merge pull request cocos#1390 from jareguo/master
Browse files Browse the repository at this point in the history
fully support serialization for FastDefined value types
  • Loading branch information
jareguo authored Dec 23, 2016
2 parents 18f0e41 + 31efbeb commit b3a5a54
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 26 deletions.
48 changes: 23 additions & 25 deletions cocos2d/core/platform/deserialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ var _Deserializer = (function () {
}
else {
if (prop) {
if (CC_DEV) {
if (CC_EDITOR || CC_TEST) {
self._deserializeObjField(instance, prop, propName, self._target && instance);
}
else {
Expand All @@ -250,33 +250,31 @@ var _Deserializer = (function () {
}
};

function _deserializeTypedObject (self, instance, serialized) {
//++self.stackCounter;
//if (self.stackCounter === 100) {
// debugger;
//}
for (var propName in instance) { // 遍历 instance,如果具有类型,才不会把 __type__ 也读进来
function _deserializeTypedObject (self, instance, serialized, klass) {
var fastDefinedProps = klass.__props__;
if (!fastDefinedProps) {
fastDefinedProps = Object.keys(instance); // 遍历 instance,如果具有类型,才不会把 __type__ 也读进来
}
for (var i = 0; i < fastDefinedProps.length; i++) {
var propName = fastDefinedProps[i];
var prop = serialized[propName];
if (typeof prop !== 'undefined' && serialized.hasOwnProperty(propName)) {
if (typeof prop !== 'object') {
instance[propName] = prop;
}
else {
if (prop) {
if (CC_DEV) {
self._deserializeObjField(instance, prop, propName, self._target && instance);
}
else {
self._deserializeObjField(instance, prop, propName);
}
else if (prop) {
if (CC_EDITOR || CC_TEST) {
self._deserializeObjField(instance, prop, propName, self._target && instance);
}
else {
instance[propName] = null;
self._deserializeObjField(instance, prop, propName);
}
}
else {
instance[propName] = null;
}
}
}
//--self.stackCounter;
}

// function _deserializeFireClass(self, obj, serialized, klass, target) {
Expand Down Expand Up @@ -308,7 +306,7 @@ var _Deserializer = (function () {
// }
// else {
// if (prop) {
// if (CC_DEV) {
// if (CC_EDITOR || CC_TEST) {
// self._deserializeObjField(obj, prop, propName, target && obj);
// }
// else {
Expand Down Expand Up @@ -380,7 +378,7 @@ var _Deserializer = (function () {
'o' + accessor + '=prop;');
sources.push( '}else{' +
'if(prop)');
if (CC_DEV) {
if (CC_EDITOR || CC_TEST) {
sources.push( 's._deserializeObjField(o,prop,' + propNameLiteral + ',t&&o);');
}
else {
Expand Down Expand Up @@ -448,7 +446,7 @@ var _Deserializer = (function () {
return null;
}

if (CC_DEV && target) {
if ((CC_EDITOR || CC_TEST) && target) {
// use target
if ( !(target instanceof klass) ) {
cc.warnID(5300, JS.getClassName(target), klass);
Expand Down Expand Up @@ -482,21 +480,21 @@ var _Deserializer = (function () {
obj.a = serialized.a || 255;
}
else {
_deserializeTypedObject(self, obj, serialized);
_deserializeTypedObject(self, obj, serialized, klass);
}
}
else if ( !Array.isArray(serialized) ) {

// embedded primitive javascript object

obj = (CC_DEV && target) || {};
obj = ((CC_EDITOR || CC_TEST) && target) || {};
self._deserializePrimitiveObject(obj, serialized);
}
else {

// Array

if (CC_DEV && target) {
if ((CC_EDITOR || CC_TEST) && target) {
target.length = serialized.length;
obj = target;
}
Expand All @@ -507,7 +505,7 @@ var _Deserializer = (function () {
for (var i = 0; i < serialized.length; i++) {
prop = serialized[i];
if (typeof prop === 'object' && prop) {
if (CC_DEV) {
if (CC_EDITOR || CC_TEST) {
self._deserializeObjField(obj, prop, '' + i, target && obj);
}
else {
Expand Down Expand Up @@ -547,7 +545,7 @@ cc.deserialize = function (data, result, options) {
var classFinder = options.classFinder || JS._getClassById;
// 启用 createAssetRefs 后,如果有 url 属性则会被统一强制设置为 { uuid: 'xxx' },必须后面再特殊处理
var createAssetRefs = options.createAssetRefs || cc.sys.platform === cc.sys.EDITOR_CORE;
var target = CC_DEV && options.target;
var target = (CC_EDITOR || CC_TEST) && options.target;
var customEnv = options.customEnv;
var ignoreEditorOnly = options.ignoreEditorOnly;

Expand Down
42 changes: 42 additions & 0 deletions test/qunit/unit/test-deserialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,48 @@ if (TestEditorExtends) {
deepEqual(cc.deserialize(str, null, {target: null}), obj, 'can deserialize null');
});

test('fast defined property', function () {
function Vec3 (x, y, z) {
this.data = [x, y, z];
}
cc.Class._fastDefine('Vec3', Vec3, { x: 0, y: 0, z: 0, });

Object.defineProperties(Vec3.prototype, {
x: {
get: function () {
return this.data[0];
},
set: function (value) {
this.data[0] = value;
},
},
y: {
get: function () {
return this.data[1];
},
set: function (value) {
this.data[1] = value;
},
},
z: {
get: function () {
return this.data[2];
},
set: function (value) {
this.data[2] = value;
},
}
});

var vec3 = cc.deserialize({ __type__: "Vec3", x: 1, y: 2, z: 3 });
ok(vec3 instanceof Vec3, 'test type');
strictEqual(vec3.x, 1, 'test x');
strictEqual(vec3.y, 2, 'test y');
strictEqual(vec3.z, 3, 'test z');

cc.js.unregisterClass(Vec3);
});

test('json deserialize test', function () {

// TODO:
Expand Down
47 changes: 46 additions & 1 deletion test/qunit/unit/test-serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,51 @@
cc.js.unregisterClass(Sprite);
});

test('fast defined property', function () {
function Vec3 (x, y, z) {
this.data = [x, y, z];
}
cc.Class._fastDefine('Vec3', Vec3, { x: 0, y: 0, z: 0, });

Object.defineProperties(Vec3.prototype, {
x: {
get: function () {
return this.data[0];
},
set: function (value) {
this.data[0] = value;
},
},
y: {
get: function () {
return this.data[1];
},
set: function (value) {
this.data[1] = value;
},
},
z: {
get: function () {
return this.data[2];
},
set: function (value) {
this.data[2] = value;
},
}
});

var obj = new Vec3(2, 3 ,1);
var expected = {
__type__: 'Vec3',
x: 2,
y: 3,
z: 1,
};
match(obj, expected, 'should be able to serialize');

cc.js.unregisterClass(Vec3);
});

test('test asset property', function () {
var sprite = new TestSprite();
sprite.texture = new TestTexture();
Expand Down Expand Up @@ -321,7 +366,7 @@
{ "__type__": "cc.Object", "_name": "test_1", "_objFlags": 0 },
{ "__type__": "cc.Object", "_name": "test_3", "_objFlags": 0 },
{ "__type__": "cc.Object", "_name": "test_4", "_objFlags": 0 }
]
];

cc._Test.nicifySerialized(data);
deepEqual(data, expected, 'nicify success');
Expand Down

0 comments on commit b3a5a54

Please sign in to comment.