1: Serialization and deserialization
![](https://img-blog.csdnimg.cn/a09c926e79794b7c9af5687a5f74f058.png)
Serialization:
The process of converting an object into a sequence of bytes
Deserialization
: The process of restoring a byte sequence into an object
Note: All serialization and deserialization operations are attribute-based and have nothing to do with type behavior.
Purpose
: persistence (persistence is the attribute of this type)
(For example: photos and images are left, but they cannot show many events in growth)
High-level languages: object-oriented, process-oriented, object-based
Format
: xml,
Json (represents
Javascript
object but
Javascript
does not have new object language, so I came up with json string)
Structure
: key-value pair structure
Advantages:
Can represent very complex data
2: Json syntax
1: Punctuation marks
{ } indicates that this is an object of type
[ ] represents an array or collection
: Used to separate keys and values
" " is used in attribute names
, each key-value pair is separated by a comma (
the last key-value pair has no comma
)
2: Attribute value types allowed by Json
string √ char×
Integer Decimal √
bool √
Array, set√
Custom type√
Note: green wavy lines represent syntax errors
3: Small exercises (1)
Step 1: Write a Json file. You can write it in document txt first, and then change the suffix to json.
![](https://img-blog.csdnimg.cn/a2e4f91879504eaea4971ec8fdee2601.png)
Step 2: Add reference to C# script
in
+using Newtonsoft.Json
;
![](https://img-blog.csdnimg.cn/64310d66927a4ed2a0a2270c1c819e9b.png)
Step 3: Create the class
![](https://img-blog.csdnimg.cn/f234a179f947442d810f0a08787eada1.png)
NOTE: Right click on the json file and always copy![]()
The json file and the above attributes must correspond one-to-one
![](https://img-blog.csdnimg.cn/f22fefe3d6e4496ab5268592f0433ff4.png)
Supplementary writing summary: (First fold, look at the brackets, and write from the inside to the outside)
The first:
The second type:
The third type:![]()
Step 4: Analysis
Corresponding to the first type above:
![](https://img-blog.csdnimg.cn/3ef685b0acb143fab1714d0622f07ac5.png)
Corresponding to the second type above:
Corresponding to the third type above: [ The object contains a collection, so another class needs to be written]
![](https://img-blog.csdnimg.cn/2e428a90541944abadb8aeffff62c9de.png)
![](https://img-blog.csdnimg.cn/26a612307e4041dcb2f39bd510b02c5e.png)
Because there is a collection, stringbuilder is used
Notice:
1: Matching class:
When declaring a type, all attribute names in json and the attribute names in the class must be consistent.
2: The abstract method is not in the ordinary class and cannot match json parsing. Use virtual methods.
4: Small exercises (2)
If there is a collection name in the file:
![](https://img-blog.csdnimg.cn/9e48e752ea5c4766930172d8997b59bd.png)
(Note: Not all class libraries can parse double using
float
)
1 First create the tank class and then write a class class
![](https://img-blog.csdnimg.cn/7e4a7755e59d4d04a8a85ee15da857ba.png)
![](https://img-blog.csdnimg.cn/26ab40396bed4e209739a9f08dd8c85f.png)
2Declare a collection
![](https://img-blog.csdnimg.cn/aee6f305b58242c19b530bf4e499ee55.png)
If there is no collection name in the file:
![](https://img-blog.csdnimg.cn/936af90aef664e92a8d1d2d88c6d59a5.png)
1 First write a weapon class
![](https://img-blog.csdnimg.cn/b74a1100ab5641b9a79542798f4080f4.png)
2 Store directly in the main function
![](https://img-blog.csdnimg.cn/08dfd68bbfe04a558664b5f3ff85284f.png)