Skip to content

Commit

Permalink
#49 mapper with declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
h1alexbel committed Jul 6, 2023
1 parent ed3baaa commit a743c58
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/main/java/io/github/eocqrs/eokson/JsonXML.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
import org.cactoos.Text;

/**
Expand Down Expand Up @@ -58,15 +56,15 @@ public JsonXML(final Json jsn, final String rt) {

@Override
public String asString() throws Exception {
final XmlMapper xml = new XmlMapper();
final ObjectMapper mapper = new ObjectMapper();
final JsonNode node = mapper.readTree(
new Jocument(
this.json
).pretty()
);
xml.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
return xml.writer()
return new WithDeclaration()
.value()
.writer()
.withRootName(this.root)
.writeValueAsString(node);
}
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/io/github/eocqrs/eokson/WithDeclaration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2023 Aliaksei Bialiauski, EO-CQRS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package io.github.eocqrs.eokson;

import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;

/**
* XML Mapper With Declaration.
*
* @author Aliaksei Bialiauski ([email protected])
* @since 0.3.2
*/
final class WithDeclaration implements Scalar<XmlMapper> {

@Override
public XmlMapper value() {
final XmlMapper xml = new XmlMapper();
xml.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
return xml;
}
}
47 changes: 47 additions & 0 deletions src/test/java/io/github/eocqrs/eokson/WithDeclarationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2023 Aliaksei Bialiauski, EO-CQRS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package io.github.eocqrs.eokson;

import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;

/**
* Test case for {@link WithDeclaration}.
*
* @author Aliaksei Bialiauski ([email protected])
* @since 0.3.2
*/
final class WithDeclarationTest {

@Test
void readsMapperInRightFormat() {
final XmlMapper mapper = new WithDeclaration().value();
MatcherAssert.assertThat(
"Mapper is present",
mapper,
Matchers.notNullValue()
);
}
}

0 comments on commit a743c58

Please sign in to comment.