JSON.stringify与序列化 [英] JSON.stringify versus serialization

查看:121
本文介绍了JSON.stringify与序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JSON.stringify( )等同于序列化或有效地序列化,还是只是朝着序列化的必要步骤 序列化?

Is JSON.stringify( ) equivalent to serialization or effectively serialization or is it just a necessary step towards serialization?

换句话说,JSON.stringify( )是否足够但不需要序列化?还是有必要但还不够?还是对JavaScript对象进行序列化既没有必要也不充分?

In other words, is JSON.stringify( ) sufficient but not necessary for serialization? Or is necessary but not sufficient? Or is it neither necessary nor sufficient for serialization of JavaScript objects?

推荐答案

串行化是将数据转换为可以写入磁盘或通过网络传输(如果需要的话,可以写在纸上)的格式的动作.通常,序列化会将对象转换为文本,但这不是必需的,因为存在多种序列化格式,例如bittorrent的 bencoding 和旧的/古老的标准 asn.1 格式均为二进制.

Serialization is the act of converting data into a format that can be written to disk or transmitted over the network (or written on paper if that's what you want). Usually, serialization is transforming objects to text but that's not necessary since there are several serialization formats such as bittorrent's bencoding and the old/ancient standard asn.1 formats are binary.

JSON 是一种基于文本的序列化格式,由于其简单性,目前非常流行.它不是唯一的.其他流行的格式包括XML和 CSV .

JSON is one form of text-based serialization format and is currently very popular due to it's simplicity. It's not the only one though. Other popular formats include XML and CSV.

由于它的流行以及它作为javascript对象文字语法的起源,ES5引入了JSON.stringify()从对象生成JSON字符串.以前,您必须使用库或编写递归下降解析器来完成这项工作.

Due to its popularity and its origin as javascript object literal syntax ES5 introduced JSON.stringify() to generate a JSON string from an object. Previously you had to use libraries or write a recursive descent parser to do the job.

那么,JSON.stringify()是否足以进行序列化?是的,如果您想要的输出格式是JSON.否,如果需要其他输出格式,例如XML或CSV或bencode.

So, is JSON.stringify() enough for serialization? Yes, if the output format you want is JSON. No, if you want other output formats such as XML or CSV or bencode.

JSON格式有局限性.一个限制是JSON无法编码函数,因此JSON.stringify()在序列化时会忽略函数/方法. JSON也无法编码循环引用.大多数其他序列化格式也具有此限制,但是由于JSON 看起来类似于javascript语法,因此某些人认为它可以执行javascript对象文字可以实现的功能.不能.

There are limitations to the JSON format. One limitation is that JSON cannot encode functions so JSON.stringify() ignores functions/methods when serializing. JSON also can't encode circular references. Most other serialization formats have this limitation as well but since JSON looks like javascript syntax some people assume it can do what javascript object literals can. It can't.

因此,"JSON"和序列化"之间的关系就像"Toyota Prius"和"car"之间的关系. JSON.stringify()只是一个生成JSON字符串的函数,所以我想这会使它成为Toyota工厂.

So the relationship between "JSON" and "serialization" is like the relationship between "Toyota Prius" and "car". JSON.stringify() is simply a function that generates JSON strings so I guess that would make it a Toyota factory.

这篇关于JSON.stringify与序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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