Skip to content

Commit

Permalink
Escaping in EJSON only goes one level
Browse files Browse the repository at this point in the history
  • Loading branch information
Naomi Seyfer committed Feb 5, 2013
1 parent d926280 commit a866689
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
16 changes: 12 additions & 4 deletions packages/ejson/ejson.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions packages/ejson/ejson_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

0 comments on commit a866689

Please sign in to comment.