JSON.parse和JSON.stringify不是幂等的,这很糟糕 [英] JSON.parse and JSON.stringify are not idempotent and that is bad

查看:127
本文介绍了JSON.parse和JSON.stringify不是幂等的,这很糟糕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是多方面的-

(1a) JSON是JavaScript的基础,那么为什么没有JSON类型呢? JSON类型将是格式为JSON的字符串.在数据更改之前,它将被标记为已解析/已字符串化.数据一旦更改,就不会被标记为JSON,需要重新解析/重新字符串化.

(1a) JSON is fundamental to JavaScript, so why is there no JSON type? A JSON type would be a string that is formatted as JSON. It would be marked as parsed/stringified until the data was altered. As soon as the data was altered it would not be marked as JSON and would need to be re-parsed/re-stringified.

(1b)在某些软件系统中,是否有可能(偶然)尝试通过网络发送普通的JS对象而不是序列化的JS对象?为什么不尝试避免这种情况?

(1b) In some software systems, isn't it possible to (accidentally) attempt to send a plain JS object over the network instead of a serialized JS object? Why not make an attempt to avoid that?

(1c)为什么不能在不首先将其字符串化的情况下在一个直接的JavaScript对象上调用JSON.parse?

(1c) Why can't we call JSON.parse on a straight up JavaScript object without stringifying it first?

    var json = {   //JS object in properJSON format
        "baz":{
            "1":1,
            "2":true,
            "3":{}
        }
    };

    var json0 = JSON.parse(json); //will throw a parse error...bad...it should not throw an error if json var is actually proper JSON.

所以我们别无选择,只能这样做:

So we have no choice but to do this:

 var json0= JSON.parse(JSON.stringify(json));

但是,存在一些不一致之处,例如:

However, there are some inconsistencies, for example:

JSON.parse(true); //works
JSON.parse(null); //works
JSON.parse({}); //throws error

(2)如果我们继续在同一对象上调用JSON.parse,最终它将引发错误.例如:

(2) If we keep calling JSON.parse on the same object, eventually it will throw an error. For example:

var json = {   //same object as above
    "baz":{
        "1":1,
        "2":true,
        "3":{}
    }
};

var json1 = JSON.parse(JSON.stringify(json));
var json2 = JSON.parse(json1); //throws an error...why

(3)为什么JSON.stringify无限地在输入中添加越来越多的斜杠?不仅很难读取调试结果,而且实际上会使您处于危险状态,因为一个JSON.parse调用不会给您返回一个普通的JS对象,您必须多次调用JSON.parse才能返回到该状态.普通的JS对象.这很不好,这意味着在给定的JS对象上多次调用JSON.stringify是非常危险的.

(3) Why does JSON.stringify infinitely add more and more slashes to the input? It is not only hard to read the result for debugging, but it actually puts you in dangerous state because one JSON.parse call won't give you back a plain JS object, you have to call JSON.parse several times to get back the plain JS object. This is bad and means it is quite dangerous to call JSON.stringify more than once on a given JS object.

   var json = {
        "baz":{
            "1":1,
            "2":true,
            "3":{}
        }
    };

    var json2 = JSON.stringify(json);
    console.log(json2);

    var json3 = JSON.stringify(json2);
    console.log(json3);

    var json4 = JSON.stringify(json3);
    console.log(json4);

    var json5 = JSON.stringify(json4);
    console.log(json5); 

(4)在不更改结果的情况下应该能够反复调用的函数的名称是什么(IMO JSON.parseJSON.stringify应该如何表现)?正如您在评论中看到的那样,最好的术语似乎是幂等".

(4) What is the name for a function that we should be able to call over and over without changing the result (IMO how JSON.parse and JSON.stringify should behave)? The best term for this seems to be "idempotent" as you can see in the comments.

(5)考虑到JSON是可用于网络对象的序列化格式,因此您无法两次甚至在某些情况下一次调用JSON.parseJSON.stringify似乎非常疯狂.情况下不会引起一些问题.为什么会这样?

(5) Considering JSON is a serialization format that can be used for networked objects, it seems totally insane that you can't call JSON.parse or JSON.stringify twice or even once in some cases without incurring some problems. Why is this the case?

如果您正在发明Java,JavaScript或任何其他语言的下一种序列化格式,请考虑此问题.

If you are someone who is inventing the next serialization format for Java, JavaScript or whatever language, please consider this problem.

IMO对于给定的对象应该有两种状态.序列化状态和反序列化状态.在具有更强类型系统的软件语言中,这通常不是问题.但是在JavaScript中使用JSON时,如果在同一对象上调用JSON.parse两次,则会遇到致命异常.同样,如果在同一个对象上两次调用JSON.stringify,则可能会进入不可恢复的状态.就像我说的那样,应该只有两个状态和两个状态,即纯JS对象和序列化JS对象.

IMO there should be two states for a given object. A serialized state and a deserialized state. In software languages with stronger type systems, this isn't usually a problem. But with JSON in JavaScript, if call JSON.parse twice on the same object, we run into fatal exceptions. Likewise, if we call JSON.stringify twice on the same object, we can get into an unrecoverable state. Like I said there should be two states and two states only, plain JS object and serialized JS object.

推荐答案

1)JSON.parse需要一个字符串,您正在向其提供Javascript对象.

1) JSON.parse expects a string, you are feeding it a Javascript object.

2)与第一个类似.您将字符串输入需要对象的函数.

2) Similar issue to the first one. You feed a string to a function that needs an object.

3)Stringfy实际上需要一个字符串,但是您正在向它提供一个String对象.因此,它采用与第一个字符串相同的措施来转义引号和斜杠.使该语言可以理解引号和字符串中的其他特殊字符.

3) Stringfy actually expects a string, but you are feeding it a String object. Therefore, it applies the same measures to escape the quotes and slashes as it would for the first string. So that the language can understand the quotes, other special characters inside the string.

4)您可以为此编写自己的函数.

4) You can write your own function for this.

5)因为您正在尝试进行非法转换.这与第一个和第二个问题有关.只要提供了正确的对象类型,您就可以多次调用它.唯一的问题是多余的斜杠,但这实际上是标准.

5) Because you are trying to do a conversion that is illegal. This is related to the first and second question. As long as the correct object types are fed, you can call it as many times as you want. The only problem is the extra slashes but it is in fact the standard.

这篇关于JSON.parse和JSON.stringify不是幂等的,这很糟糕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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