Java to yaml schema maven example

Java to yaml schema maven example

Java to yaml schema maven example:
YAML and JSON are simple and nice format for structured data and easier for human to read and write than XML. But there have been no schema for YAML such as RelaxNG or DTD. Below is an example that convert java object to yaml.

  • Create java maven project; maven
  • Add below dependencies to your project:
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
    <dependencies>
    <dependency>
    			<groupId>org.yaml</groupId>
    			<artifactId>snakeyaml</artifactId>
    			<version>1.16</version>
    		</dependency>
    		<dependency>
    			<groupId>com.esotericsoftware.yamlbeans</groupId>
    			<artifactId>yamlbeans</artifactId>
    			<version>1.09</version>
    		</dependency>
    </dependencies>
  • Create java Object that you want to convert to yaml. In example I have used Object class, replace it with your own class object
  • 1
    2
    3
    4
    5
    6
    7
    8
    
     public static void yamlGenerator(  ) throws IOException
        {
          Object obj=new Object();
          YamlWriter writer = new YamlWriter(new FileWriter("Object.yaml"));
          writer.getConfig().setClassTag("Object", Object.class);
          writer.write(obj);
          writer.close();
        }

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.