Categories

  • java

Tags

  • java

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:
<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
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(); }