JSON会演变为对Ecmascript映射对象具有本地支持吗? [英] Will JSON Evolve to Have Native Support for Ecmascript Map Objects?

查看:127
本文介绍了JSON会演变为对Ecmascript映射对象具有本地支持吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何正式的提议在进行中,以解决对JSON当前对Map对象的处理的向后兼容演变?

Are there any formal proposals, in progress, that address a backwards-compatible evolution to JSON's current treatment of Map objects?

例如,假设您要将Map转换为JSON,然后将其写入文件:

For example, let say you want to convert a Map to JSON and then write it to file:

let map = new Map();
map.set(1, "A");
map.set(2, "B");
map.set(3, "C");

// Convert map to an "array of 2-element arrays":
let arrayFromMap = [... map];

let json = JSON.stringify(arrayFromMap);
writeJSONtoFile(json,"path/to/jsonFile.json");

所以现在磁盘上有一个JSON文件。问题是,最终读取该文件的代码不知道它包含Map对象,除非我们使该代码文件具有这种意识。 JSON中没有固有的内容来明确指示 Map 而不是 2元素数组的数组。例如,不具有意识的代码可能会这样做:

So now we have a JSON file sitting on the disk. The issue is that, the code that ultimately reads this file has no knowledge that it contains a Map object unless we give that code-file that awareness. There is nothing inherent in JSON to explicitly indicate Map instead of a "array of 2-element arrays". For example, code not having this awareness might do this:

let json = readJSONfromFile("path/to/jsonFile.json");
let parsedJSON = JSON.parse(json);
console.log(parsedJSON); // outputs an "array of 2-element arrays"

但是,如果代码编写者给出了代码意识(原始类型是Map),可以将文件转换回Map:

However, if the the code writer gives the code awareness (that the original type was a Map) the file can be converted back to a Map:

let json = readJSONfromFile("path/to/jsonFile.json");
let map = new Map(JSON.parse(json));

这些内置的意识并非必需:对象

This same awareness isn't necessary for these built-ins: Objects and Arrays.

在上面的示例中,这种意识要求并不是很繁重。但是,请想象一个既包含Maps又不包含Maps的 2元素数组的数组的大型层次结构。现在,这种意识负担已扩展到层次结构中的每个嵌套地图

In the example above, this "awareness" requirement isn't too burdensome. However, imagine a large hierarchy that contains both Maps and "arrays of 2-element arrays" that are not intended to be Maps. This "awareness burden" has now extended to each nested Map within the hierarchy.

是否有任何正式的提议在进行中,以解决对JSON当前对Map对象的处理的向后兼容演变?

Are there any formal proposals, in progress, that address a backwards-compatible evolution to JSON's current treatment of Map objects?

推荐答案

否,因为JSON表示法起源于Javascript,但它在许多语言中得到了广泛使用,但是Javascript Map仅在Javascript上下文中具有含义。

No, because JSON notation, while it originated with Javascript, is widely used in a very large number of languages, but a Javascript Map only has meaning within the context of Javascript.

对JSON表示法进行任何形式的更改,以允许对仅在Javascript中具有含义的对象进行序列化和反序列化,将使JSON格式与大多数其他语言不兼容。

Any sort of change to JSON notation to allow for the serialization and deserialization of objects that only have meaning in Javascript would make that JSON format incompatible with most other languages.

这并不是说不能编写基于JSON的自定义解析器来正确地序列化和反序列化Map,但是这样的事情只能基于基于JSON ,而不是

This isn't to say that a custom parser based on JSON couldn't be written to properly serialize and deserialize Maps, but such a thing would only be based on JSON, rather than being an addition to the JSON standard.

这篇关于JSON会演变为对Ecmascript映射对象具有本地支持吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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