Escaping in EJSON only goes one level

This commit is contained in:
Naomi Seyfer
2013-02-04 16:12:49 -08:00
parent d92628020f
commit a866689c6a
2 changed files with 20 additions and 4 deletions

View File

@@ -63,9 +63,9 @@ var builtinConverters = [
return EJSON._base64Decode(obj.$binary);
}
},
{ // Literal
{ // Escaping one level
matchJSONValue: function (obj) {
return _.has(obj, '$literal') && _.size(obj) === 1;
return _.has(obj, '$escape') && _.size(obj) === 1;
},
matchObject: function (obj) {
if (_.isEmpty(obj) || _.size(obj) > 2) {
@@ -76,10 +76,18 @@ var builtinConverters = [
});
},
toJSONValue: function (obj) {
return {$literal: obj};
var newObj = {};
_.each(obj, function (value, key) {
newObj[key] = EJSON.toJSONValue(value);
});
return {$escape: newObj};
},
fromJSONValue: function (obj) {
return obj.$literal;
var newObj = {};
_.each(obj.$escape, function (value, key) {
newObj[key] = EJSON.fromJSONValue(value);
});
return newObj;
}
},
{ // Custom

View File

@@ -23,3 +23,11 @@ Tinytest.add("ejson - keyOrderSensitive", function (test) {
d: {f: 4, e: 3}
}, {keyOrderSensitive: true}));
});
Tinytest.add("ejson - nesting and literal", function (test) {
var d = new Date;
var obj = {$date: d};
var eObj = EJSON.toJSONValue(obj);
var roundTrip = EJSON.fromJSONValue(eObj);
test.equal(obj, roundTrip);
});