Skip to content

Commit

Permalink
#231 Added + tested Yaml.fromJsonObject()
Browse files Browse the repository at this point in the history
  • Loading branch information
criske committed Oct 31, 2020
1 parent 8ee6f1f commit e8da878
Show file tree
Hide file tree
Showing 7 changed files with 365 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@
<developerConnection>scm:git:git@github.com:decorators-squad/eo-yaml.git</developerConnection>
</scm>
<dependencies>
<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
<version>1.1.4</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
77 changes: 77 additions & 0 deletions src/main/java/com/amihaiemil/eoyaml/JsonYamlDump.java
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;
}
}
57 changes: 57 additions & 0 deletions src/main/java/com/amihaiemil/eoyaml/JsonYamlMapping.java
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 "";
}
};
}
}
49 changes: 49 additions & 0 deletions src/main/java/com/amihaiemil/eoyaml/JsonYamlSequence.java
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 "";
}
};
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/amihaiemil/eoyaml/Yaml.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*/
package com.amihaiemil.eoyaml;

import javax.json.JsonObject;
import java.io.*;

/**
Expand Down Expand Up @@ -181,4 +182,13 @@ public static YamlPrinter createYamlPrinter(final Writer destination) {
public static YamlDump createYamlDump(final Object object) {
return new ReflectedYamlDump(object);
}

/**
* Create a YAML mapping from a {@link JsonObject}.
* @param object JsonObject in question.
* @return YamlMapping.
*/
public static YamlMapping fromJsonObject(final JsonObject object) {
return new JsonYamlMapping(object);
}
}
148 changes: 148 additions & 0 deletions src/test/java/com/amihaiemil/eoyaml/JsonYamlMappingTest.java
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)
);
}
}

1 comment on commit e8da878

@charlesmike
Copy link
Member

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.

Please sign in to comment.