问题
I have seen the terms "deserialize" and "serialize" with JSON. What do they mean?
(I'm new in JSON. Now, I need to use JSON for my Flash ActionScript 3.0. So I found one lib for JSON)
回答1:
JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object).
When transmitting data or storing them in a file, the data are required to be byte strings, but complex objects are seldom in this format. Serialization can convert these complex objects into byte strings for such use. After the byte strings are transmitted, the receiver will have to recover the original object from the byte string. This is known as deserialization.
Say, you have an object:
{foo: [1, 4, 7, 10], bar: "baz"}
serializing into JSON will convert it into a string:
'{"foo":[1,4,7,10],"bar":"baz"}'
which can be stored or sent through wire to anywhere. The receiver can then deserialize this string to get back the original object. {foo: [1, 4, 7, 10], bar: "baz"}
.
回答2:
In the context of data storage, serialization (or serialisation) is the process of translating data structures or object state into a format that can be stored (for example, in a file or memory buffer) or transmitted (for example, across a network connection link) and reconstructed later. [...]
The opposite operation, extracting a data structure from a series of bytes, is deserialization. From Wikipedia
In Python "serialization" does nothing else than just converting the given data structure (e.g. a dict
) into its valid JSON pendant (object).
- Python's
True
will be converted to JSONstrue
and the dictionary itself will then be encapsulated in quotes. - You can easily spot the difference between a Python dictionary and JSON by their Boolean values:
- Python:
True
/False
, - JSON:
true
/false
- Python:
- Python builtin module
json
is the standard way to do serialization:
Code example:
data = {
"president": {
"name": "Zaphod Beeblebrox",
"species": "Betelgeusian",
"male": True,
}
}
import json
json_data = json.dumps(data, indent=2) # serialize
restored_data = json.loads(json_data) # deserialize
# serialized json_data now looks like:
# {
# "president": {
# "name": "Zaphod Beeblebrox",
# "species": "Betelgeusian",
# "male": true
# }
# }
Source: realpython.com
回答3:
Python Example using Python Standard Library
In python, pickle module is used for serialization. So, the serialization process is called pickling in Python. This module is available in Python standard library.
Serialization using pickle
import pickle
#the object to serialize
example_dic={1:"6",2:"2",3:"f"}
#where the bytes after serializing end up at, wb stands for write byte
pickle_out=open("dict.pickle","wb")
#Time to dump
pickle.dump(example_dic,pickle_out)
#whatever you open, you must close
pickle_out.close()
The PICKLE file (can be opened by a text editor like notepad) contains this (serialized data):
€}q (KX 6qKX 2qKX fqu.
Deserialization using pickle
import pickle
pickle_in=open("dict.pickle","rb")
get_deserialized_data_back=pickle.load(pickle_in)
print(get_deserialized_data_back)
Output:
{1: '6', 2: '2', 3: 'f'}
回答4:
Try this one:
Dim jss As New JavaScriptSerializer()
Dim jcon = "{""type"":""clientlist"",""client_list"":[""client 1"",""client 2""]}"
Dim oListjs As List(Of MyModel) = jss.Deserialize(Of List(Of MyModel))(jcon)
Public Class MyModel
Public Property Type() As String
Public Property Client_list() As String()
End Class
来源:https://stackoverflow.com/questions/3316762/what-is-deserialize-and-serialize-in-json