Skip to content

Commit

Permalink
Refactored toYaml / fromYaml into separate classes to better separate…
Browse files Browse the repository at this point in the history
… concerns and improve testability for a future custom implementation
  • Loading branch information
JanHolger committed Mar 10, 2024
1 parent 64444aa commit 753384e
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 21 deletions.
30 changes: 9 additions & 21 deletions src/main/java/org/javawebstack/abstractdata/AbstractElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -110,41 +112,27 @@ 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() {
return toYaml(true);
}

static AbstractElement fromYaml(String source, boolean singleRoot) {
Yaml yaml = new Yaml();
Object object = yaml.load(source);
if (singleRoot && object instanceof List) {
List<Object> list = (List<Object>) 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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
@@ -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<Object> list = (List<Object>) object;
if (list.size() == 0) {
object = new HashMap<>();
} else {
object = list.get(0);
}
}
return AbstractElement.fromObject(object);
}

}
22 changes: 22 additions & 0 deletions src/main/java/org/javawebstack/abstractdata/yaml/YamlDumper.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
22 changes: 22 additions & 0 deletions src/main/java/org/javawebstack/abstractdata/yaml/YamlParser.java
Original file line number Diff line number Diff line change
@@ -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);
}

}

0 comments on commit 753384e

Please sign in to comment.