RSS Feed for This PostCurrent Article

Java: Using YAML for Data Transfer

Download Sample

Previously I posted a few articles on using JSON for data transfer, in Java and .NET. Recently I have also tested YAML.

YAML stands for YAML Ain’t Markup Language. Well, sounds like GNU is not for UNIX :) . It is a human friendly data serialization standard for all programming languages.

Here I am going to show you a simple example using JYaml.

I defined my value object or data transfer object called Person, which has name, email

public class Person {
    private String name;
    private String email;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

To use YAML to serialize or deserialize object is very easy. JYaml supports

  • Primitives and respective wrapper classes
  • JavaBean compliant objects (from mappings)
  • Collection (from sequences)
    • List
    • Set
  • Map (from mappings)
  • Arrays (from sequences)
  • BigInteger and BigDecimal
  • Date
  • Custom Java Objects by implementing ObjectsWrappers yourself

To test it, see the followings

import org.ho.yaml.Yaml;

import java.util.ArrayList;
import java.util.List;

public class YamlTest {

    public static void main(String[] args) {
        // Create a single object
        Person me = new Person();
        me.setName("jason");
        me.setEmail("[email protected]");
        String yamlString = Yaml.dump(me);
        System.out.println(yamlString);

        // Create a list of objects
        Person another = new Person();
        another.setName("alice");
        another.setEmail("[email protected]");
        List<Person> personList = 
                       new ArrayList<Person>(2);
        personList.add(me);
        personList.add(another);
        String yamlList = 
                     Yaml.dumpStream(personList.iterator());
        System.out.println(yamlList);

        // Read a single object
        Person obj = (Person) Yaml.load(yamlString);
        System.out.println("name: " + obj.getName());
        System.out.println("email: " + obj.getEmail());

        System.out.println();
        
        // Read a list of objects
        for (Object object : Yaml.loadStream(yamlList)) {
            Person p = (Person) object;
            System.out.println("name: " + p.getName());
            System.out.println("email: " + p.getEmail());
        }
    }
}

The output

--- !Person
email: [email protected]
name: jason

--- !Person
email: [email protected]
name: jason
--- !Person
email: [email protected]
name: alice

name: jason
email: [email protected]

name: jason
email: [email protected]
name: alice
email: [email protected]

YAML is a good alternative for JSON. It is also used in symfony, a popular PHP framework.


Trackback URL


RSS Feed for This PostPost a Comment

CAPTCHA Image
Refresh Image
*