Javascript 如何在自定义 toJSON 方法中使用 JSON.stringify?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4289282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:45:39  来源:igfitidea点击:

How do you use JSON.stringify in a custom toJSON method?

javascriptjson

提问by machineghost

So, JSON.stringify provides a great way to turn a JS object like:

因此,JSON.stringify 提供了一种很好的方法来转换 JS 对象,例如:

var baz = {"foo":1, "bar":someFunction};

in to a JSON string like:

到一个 JSON 字符串,如:

{"foo":1}

It does this with an optional second argument that controls which fields should be serialized:

它使用一个可选的第二个参数来控制哪些字段应该被序列化:

JSON.stringify(baz, ["foo"]);

That's great, but there's a problem. Let's say your "baz" is actually the property of another object, and you want to serialize that other object:

这很好,但有一个问题。假设您的“baz”实际上是另一个对象的属性,并且您想序列化该另一个对象:

someObject.baz = {"foo":1, "bar":someFunction};
JSON.stringify(someObject, ["baz"]);

Well, normally you would just define a toJSON method on baz, eg.:

好吧,通常你只需要在 baz 上定义一个 toJSON 方法,例如:

someObject.baz = {"foo":1, "bar":someFunction};
someObject.baz.toJSON = function() { /* logic to "toJSON" baz*/ }
JSON.stringify(someObject, ["baz"]);

Now, as I mentioned earlier, we have the perfect logic to "toJSON" baz already:

现在,正如我之前提到的,我们已经有了“toJSON” baz 的完美逻辑:

someObject.baz.toJSON = function() {
    return JSON.stringify(baz, ["foo"]);
}

but if you try putting that in to your toJSON, you'll get a recursion error, because stringify will trigger the toJSON, which will trigger the stringify, which will ... :-(

但是如果你尝试把它放到你的 toJSON 中,你会得到一个递归错误,因为 stringify 将触发 toJSON,这将触发 stringify,这将...... :-(

You can work around this with a hack:

您可以通过 hack 解决此问题:

someObject.baz.toJSON = function() {
    var oldToJON = this.toJSON;
    this.toJSON = null;
    var ret = JSON.stringify(baz, ["foo"]);
    this.toJSON = oldToJON;
    return ret;
}

But ... that just seems wrong. So, my question is: is there any way you can utilize the nifty built-in serialization power of JSON.stringify insidea toJSON method of an object (without having to hide the toJSON method itself during the stringify operation)?

但是……这似乎是错误的。所以,我的问题是:是否有可以利用漂亮的内置JSON.stringify序列化的功率任何方式内部对象的方法的toJSON(不必隐藏在字符串化操作过程中的toJSON方法本身)?

回答by casablanca

Crockford's json2.jssays:

Crockford 的 json2.js说:

A toJSON method does not serialize: it returns the value represented by the name/value pair that should be serialized, or undefined if nothing should be serialized.

toJSON 方法不会序列化:它返回由应该序列化的名称/值对表示的值,如果没有应该序列化,则返回 undefined。

So you are simply expected to return the value that you want serialized. In your case, baz.toJSONshould simply return the portion of the bazobject that you want serialized:

所以你只需要返回你想要序列化的值。在您的情况下,baz.toJSON应该简单地返回baz您想要序列化的对象部分:

someObject.baz.toJSON = function() {
  return { foo: this.foo };
};