-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#231 Added + tested Yaml.fromJsonObject()
- Loading branch information
Showing
7 changed files
with
365 additions
and
0 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
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,77 @@ | ||
/** | ||
* Copyright (c) 2016-2020, Mihai Emil Andronache | ||
* All rights reserved. | ||
* <p> | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* Redistributions of source code must retain the above copyright notice, this | ||
* list of conditions and the following disclaimer. | ||
* Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* Neither the name of the copyright holder nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
* SUCH DAMAGE. | ||
*/ | ||
package com.amihaiemil.eoyaml; | ||
|
||
import javax.json.JsonArray; | ||
import javax.json.JsonObject; | ||
import javax.json.JsonValue; | ||
|
||
/** | ||
* A YamlDump that works with javax-json-api. | ||
* @author criske | ||
* @version $Id$ | ||
* @since 5.1.7 | ||
*/ | ||
final class JsonYamlDump implements YamlDump { | ||
|
||
/** | ||
* JsonValue to dump. | ||
*/ | ||
private final JsonValue value; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param value JsonValue to dump. | ||
*/ | ||
JsonYamlDump(final JsonValue value) { | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public YamlNode dump() { | ||
final YamlNode node; | ||
final JsonValue safeValue; | ||
if (this.value == null) { | ||
safeValue = JsonValue.NULL; | ||
} else { | ||
safeValue = this.value; | ||
} | ||
if (safeValue instanceof JsonObject) { | ||
node = new JsonYamlMapping((JsonObject) safeValue); | ||
} else if (safeValue instanceof JsonArray) { | ||
node = new JsonYamlSequence((JsonArray) safeValue); | ||
} else { | ||
node = new PlainStringScalar(safeValue | ||
.toString() | ||
.replace("\"", "") | ||
); | ||
} | ||
return node; | ||
} | ||
} |
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,57 @@ | ||
package com.amihaiemil.eoyaml; | ||
|
||
import javax.json.JsonObject; | ||
import javax.json.JsonValue; | ||
import java.util.LinkedHashSet; | ||
import java.util.Set; | ||
|
||
/** | ||
* Representation of a {@link javax.json.JsonObject} as YAML Mapping. | ||
* @author criske | ||
* @version $Id$ | ||
* @since 5.1.7 | ||
*/ | ||
final class JsonYamlMapping extends BaseYamlMapping { | ||
|
||
/** | ||
* Json object being mapped. | ||
*/ | ||
private final JsonObject object; | ||
|
||
/** | ||
* Ctor. | ||
* @param object Json object being mapped. | ||
*/ | ||
JsonYamlMapping(final JsonObject object) { | ||
this.object = object; | ||
} | ||
|
||
@Override | ||
public Set<YamlNode> keys() { | ||
final Set<YamlNode> keys = new LinkedHashSet<>(); | ||
this.object.keySet().forEach(key -> keys | ||
.add(new PlainStringScalar(key))); | ||
return keys; | ||
} | ||
|
||
@Override | ||
public YamlNode value(final YamlNode key) { | ||
final JsonValue jsonValue = this.object.get(key.asScalar().value()); | ||
return new JsonYamlDump(jsonValue).dump(); | ||
} | ||
|
||
@Override | ||
public Comment comment() { | ||
return new Comment() { | ||
@Override | ||
public YamlNode yamlNode() { | ||
return JsonYamlMapping.this; | ||
} | ||
|
||
@Override | ||
public String value() { | ||
return ""; | ||
} | ||
}; | ||
} | ||
} |
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,49 @@ | ||
package com.amihaiemil.eoyaml; | ||
|
||
import javax.json.JsonArray; | ||
import java.util.Collection; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Representation of a {@link javax.json.JsonArray} as YAML Sequence. | ||
* @author criske | ||
* @version $Id$ | ||
* @since 5.1.7 | ||
*/ | ||
final class JsonYamlSequence extends BaseYamlSequence { | ||
|
||
/** | ||
* Json array being mapped. | ||
*/ | ||
private final JsonArray array; | ||
|
||
/** | ||
* Ctor. | ||
* @param array Json array being mapped. | ||
*/ | ||
JsonYamlSequence(final JsonArray array) { | ||
this.array = array; | ||
} | ||
|
||
@Override | ||
public Collection<YamlNode> values() { | ||
return this.array.stream() | ||
.map(value -> new JsonYamlDump(value).dump()) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@Override | ||
public Comment comment() { | ||
return new Comment() { | ||
@Override | ||
public YamlNode yamlNode() { | ||
return JsonYamlSequence.this; | ||
} | ||
|
||
@Override | ||
public String value() { | ||
return ""; | ||
} | ||
}; | ||
} | ||
} |
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
148 changes: 148 additions & 0 deletions
148
src/test/java/com/amihaiemil/eoyaml/JsonYamlMappingTest.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,148 @@ | ||
/** | ||
* Copyright (c) 2016-2020, Mihai Emil Andronache | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* Redistributions of source code must retain the above copyright notice, this | ||
* list of conditions and the following disclaimer. | ||
* Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* Neither the name of the copyright holder nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
* SUCH DAMAGE. | ||
*/ | ||
package com.amihaiemil.eoyaml; | ||
|
||
import org.hamcrest.MatcherAssert; | ||
import org.hamcrest.Matchers; | ||
import org.junit.Test; | ||
|
||
import javax.json.Json; | ||
import javax.json.JsonObject; | ||
|
||
/** | ||
* Unit tests for {@link JsonYamlMapping}. | ||
* @author criske | ||
* @version $Id$ | ||
* @since 5.1.7 | ||
*/ | ||
public final class JsonYamlMappingTest { | ||
|
||
/** | ||
* YamlMapping can map a json object to yaml object. | ||
*/ | ||
@Test | ||
public void canMapJsonObject(){ | ||
final JsonObject json = Json.createObjectBuilder() | ||
.add("architect", "mihai") | ||
.add("developers", | ||
Json.createArrayBuilder() | ||
.add("rultor") | ||
.add("salikjan") | ||
.add("sherif") | ||
.build() | ||
) | ||
.add("lib", | ||
Json.createObjectBuilder() | ||
.add("name", "eo-yaml") | ||
.add("full-name", "") | ||
.add("version", 5) | ||
.build() | ||
) | ||
.add("notes", Json.createArrayBuilder().build()) | ||
.add("latest", false) | ||
.build(); | ||
final YamlMapping expected = Yaml.createYamlMappingBuilder() | ||
.add("architect", "mihai") | ||
.add("developers", | ||
Yaml.createYamlSequenceBuilder() | ||
.add("rultor") | ||
.add("salikjan") | ||
.add("sherif") | ||
.build() | ||
) | ||
.add("lib", | ||
Yaml.createYamlMappingBuilder() | ||
.add("name", "eo-yaml") | ||
.add("full-name", "") | ||
.add("version", "5") | ||
.build() | ||
) | ||
.add("notes", Yaml.createYamlSequenceBuilder().build()) | ||
.add("latest", "false") | ||
.build(); | ||
final YamlMapping jsonMapping = new JsonYamlMapping(json); | ||
MatcherAssert.assertThat( | ||
jsonMapping.string("architect"), | ||
Matchers.equalTo(expected.string("architect")) | ||
); | ||
MatcherAssert.assertThat( | ||
jsonMapping.value("lib").asMapping().string("name"), | ||
Matchers.equalTo(expected.value("lib").asMapping() | ||
.string("name")) | ||
); | ||
MatcherAssert.assertThat( | ||
jsonMapping.value("lib").asMapping().string("full-name"), | ||
Matchers.equalTo(expected.value("lib").asMapping() | ||
.string("full-name")) | ||
); | ||
MatcherAssert.assertThat( | ||
jsonMapping.value("lib").asMapping().integer("version"), | ||
Matchers.equalTo(expected.value("lib").asMapping() | ||
.integer("version")) | ||
); | ||
MatcherAssert.assertThat( | ||
jsonMapping.yamlSequence("notes").size(), | ||
Matchers.equalTo(expected.yamlSequence("notes").size()) | ||
); | ||
MatcherAssert.assertThat( | ||
jsonMapping.yamlSequence("developers").size(), | ||
Matchers.equalTo(expected.yamlSequence("developers").size()) | ||
); | ||
MatcherAssert.assertThat( | ||
jsonMapping.yamlSequence("developers").size(), | ||
Matchers.equalTo(expected.yamlSequence("developers").size()) | ||
); | ||
MatcherAssert.assertThat( | ||
jsonMapping.yamlSequence("developers").string(0), | ||
Matchers.equalTo(expected.yamlSequence("developers").string(0)) | ||
); | ||
MatcherAssert.assertThat( | ||
jsonMapping.yamlSequence("developers").string(1), | ||
Matchers.equalTo(expected.yamlSequence("developers").string(1)) | ||
); | ||
MatcherAssert.assertThat( | ||
jsonMapping.yamlSequence("developers").string(2), | ||
Matchers.equalTo(expected.yamlSequence("developers").string(2)) | ||
); | ||
MatcherAssert.assertThat( | ||
jsonMapping, Matchers.equalTo(expected) | ||
); | ||
} | ||
|
||
/** | ||
* YamlMapping can map an empty json object to yaml object. | ||
*/ | ||
@Test | ||
public void canMapEmptyJsonObject(){ | ||
final JsonObject json = Json.createObjectBuilder().build(); | ||
final YamlMapping expected = Yaml.createYamlMappingBuilder().build(); | ||
MatcherAssert.assertThat( | ||
new JsonYamlMapping(json), Matchers.equalTo(expected) | ||
); | ||
} | ||
} |
Oops, something went wrong.
e8da878
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@criske I've opened the Issues [#433, #434, #435] for the newly added to-dos.
The to-dos may have been added in an earlier commit, but I've found them just now.