1. SnakeYAML:
import org.yaml.snakeyaml.Yaml;
public class SnakeYamlExample {
public static void main(String[] args) {
Yaml yaml = new Yaml();
Object data = yaml.load(new FileInputStream("data.yml"));
System.out.println(data);
Map<String, Object> map = new HashMap<>();
map.put("name", "John Doe");
map.put("age", 30);
map.put("email", "johndoe@example.com");
yaml.dump(map, new FileWriter("output.yml"));
}
}
2. Jackson:
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
public class JacksonYamlExample {
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
Object data = mapper.readValue(new File("data.yml"), Object.class);
System.out.println(data);
Map<String, Object> map = new HashMap<>();
map.put("name", "Jane Smith");
map.put("age", 25);
map.put("email", "janesmith@example.com");
mapper.writeValue(new File("output.yml"), map);
}
}