summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSiyuan Zhou <siyuan.zhou@mongodb.com>2019-04-26 17:22:14 -0400
committerSiyuan Zhou <visualzhou@gmail.com>2019-05-12 20:36:22 -0400
commit20107d15da5ff360378f2bf98acb8040b7a5f8a3 (patch)
tree46e58b3fadeea996ec0a074a837f66b8b6630331
parente815c7ff680ba4687ed83a7a208d2daaef92e409 (diff)
downloadmongo-20107d15da5ff360378f2bf98acb8040b7a5f8a3.tar.gz
SERVER-40866 Use JSON.stringify() to serialize string in tojson()
(cherry picked from commit b877bd0172b613e77365f29886d359e5230f1a3e)
-rw-r--r--src/mongo/shell/types.js43
1 files changed, 2 insertions, 41 deletions
diff --git a/src/mongo/shell/types.js b/src/mongo/shell/types.js
index af216eed661..80c033b7b03 100644
--- a/src/mongo/shell/types.js
+++ b/src/mongo/shell/types.js
@@ -606,47 +606,8 @@ tojson = function(x, indent, nolint, depth) {
}
switch (typeof x) {
- case "string": {
- var out = new Array(x.length + 1);
- out[0] = '"';
- for (var i = 0; i < x.length; i++) {
- switch (x[i]) {
- case '"':
- out[out.length] = '\\"';
- break;
- case '\\':
- out[out.length] = '\\\\';
- break;
- case '\b':
- out[out.length] = '\\b';
- break;
- case '\f':
- out[out.length] = '\\f';
- break;
- case '\n':
- out[out.length] = '\\n';
- break;
- case '\r':
- out[out.length] = '\\r';
- break;
- case '\t':
- out[out.length] = '\\t';
- break;
-
- default: {
- var code = x.charCodeAt(i);
- if (code < 0x20) {
- out[out.length] =
- (code < 0x10 ? '\\u000' : '\\u00') + code.toString(16);
- } else {
- out[out.length] = x[i];
- }
- }
- }
- }
-
- return out.join('') + "\"";
- }
+ case "string":
+ return JSON.stringify(x);
case "number":
case "boolean":
return "" + x;