Serialization format: XML, JSON, YAML

Source: Internet
Author: User
Tags yaml parser
Serialization in. NET is usually in binary or xml format. After ajax emerged, the JSON format is also used, which is rare compared with YAML.

Binary

Cannot be directly read. It must be decoded at the binary level;
The format is defined by the vendor. When the objects to be represented by a specific application are complex, the format is also complex, such as the office file format;
We usually need to fully consider the Protocol's scalability and compatibility, such as the windows file format, DOS header, COFF header, PE, CLR header, and so on;
Compared with the text format, it is small in size and can be more efficient in encoding and decoding;

XML

Text protocol, which can be read;
Strict format requirements;
Widely used and rich related technologies, such as DTD, XPath, XLink, XPoint, and XSLT;

JSON

Text protocol, easy to read;
Compared with XML, the syntax is simpler and smaller, with standard support for the javascript language. Lack of reference concepts (XLink and XPoint) and path concepts (XPath );
XML is used for more general purposes. JSON is more suitable for Data Interaction environments (especially web environments );

JSON is based on the javascript language ECMA 262 3rd Edition and tends to become a cross-Language Data Interaction format.
The complete format is initially defined by RFC4627. For more information about syntax digoal and supported class libraries in different languages, see json.org.

JSONP

JSONP only adds something on the basis of JSON to facilitate interaction of JSON data between sites.
For example, url: http://domain1.com/getjsonreturns A jsonobject {"Name": "Cheeso", "Rank": 7}. JSONP can be referenced as follows:
<Script type = 'text/javascript 'src = 'HTTP: // domain1.com/getjson? Jsonp = parseResponse '> </script>
Returned data parseResponse ({"Name": "Cheeso", "Rank": 7 })

YAML

Text protocol, easy to read;
The syntax of YAML is more complex than that of JSON. JSON can be considered as a subset of YAML. Because the syntax specification is complex, different YAML libraries may process some features differently.

For complete YAML specifications and language support class libraries, refer to yaml.org
For more information about simple syntax elements, see YAML RefCard.
For the syntax tree of the YAML Parser, refer to YAML Reference Parser. You can directly input the YAML text or upload a YAML file, click preview to view the syntax tree structure after parsing by the parser and the relationship between each node and YAML.

YAML document example:

---
receipt: Oz-Ware Purchase Invoice
date: 2007-08-06
customer:
given: Dorothy
family: Gale
items:
- part_no: A4786
descrip: Water Bucket (Filled)
price: 1.47
quantity: 4
- part_no: E1628
descrip: High Heeled "Ruby" Slippers
price: 100.27
quantity: 1
bill-to: &id001
street: |
123 Tornado Alley
Suite 16
city: East Westville
state: KS
ship-to: *id001
specialDelivery: >
Follow the Yellow Brick
Road to the Emerald City.
Pay no attention to the
man behind the curtain.
---

Different YAML parser may extend some different features. Below is a brief description of the basic YAML Syntax:
1. The comment starts with the # symbol and ends at the end of the line.

2. Simple data (scalars, scalar data) can be enclosed without quotation marks, including string data. It is enclosed in single or double quotation marks as a string, and C-style escape characters are used in single or double quotation marks.

3. The specified data type can be displayed, and the user-defined data types supported by the YAML parser can also be used.

---
A: 123 # Integer type
B: "123" # string type, enclosed in double quotation marks
C: 123.0 # Floating Point Type
D :!! Float 123 # specify the display as a floating point type
E :!! Str 123 # specify the display as a string type
F :!! Str Yes # The display is specified as a string type
G: Yes # a boolean True
H: Yes we have No bananas # string, where Yes and No are part of the string
# Binary type
Picture :!! Binary |
R0lGODlhDAAMAIQAAP // 9/X
17unp5WZmZgAAAOfn515eXv
Pz7Y6OjuDg4J + fn5OTk6enp
56 enmleECcgggoBADs = mZmE
MyObject :! MyClass {name: Joe, age: 15} # use a Custom Data Type

4. Text block
A). newline preserved. Mark with a vertical line

--- |
There once was a man from Darjeeling
Who got on a bus bound for Ealing
It said on the door
"Please don't spit on the floor"
So he carefully spat on the ceiling

By default, the blank characters at the start and end positions of each line are removed, and the line breaks at the end of each line are retained.
The conversion from the preceding example to html should be similar to the following:

B). newline folded. Mark with right angle brackets

--- >
Wrapped text
will be folded
into a single
paragraph
Blank lines denote
paragraph breaks

By default, the blank characters at the beginning and end of each line are removed. The line breaks at the end of each line are replaced with a space. The blank lines are marked as a paragraph.
The conversion from the preceding example to html should be similar to the following:

Wrapped text will be folded into a single paragraph

Blank lines denote paragraph breaks

The string converted into a programming language should be similar to the following:

"Wrapped text will be folded into a single paragraph\nBlank lines denote paragraph breaks"

You can use the plus sign (+) or minus sign (-) to control the behavior of line breaks and paragraphs.

5. Hash
A). Use the key: value Method. Each row provides a value.
B). Representation of JSON objects

--- # Indented Blocks, common in YAML data files, use indentation and new lines to separate the key: value pairs
name: John Smith
age: 33
--- # Inline Blocks, common in Yaml data streams, use commas to separate the key: value pairs between braces
{name: John Smith, age: 33}

6. List
A). Use a hyphen + space. Each line provides a list element.
B). Similar to the JSON array Representation

--- # Favorite movies
- Casablanca
- North by Northwest
- The Man Who Wasn't There
--- # Shopping list
[milk, pumpkin pie, eggs, juice]

Lists of hashes

- {name: John Smith, age: 33}
- name: Mary Smith
age: 27

Hashes of lists

men: [John Smith, Bill Jones]
women:
- Mary Smith
- Susan Williams

7. Data Reference and merge
You can use the & symbol to define a reference tag, use the symbol * to reference the data of this tag, and use the symbol <to merge hash values, for example:

# sequencer protocols for Laser eye surgery
---
- step: &id001 # defines anchor label &id001
instrument: Lasik 2000
pulseEnergy: 5.4
pulseDuration: 12
repetition: 1000
spotSize: 1mm
- step:
<<: *id001 # merges key:value pairs defined in step1 anchor
spotSize: 2mm # overrides "spotSize" key's value
- step:
<<: *id001 # merges key:value pairs defined in step1 anchor
pulseEnergy: 500.0 # overrides key
alert: > # adds additional key
warn patient of
audible pop

& Id001 defines an id001 reference tag (referencing all attributes of the first step element in the Document). The second step element references id001 and then overrides the spotSize attribute; after the third step element references id001, rewrite the pulseEnergy attribute and add the alert attribute.

8. Multiple YAML documents can be placed in one file and separated by --- (an YAML document can be understood as an object similar to JSON)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.