From 753384eefa43e93664e2eb6476a0ff981ce35c47 Mon Sep 17 00:00:00 2001 From: JanHolger Date: Sun, 10 Mar 2024 16:21:38 +0100 Subject: [PATCH] Refactored toYaml / fromYaml into separate classes to better separate concerns and improve testability for a future custom implementation --- .../abstractdata/AbstractElement.java | 30 ++++++------------- .../abstractdata/yaml/LegacyYamlDumper.java | 22 ++++++++++++++ .../abstractdata/yaml/LegacyYamlParser.java | 25 ++++++++++++++++ .../abstractdata/yaml/YamlDumper.java | 22 ++++++++++++++ .../abstractdata/yaml/YamlParser.java | 22 ++++++++++++++ 5 files changed, 100 insertions(+), 21 deletions(-) create mode 100644 src/main/java/org/javawebstack/abstractdata/yaml/LegacyYamlDumper.java create mode 100644 src/main/java/org/javawebstack/abstractdata/yaml/LegacyYamlParser.java create mode 100644 src/main/java/org/javawebstack/abstractdata/yaml/YamlDumper.java create mode 100644 src/main/java/org/javawebstack/abstractdata/yaml/YamlParser.java diff --git a/src/main/java/org/javawebstack/abstractdata/AbstractElement.java b/src/main/java/org/javawebstack/abstractdata/AbstractElement.java index 852a783..a2a441d 100644 --- a/src/main/java/org/javawebstack/abstractdata/AbstractElement.java +++ b/src/main/java/org/javawebstack/abstractdata/AbstractElement.java @@ -7,6 +7,8 @@ import org.javawebstack.abstractdata.json.JsonDumper; import org.javawebstack.abstractdata.json.JsonParser; import org.javawebstack.abstractdata.util.QueryString; +import org.javawebstack.abstractdata.yaml.YamlDumper; +import org.javawebstack.abstractdata.yaml.YamlParser; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; @@ -110,16 +112,7 @@ default String toJsonString() { Object toObject(); default String toYaml(boolean pretty) { - Yaml yaml; - if (pretty) { - DumperOptions options = new DumperOptions(); - options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); - options.setPrettyFlow(true); - yaml = new Yaml(options); - } else { - yaml = new Yaml(); - } - return yaml.dump(toObject()); + return new YamlDumper().setPretty(pretty).dump(this); } default String toYaml() { @@ -127,24 +120,19 @@ default String toYaml() { } static AbstractElement fromYaml(String source, boolean singleRoot) { - Yaml yaml = new Yaml(); - Object object = yaml.load(source); - if (singleRoot && object instanceof List) { - List list = (List) object; - if (list.size() == 0) { - object = new HashMap<>(); - } else { - object = list.get(0); - } - } - return fromAbstractObject(object); + return new YamlParser().setSingleRoot(singleRoot).parse(source); } static AbstractElement fromYaml(String source) { return fromYaml(source, false); } + @Deprecated static AbstractElement fromAbstractObject(Object object) { + return fromObject(object); + } + + static AbstractElement fromObject(Object object) { if (object == null) return AbstractNull.VALUE; if (object instanceof List) { diff --git a/src/main/java/org/javawebstack/abstractdata/yaml/LegacyYamlDumper.java b/src/main/java/org/javawebstack/abstractdata/yaml/LegacyYamlDumper.java new file mode 100644 index 0000000..4dd7f37 --- /dev/null +++ b/src/main/java/org/javawebstack/abstractdata/yaml/LegacyYamlDumper.java @@ -0,0 +1,22 @@ +package org.javawebstack.abstractdata.yaml; + +import org.javawebstack.abstractdata.AbstractElement; +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.Yaml; + +public class LegacyYamlDumper { + + protected static String dump(AbstractElement e, boolean pretty) { + Yaml yaml; + if (pretty) { + DumperOptions options = new DumperOptions(); + options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); + options.setPrettyFlow(true); + yaml = new Yaml(options); + } else { + yaml = new Yaml(); + } + return yaml.dump(e.toObject()); + } + +} diff --git a/src/main/java/org/javawebstack/abstractdata/yaml/LegacyYamlParser.java b/src/main/java/org/javawebstack/abstractdata/yaml/LegacyYamlParser.java new file mode 100644 index 0000000..24399ee --- /dev/null +++ b/src/main/java/org/javawebstack/abstractdata/yaml/LegacyYamlParser.java @@ -0,0 +1,25 @@ +package org.javawebstack.abstractdata.yaml; + +import org.javawebstack.abstractdata.AbstractElement; +import org.yaml.snakeyaml.Yaml; + +import java.util.HashMap; +import java.util.List; + +public class LegacyYamlParser { + + protected static AbstractElement parse(String source, boolean singleRoot) { + Yaml yaml = new Yaml(); + Object object = yaml.load(source); + if (singleRoot && object instanceof List) { + List list = (List) object; + if (list.size() == 0) { + object = new HashMap<>(); + } else { + object = list.get(0); + } + } + return AbstractElement.fromObject(object); + } + +} diff --git a/src/main/java/org/javawebstack/abstractdata/yaml/YamlDumper.java b/src/main/java/org/javawebstack/abstractdata/yaml/YamlDumper.java new file mode 100644 index 0000000..f92734b --- /dev/null +++ b/src/main/java/org/javawebstack/abstractdata/yaml/YamlDumper.java @@ -0,0 +1,22 @@ +package org.javawebstack.abstractdata.yaml; + +import org.javawebstack.abstractdata.AbstractElement; + +public class YamlDumper { + + private boolean pretty = false; + + public YamlDumper setPretty(boolean pretty) { + this.pretty = pretty; + return this; + } + + public boolean isPretty() { + return pretty; + } + + public String dump(AbstractElement e) { + return LegacyYamlDumper.dump(e, pretty); + } + +} diff --git a/src/main/java/org/javawebstack/abstractdata/yaml/YamlParser.java b/src/main/java/org/javawebstack/abstractdata/yaml/YamlParser.java new file mode 100644 index 0000000..7eacc5a --- /dev/null +++ b/src/main/java/org/javawebstack/abstractdata/yaml/YamlParser.java @@ -0,0 +1,22 @@ +package org.javawebstack.abstractdata.yaml; + +import org.javawebstack.abstractdata.AbstractElement; + +public class YamlParser { + + boolean singleRoot = false; + + public YamlParser setSingleRoot(boolean singleRoot) { + this.singleRoot = singleRoot; + return this; + } + + public boolean isSingleRoot() { + return singleRoot; + } + + public AbstractElement parse(String source) { + return LegacyYamlParser.parse(source, singleRoot); + } + +}