Research on YAML Technology

Source: Internet
Author: User
Tags comparison table format definition

YAML Pre-research document YAML Overview

YAML is a recursive abbreviation for "Yaml Ain t a Markup Language" (Yaml is not a set-up language), and the previous yaml means "yet another Markup Language" (another kind of labeling language), But in order to emphasize that the language is centered on data, rather than focusing on the labeling language, the official definition of YAML is simple, that is, a user-friendly data format definition language whose main function is similar to XML or JSON, Yaml separates data with whitespace characters and branches, and cleverly avoids various enclosing symbols, such as quotation marks, parentheses, and so on, to prevent these symbols from becoming illegible in complex hierarchies. Yaml's syntax is similar to a higher-order language in that it is simple to describe a sequence (list in Java), a hash table (a map in Java), a scalar (basic type in Java, and so on) , which emphasizes readability.

YAML vs XML

Data format definition language similar to YAML is XML,YAML than XML superiority in

Advantage:

  • Good readability of Yaml
  • The interactivity between Yaml and scripting language is good
  • YAML uses the data type of the implementation language
  • YAML has a consistent information model.
  • Yaml is easy to implement

The above 5 is where XML is not enough, and YAML has the following advantages of XML:

  • YAML can process based on streams
  • Yaml has strong expressive ability and good extensibility

Yaml is similar to XML's data Description language, syntax is much simpler than XML, and Yaml tries to accomplish the task of XML in a more agile way than XML.

YAML vs JSON

The JSON syntax is actually a subset of YAML, and most JSON files can be parsed by Yaml's profiler. Although a JSON-like format can be used for most data layering, YAML does not recommend this, unless it adds to the readability of the file, and more importantly, many of YAML's extensions are not found in JSON, such as: Advanced Data Form , relationship anchor Point , string does not need quotation marks , map data form will store the order of key values .

YAML Purpose scripting language

Because the implementation is simple and the parsing cost is low, YAML is particularly suitable for use in scripting languages. Make a list of the existing language implementations: Ruby,java,perl,python,php,ocaml,javascript, except for Java, the others are scripting languages.

Serialization of

Yaml is more suitable for serialization. Because it is the host language data type goes straight.

Configuration file

Yaml makes configuration files also good. Writing Yaml is much faster than writing XML ( no need to focus on tags or quotes ) and is more powerful than INI documentation.

Debugging

Because of its strong reading ability, it is also a convenient way to use dump information for analysis during debugging.

Yaml defects and deficiencies

YAML does not have its own definition of the data type, but rather uses the data type of the implementation language. A yaml file, the data types that are parsed in different languages may be different, and because of their compatibility problems, the data flow between different languages is not recommended for use with YAML.

YAML syntax and examples
  • YAML uses printable Unicode characters and can use UTF-8 or UTF-16
  • Use white-space characters (not used Tab ) for layering, aligned to the left of the same layer elements
  • Single-line annotations start with a well font size (  # ) and can appear anywhere in the row
  • Each manifest member is represented by a single line, with a short slash + blank ( -  ) Start
  • Members of each hash table :  separate keys and values with colons + blanks ()
  • The key value of a hash table can be started with a question mark ( ? ), representing a key value of multiple words
  • Strings generally do not use quotation marks, but you can enclose them in quotation marks when necessary.
  • Use a backslash ( \ ) to escape special characters when using double quotation marks to denote a string
  • Chunk strings are delimited with other data by indentation and modifiers (not necessary), with new rows reserved (using symbols | ) or new rows collapsed (using symbols > ) in two ways
  • In a single file, multiple files can be distinguished by three consecutive hyphens ( --- )
  • The optional three consecutive dots ( ... ) are used to indicate the end of the file (useful when streaming, and you don't need to close the stream to know where the end is)
  • Duplicate content can be copied from the reference mark asterisk ( * ) to the anchor Mark ( & )
  • The specified format can use two exclamation points (!!), followed by a name
Receipt:oz-ware Purchase InvoiceDate2007-18L06customer:given:dorothy family:galeitems:-part_no:a4786 descrip:water Bucket (Filled) Price: 1.47 Quantity: 4-part_no:e1628 Descrip:high heeled " Ruby "Slippers Price: 100.27 Quantity: 1bill-< Span class= "Hljs-keyword" >to: &id001 Street: | 123 Tornado Alley Suite 16 city:east Westville State:ksship-to: *id001 specialdelivery: > Follow the Yellow Brick Road to the Emerald City. Pay no attention to the man behind Span class= "Hljs-keyword" >the curtain ....            

The top level of this file consists of seven key values: one of the key values, "items", is a list of two elements, and the two elements in the list are also hash tables containing four key values.
Repeated partial processing of the file: Use the Anchor Point (&) and the reference (*) tag to copy the contents of the "bill-to" hash table to the "ship-to" hash table. Optionally, blank lines can be added to the file for readability.

The Java implementation of YAML

YAML has been implemented in many languages, see the official website of Yaml.
The general yaml file extension is. yaml, such as John.yaml, whose contents are:

name: John Smithage: 37children:    - name: Jimmy Smith      age: 15    - name: Jenny Smith age: 12spouse: name: Jane Smith age: 25

Due to the super readability of Yaml, we learned that John is 37 years old, two kids Jimmy and Jenny are lively and cute, his wife Jane is young and beautiful, and she is only 25 years old, a happy four-mouth home.
Java description of the John.yaml, abstract a person class, as follows:

public class Person {    private String name;    private int age; private Person sponse; private Person[] children; // setXXX, getXXX方法略.}

Now we use Java to assemble a jone:

Person John = new Person (); John. Setage (67L; John. SetName ("John Smith"); Person sponse = new Person (); Sponse. SetName ("Jane Smith"); Sponse. Setage (25).setsponse (sponse) 0]< Span class= "Hljs-preprocessor" >.setname ( "Jimmy Smith") 0].setage (15) Span class= "hljs-comment" >; Children[1].setname ( "Jenny Smith ") 1]12)           
Using the SNAKEYAML implementation

Project home: http://code.google.com/p/snakeyaml/
User manual: Https://code.google.com/p/snakeyaml/wiki/Documentation
Snakeyaml is a standard YAML Java implementation that has the following features:

  • Full support for YAML 1.1, which can run through all the examples in the specification
  • Supports all types of YAML
  • Support for utf-8/utf-16 input and output
  • A high-level API that provides serialization and deserialization of native Java objects
  • Provide a relatively reasonable error message

Use Snakeyaml to dump John out, and if you have references to the same object, the dump out to the Yaml file will automatically use & and * make the anchor points and references :

DumperOptions options = new DumperOptions();options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);Yaml yaml = new Yaml(options);//Yaml yaml = new Yaml();String dump = yaml.dump(john);System.out.println(dump);

The contents are as follows:

37children:- age: 15  children: null  name: Jimmy Smith  sponse: null- age: 12 children: null name: Jenny Smith sponse: nullname: John Smithsponse: age: 25 children: null name: Jane Smith sponse: null

Now with Snakeyaml, Yaml load comes in, and if the Yaml file is used, the & * same value is automatically assigned to the Load object :

load = yaml.load(new FileInputStream(new File("jhon.yaml")));System.out.println(load.getClass());System.out.println(yaml.dump(load));

Or

Yaml yaml = new Yaml(options);Person person = yaml.loadAs(inputStream, Person.class);System.out.println(person.getSponse().getChildren().length);

If there is more than one document in a Yaml file, it is --- resolved by splitting it as follows:

Yaml yaml = new Yaml();        int counter = 0;        for (Object data : yaml.loadAll(input)) { System.out.println(data); counter++; }

Save a Map object:

map<string, object> data = new Hashmap<string, object> (); Data.put (" name ", " Silenthand Olleander "); Data.put ( "race",  "Human"); Data.put (  "traits", new string[] { "One_hand",  "One_eye"}); Yaml Yaml = new Yaml (); string output = yaml.dump (data); SYSTEM.OUT.PRINTLN (output); //or StringWriter writer = new StringWriter (); Yaml.dump (data, writer); System.out.println (Writer.tostring ());            

Dump multiple documents into the same Yaml file:

list<integer> docs = new Linkedlist<integer> (); for (int i =1; i < 4; i++) {Docs.add (i ) .explicitstart (True) .out.println (Yaml .dump (Docs)) .out.println (Yaml .dumpall (Docs.iterator ()))              
--- [1, 2, 3]--- 1--- 2--- 3

Yaml vs. Java type comparison table:

YAML JAVA
!null Null
!! bool Boolean
!! Int Integer, Long, BigInteger
!! Float Double
!! Binary String
!! Timestamp Java.util.Date, Java.sql.Date, Java.sql.Timestamp
!! OMAP,!! Pairs List of object[]
!! Set Set
!! Str String
!! Seq List
!! Map Map

The default implementation of the collection is:

  • List:arraylist
  • Map:linkedhashmap
Using the JYAML implementation

Jyaml (The latest version is 2007, you can consider giving up ), use Jyaml to put Jone "Dump" Out:

    File dumpfile = new File("John_dump.yaml");    Yaml.dump(john, dumpfile);

Now let's see what John_dump.yaml looks like:

---! yaml. Test. Internal. Personage: 37children:!yaml.test.internal.test.internal15 name:jimmy Smith-!yaml.test.internal12 name:jenny smithname:john smithsponse:!yaml.test.internal25 name:jane Smith         

Where!yaml.test.internal.person is some type of information. The load needs to be used.
Now use Jyaml to get Jone_dump.yaml load in:

    Person john2 = (Person) Yaml.loadType(dumpfile, Person.class);

You can also use the following code to dump a john.yaml with no type information:

true);

Let's take a look at the support for JYAML convection processing, for the sake of simplicity, we just wrote the same John 10 times:

    YamlEncoder enc = new YamlEncoder(new FileOutputStream(dumpfile));    for(int i=0; i<10; i++){ john.setAge(37+i); enc.writeObject(john); enc.flush(); } enc.close();

Now read the 10 objects one at a time (note how the while loop exits):

   new YamlDecoder(new FileInputStream(dumpfile));   int age = 37;   while(true){ try{ john = (Person) dec.readObject(); assertEquals(age, john.getAge()); age++; }catch(EOFException eofe){ break; } }

Research on YAML Technology

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.