限制JSON字符串化深度 [英] Limit JSON stringification depth

查看:70
本文介绍了限制JSON字符串化深度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用JSON.stringify(或类似方法)对对象进行字符串化时,有一种方法可以限制字符串化深度,即仅将 n 级别深入对象树,而忽略之后的所有内容(或更好的方法:在其中放置占位符,表示已遗漏了某些内容)?

我知道JSON.stringify采用形式为function (key, value)的替换功能,但是我没有找到一种方法来获取传递给替换功能的当前键值对的原始对象的深度. /p>

是否可以使用默认的JSON.stringify实现来做到这一点?还是我已经达到我应该自己实施严格化的程度? 还是有另一个字符串库可以建议您使用此选项?

解决方案

我想在不包含第三方库/太多代码的情况下在第一层上对对象进行字符串化.

如果您要查找相同的东西,这里有一个简单的方法:

var json = JSON.stringify(obj, function (k, v) { return k && v && typeof v !== "number" ? (Array.isArray(v) ? "[object Array]" : "" + v) : v; });

顶级对象将没有键,因此它总是简单地返回,但是在下一级中不是数字"的任何内容都将强制转换为包含字符串.数组的一种特殊情况,否则这些数组将进一步暴露.

如果您不喜欢这种特殊的阵列盒,请使用我改进的旧解决方案:

var json = JSON.stringify(obj, function (k, v) { return k && v && typeof v !== "number" ? "" + v : v; }); // will expose arrays as strings.

尽管在两种解决方案中都可以在顶级中传递数组而不是对象,但仍然可行.

示例:

 var obj = {
  keyA: "test",
  keyB: undefined,
  keyC: 42,
  keyD: [12, "test123", undefined]
}
obj.keyD.push(obj);
obj.keyE = obj;

var arr = [12, "test123", undefined];
arr.push(arr);

var f = function (k, v) { return k && v && typeof v !== "number" ? (Array.isArray(v) ? "[object Array]" : "" + v) : v; };
var f2 = function (k, v) { return k && v && typeof v !== "number" ? "" + v : v; };

console.log("object:", JSON.stringify(obj, f));
console.log("array:", JSON.stringify(arr, f));
console.log("");
console.log("with array string cast, so the array gets exposed:");
console.log("object:", JSON.stringify(obj, f2));
console.log("array:", JSON.stringify(arr, f2)); 

When stringifying an object using JSON.stringify (or something similar) is there a way to limit the stringification depth, i.e. only go n levels deep into the object tree and ignore everything that comes after that (or better: put placeholders in there, indicating something was left out)?

I know that JSON.stringify takes a replacer function of the form function (key, value) but I didn't find a way to get the depth in the original object of the current key-value-pair handed to the replacer function.

Is there a way to do this with the default JSON.stringify implementation? Or have I reached a point where I should just implement the stringification myself? Or is there another stringification library you can recommend that has this option?

解决方案

I wanted to stringify an object on the first level without including a third-party library / too much code.

In case you look for the same, here is a quick one-liner to do so:

var json = JSON.stringify(obj, function (k, v) { return k && v && typeof v !== "number" ? (Array.isArray(v) ? "[object Array]" : "" + v) : v; });

The top level object will have no key, so it's always simply returned, however anything that is not a "number" on the next level will be casted to a string incl. a special case for arrays, otherwise those would be exposed further more.

If you don't like this special array case, please use my old solution, that I improved, too:

var json = JSON.stringify(obj, function (k, v) { return k && v && typeof v !== "number" ? "" + v : v; }); // will expose arrays as strings.

Passing an array instead of an object in the top level will work nonetheless in both solutions.

EXAMPLES:

var obj = {
  keyA: "test",
  keyB: undefined,
  keyC: 42,
  keyD: [12, "test123", undefined]
}
obj.keyD.push(obj);
obj.keyE = obj;

var arr = [12, "test123", undefined];
arr.push(arr);

var f = function (k, v) { return k && v && typeof v !== "number" ? (Array.isArray(v) ? "[object Array]" : "" + v) : v; };
var f2 = function (k, v) { return k && v && typeof v !== "number" ? "" + v : v; };

console.log("object:", JSON.stringify(obj, f));
console.log("array:", JSON.stringify(arr, f));
console.log("");
console.log("with array string cast, so the array gets exposed:");
console.log("object:", JSON.stringify(obj, f2));
console.log("array:", JSON.stringify(arr, f2));

这篇关于限制JSON字符串化深度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆