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 inside a toJSON method of an object (without having to hide the toJSON method itself during the stringify operation)?
但是......那似乎是错的。所以,我的问题是:有没有什么方法可以在对象的toJSON方法中利用JSON.stringify的漂亮的内置序列化功能(在stringify操作期间不必隐藏toJSON方法本身)?
1 个解决方案
#1
34
Crockford's json2.js says:
克罗克福德的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.toJSON
should simply return the portion of the baz
object that you want serialized:
因此,您只需要返回要序列化的值。在您的情况下,baz.toJSON应该只返回您想要序列化的baz对象的部分:
someObject.baz.toJSON = function() {
return { foo: this.foo };
};
#1
34
Crockford's json2.js says:
克罗克福德的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.toJSON
should simply return the portion of the baz
object that you want serialized:
因此,您只需要返回要序列化的值。在您的情况下,baz.toJSON应该只返回您想要序列化的baz对象的部分:
someObject.baz.toJSON = function() {
return { foo: this.foo };
};