-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored toYaml / fromYaml into separate classes to better separate…
… concerns and improve testability for a future custom implementation
- Loading branch information
Showing
5 changed files
with
100 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/java/org/javawebstack/abstractdata/yaml/LegacyYamlDumper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/org/javawebstack/abstractdata/yaml/LegacyYamlParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
22
src/main/java/org/javawebstack/abstractdata/yaml/YamlDumper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
22
src/main/java/org/javawebstack/abstractdata/yaml/YamlParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |