Skip to content

Commit

Permalink
Fix serializing objects into strings
Browse files Browse the repository at this point in the history
Closes #63
  • Loading branch information
imaustink committed Oct 13, 2017
1 parent 38cc6fa commit 1b5655a
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions reflections/shape/shape.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,15 @@ function makeSerializer(methodName, symbolsToCheck){
if(MapType && !serializeMap) {
serializeMap = {
unwrap: new MapType(),
serialize: new MapType()
serialize: new MapType(),
isSerializing: {
unwrap: new MapType(),
serialize: new MapType()
},
circularReferenceIsSerializing: {
unwrap: new MapType(),
serialize: new MapType()
}
};
firstSerialize = true;
}
Expand All @@ -83,6 +91,9 @@ function makeSerializer(methodName, symbolsToCheck){
if(serializeMap) {

if( serializeMap[methodName].has(value) ) {
if(serializeMap.isSerializing[methodName].has(value)) {
serializeMap.circularReferenceIsSerializing[methodName].set(value, true);
}
return serializeMap[methodName].get(value);
} else {
serializeMap[methodName].set(value, serialized);
Expand All @@ -92,15 +103,31 @@ function makeSerializer(methodName, symbolsToCheck){
for(var i = 0, len = symbolsToCheck.length ; i< len;i++) {
var serializer = value[symbolsToCheck[i]];
if(serializer) {
var result = serializer.call(value, serialized);
serializeMap.isSerializing[methodName].set(value, true);
var result = serializer.call(value, serialized);
serializeMap.isSerializing[methodName].delete(value);

if(result !== serialized) {
// jshint -W073
if(serializeMap.circularReferenceIsSerializing[methodName].has(value)) {
// Circular references should use a custom serializer
// that sets the serialized value on the object
// passed to it as the first argument e.g.
// function(proto){
// return proto.a = canReflect.serialize(this.a);
// }
throw new Error("Cannot serialize cirular reference!");
}
serializeMap[methodName].set(value, result);
}
if(firstSerialize) {
serializeMap = null;
}
return result;
}
}

if (typeof obj ==='function') {
if (typeof obj === "function") {
if(serializeMap) {
serializeMap[methodName].set(value, value);
}
Expand Down

0 comments on commit 1b5655a

Please sign in to comment.