Javascript 什么是数据序列化?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11817950/
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
What is data serialization ?
提问by Dovydas Navickas
First of all, I could not able to get clear definition of it from WikiPediaor even from serializefunction in the PHP manual. I need to know some cases we need the term serialization and how things are going without it? In other word, Where you must need serialization and without it your code will be missing some important feature.
首先,我无法从维基百科甚至PHP 手册中的序列化函数中获得明确的定义。我需要知道在某些情况下我们需要术语序列化以及没有它的情况如何?换句话说,在您必须需要序列化的地方,如果没有它,您的代码将缺少一些重要的功能。
回答by Dovydas Navickas
What is serialization?
什么是序列化?
Serialization encodes objects into another language.
For example you have an array in PHP like this:
序列化将对象编码为另一种语言。
例如,您在 PHP 中有一个数组,如下所示:
$array = array("a" => 1, "b" => 2, "c" => array("a" => 1, "b" => 2));
And then you want to store it in file or send to other application.
然后您想将其存储在文件中或发送到其他应用程序。
There are several language choices, but the idea is the same:
The array has to be encoded (or translated), into text or bytes, that can be written to a file or sent via the network.
For example, in PHP, if you:
有多种语言选择,但思想是相同的:必须将数组编码(或翻译)为文本或字节,然后将其写入文件或通过网络发送。
例如,在 PHP 中,如果您:
$data = serialize($array);
you will get this:
你会得到这个:
a:3:{s:1:"a";i:1;s:1:"b";i:2;s:1:"c";a:2:{s:1:"a";i:1;s:1:"b";i:2;}}
This is PHP's particular serializing format that PHP understands, and it works vice versa, so you are able to use it to deserialize objects.
For example, you stored a serialized array in a file, and you want it back in your code as an array:
这是 PHP 能够理解的 PHP 特定序列化格式,反之亦然,因此您可以使用它来反序列化对象。
例如,您在文件中存储了一个序列化数组,并且希望将其作为数组返回到代码中:
$array = unserialize($data);
But you could choose a different serialization format, for example, JSON:
但是您可以选择不同的序列化格式,例如 JSON:
$json = json_encode($array);
will give you this:
会给你这个:
{"a":1,"b":2,"c":{"a":1,"b":2}}
The result is not only easily saved, read by human eye, or sent via network, but is also understandable to almost every other language (JavaScript, Java, C#, C++, ...)
结果不仅易于保存、人眼读取或通过网络发送,而且几乎所有其他语言(JavaScript、Java、C#、C++ 等)都可以理解
Conclusion
Serialization translations objects to another language, in case you want to store or share data.
结论
序列化将对象转换为另一种语言,以防您想要存储或共享数据。
Are there any situations, where you cannot do anything, but serialize it?
有没有什么情况,你不能做任何事情,但序列化它?
No. But serialization usually makes things easier.
不会。但是序列化通常会使事情变得更容易。
Are JSON and PHP format the only possible formats?
No, no, no and one more time no. There are plenty of formats.
JSON 和 PHP 格式是唯一可能的格式吗?
不,不,不,还有一次不。有很多格式。
- XML which has successors like SOAP, WSDL, etc (those have particular purpose)
- Bytes
- ...
- ...
- ...
- Your own formats (you can create your own format for serialization and use it, but that is a big thing to do and is not worth it, most of the time)
- XML 具有 SOAP、WSDL 等后继者(具有特定目的)
- 字节
- ...
- ...
- ...
- 您自己的格式(您可以创建自己的序列化格式并使用它,但大多数情况下这是一件大事而且不值得)
回答by Darin Dimitrov
Serialization is the process of converting some in-memory object to another format that could be used to either store in a file or sent over the network. Deserialization is the inverse process meaning the actual object instance is restored from the given serialized representation of the object. This is very useful when communicating between various systems.
序列化是将某些内存中的对象转换为另一种格式的过程,该格式可用于存储在文件中或通过网络发送。反序列化是相反的过程,这意味着实际的对象实例是从对象的给定序列化表示中恢复的。这在各种系统之间进行通信时非常有用。
The serialization format could be either interoperableor non-interoperable. Interoperable formats (such as JSON, XML, ...) allow for serializing some object using a given platform and deserializing it using a different platform. For example with JSON you could use javascript to serialize the object and send it over the network to a PHP script that will deserialize the object and use it.
序列化格式可以是可互操作的或不可互操作的。可互操作的格式(例如 JSON、XML 等)允许使用给定平台序列化某些对象并使用不同平台对其进行反序列化。例如,对于 JSON,您可以使用 javascript 序列化对象并通过网络将其发送到 PHP 脚本,该脚本将反序列化对象并使用它。
The serialize()
PHP function uses an non-interoperable format. This means that only PHP could be used to both serialize and deserialize the object back.
该serialize()
PHP函数使用非可互操作的格式。这意味着只有 PHP 可以用于序列化和反序列化对象。
You could use the json_encode
and json_decode()
functions in order to serialize/deserialize PHP objects using the JSON interoperable format.
您可以使用json_encode
和json_decode()
函数来使用 JSON 互操作格式序列化/反序列化 PHP 对象。
回答by Mahn
Serialization is the process of turning data (e.g. variables) into a representation such as a string, that can easily be written and read back from for example a file or the database.
序列化是将数据(例如变量)转换为字符串等表示的过程,该表示可以轻松地从例如文件或数据库中写入和读取。
Use cases? There are many, but generally it revolves around the idea of taking a complex, nested array or object and turning it into a simple string that can be saved and read later to retrieve the same structure. For example, provided you have in php:
用例?有很多,但通常它围绕着将复杂的嵌套数组或对象转换成一个简单的字符串的想法,该字符串可以保存并在以后读取以检索相同的结构。例如,假设您在 php 中有:
$blub = array();
$blub['a'] = 1;
$blub['a']['b'] = 4;
$blub['b'] = 27;
$blub['b']['b'] = 46;
Instead of going through every array member individually and writing it one could just:
与其单独遍历每个数组成员并编写它,不如:
$dataString = serialize($blub);
And the serialized array is ready to be written anywhere as a simple string, in such a way that retrieving this string again and doing unserialize() over it gets you the exact same array structure you had before. Yes, it's really that simple.
并且序列化的数组已准备好作为一个简单的字符串写入任何地方,这样再次检索该字符串并对其执行 unserialize() 可以获得与之前完全相同的数组结构。是的,就是这么简单。
回答by hakre
I need to know some cases we need the term serialization and how things are going without it?
我需要知道在某些情况下我们需要术语序列化以及没有它的情况如何?
Serialization can become handy if you need to store complete structures (like an invoice with all associated data like customer address, sender address, product positions, tax caclulcations etc) that are only valid at a certain point in time.
如果您需要存储仅在特定时间点有效的完整结构(例如带有所有关联数据(如客户地址、发件人地址、产品位置、税收计算等)的发票),序列化会变得很方便。
All these data will change in the future, new tax regulations might come, the address of a customer changes, products go out of life. But still the invoice needs to be valid and stored.
所有这些数据在未来都会发生变化,新的税收法规可能会出现,客户地址发生变化,产品报废。但是发票仍然需要有效并存储。
This is possible with serialization. Like a snapshot. The object in memory are serialized into a (often like in PHP) binary form that can be just stored. It can be brought back to live later on (and in a different context). Like with this invoice example: In ten years, the data can still be read and the invoice object is the same as it was ten years earlier.
这可以通过序列化实现。像一张快照。内存中的对象被序列化为(通常像在 PHP 中一样)二进制形式,可以直接存储。它可以在以后(并在不同的上下文中)重新使用。就像这个发票示例:十年后,仍然可以读取数据,并且发票对象与十年前相同。
In other word, Where you must need serialization and without it your code will be missing some important feature.
换句话说,在您必须需要序列化的地方,如果没有它,您的代码将缺少一些重要的功能。
That was one example. It's not that you always needs that, but if things become more complex, serialization can be helpful.
那是一个例子。并不是你总是需要它,但如果事情变得更复杂,序列化可能会有所帮助。
回答by Robin Maben
Since you've tagged it with javascript
, one kind of serialization could be form serialization.
由于您已经用 标记了它javascript
,因此一种序列化可能是表单序列化。
Here are the references for the jQueryand prototype.JSequivalents.
以下是jQuery和prototype.JS等价物的参考。
What they basically do is serialize form input values into comma-separated name-value pairs.
他们基本上做的是将表单输入值序列化为逗号分隔的名称-值对。
So considering an actual usage..
所以考虑实际使用..
$.ajax({
url : 'insert.php?a=10,b=15' //values serialized via .serialize()
type: 'GET'
});
And you would probably do $GET["a"]
to retrieve those values, I'm not familiar with PHP though.
您可能会$GET["a"]
检索这些值,但我不熟悉 PHP。