diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..81a66a0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +/target/ +/.settings/ +/.classpath +/.project +/build/ +/.idea/ +*.iml + diff --git a/README.md b/README.md new file mode 100644 index 0000000..a9638fa --- /dev/null +++ b/README.md @@ -0,0 +1,28 @@ +## YAML Utils + +A collection of useful YAML utilities. + +Object to YAML + +```java +String yaml = YamlUtils.objectToYaml(myObject); +``` + +YAML to Object + +```java +MyClass myObject = YamlUtils.yamlToObject(yaml, MyClass.class); +``` + +Check if String is a valid YAML + +```java +YamlUtils.isValid(yaml) +``` + +Determine if two YAML Strings are equal + +```java +YamlUtils.areEqual(yaml1, yaml2) +``` + diff --git a/lombok.config b/lombok.config new file mode 100644 index 0000000..a23edb4 --- /dev/null +++ b/lombok.config @@ -0,0 +1,2 @@ +config.stopBubbling = true +lombok.addLombokGeneratedAnnotation = true \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..69c0a87 --- /dev/null +++ b/pom.xml @@ -0,0 +1,39 @@ + + 4.0.0 + + com.ljunggren + yaml-utils + 0.0.1-SNAPSHOT + jar + + yaml-utils + http://maven.apache.org + + + UTF-8 + 1.8 + 1.8 + + + + + com.fasterxml.jackson.dataformat + jackson-dataformat-yaml + 2.13.0 + + + org.projectlombok + lombok + 1.18.12 + provided + + + junit + junit + 4.13 + test + + + diff --git a/src/main/java/com/ljunggren/yamlUtils/YamlUtils.java b/src/main/java/com/ljunggren/yamlUtils/YamlUtils.java new file mode 100644 index 0000000..e45386f --- /dev/null +++ b/src/main/java/com/ljunggren/yamlUtils/YamlUtils.java @@ -0,0 +1,34 @@ +package com.ljunggren.yamlUtils; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature; + +public class YamlUtils { + + public static String objectToYaml(Object object) throws JsonProcessingException { + ObjectMapper mapper = new ObjectMapper(new YAMLFactory().disable(Feature.WRITE_DOC_START_MARKER)); + return mapper.writeValueAsString(object); + } + + public static T yamlToObject(String yaml, Class clazz) throws JsonMappingException, JsonProcessingException { + return new ObjectMapper(new YAMLFactory()).readValue(yaml, clazz); + } + + public static boolean isValid(String yaml) { + try { + new ObjectMapper(new YAMLFactory()).readTree(yaml); + return true; + } catch (Exception e) { + return false; + } + } + + public static boolean areEqual(String yaml1, String yaml2) throws JsonMappingException, JsonProcessingException { + ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); + return mapper.readTree(yaml1).equals(mapper.readTree(yaml2)); + } + +} diff --git a/src/test/java/com/ljunggren/yamlUtils/YamlUtilsTest.java b/src/test/java/com/ljunggren/yamlUtils/YamlUtilsTest.java new file mode 100644 index 0000000..0a31927 --- /dev/null +++ b/src/test/java/com/ljunggren/yamlUtils/YamlUtilsTest.java @@ -0,0 +1,80 @@ +package com.ljunggren.yamlUtils; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonMappingException; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +public class YamlUtilsTest { + + @Data + @NoArgsConstructor + @AllArgsConstructor + private static class User { + private String name; + private int age; + private boolean active; + } + + @Test + public void objectToYamlTest() throws JsonProcessingException { + User user = new User("Alex", 40, true); + String expected = "name: \"Alex\"\n" + + "age: 40\n" + + "active: true\n"; + String actual = YamlUtils.objectToYaml(user); + assertEquals(expected, actual); + } + + @Test + public void yamlToObjectTest() throws JsonMappingException, JsonProcessingException { + User user = new User("Alex", 40, true); + String yaml = YamlUtils.objectToYaml(user); + User generatedUser = YamlUtils.yamlToObject(yaml, User.class); + assertEquals(user, generatedUser); + } + + @Test + public void isValidTest() throws JsonProcessingException { + User user = new User("Alex", 40, true); + String yaml = YamlUtils.objectToYaml(user); + assertTrue(YamlUtils.isValid(yaml)); + } + + @Test + public void isValidFalseTest() { + String yaml = "`name"; + assertFalse(YamlUtils.isValid(yaml)); + } + + @Test + public void areEqualTest() throws JsonProcessingException { + User user = new User("Alex", 40, true); + String expected = "# comment\n" + + "name: \"Alex\"\n" + + "age: 40\n" + + "active: true\n"; + String actual = YamlUtils.objectToYaml(user); + assertTrue(YamlUtils.areEqual(expected, actual)); + } + + @Test + public void areEqualFalseTest() throws JsonProcessingException { + User user = new User("Alex", 40, true); + String expected = "# comment\n" + + "name: \"Alexander\"\n" + + "age: 40\n" + + "active: true\n"; + String actual = YamlUtils.objectToYaml(user); + assertFalse(YamlUtils.areEqual(expected, actual)); + } + +}