Skip to content

Commit 3fe176a

Browse files
committed
alwaysBlock parameter to control printing of original JsonObject nodes
1 parent 2972636 commit 3fe176a

File tree

6 files changed

+87
-22
lines changed

6 files changed

+87
-22
lines changed

Diff for: src/main/java/com/amihaiemil/eoyaml/BaseYamlNode.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public final <T extends YamlNode> T asClass(final Class<T> clazz,
9191
*
9292
*/
9393
@Override
94-
public final String toString() {
94+
public String toString() {
9595
final StringWriter writer = new StringWriter();
9696
final YamlPrinter printer = new RtYamlPrinter(writer);
9797
try {

Diff for: src/main/java/com/amihaiemil/eoyaml/JsonYamlMapping.java

+18
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727
*/
2828
package com.amihaiemil.eoyaml;
2929

30+
import com.amihaiemil.eoyaml.exceptions.YamlPrintException;
31+
3032
import javax.json.JsonObject;
33+
import java.io.IOException;
34+
import java.io.StringWriter;
3135
import java.util.LinkedHashSet;
3236
import java.util.Set;
3337

@@ -86,4 +90,18 @@ public String value() {
8690
}
8791
};
8892
}
93+
94+
@Override
95+
public String toString() {
96+
final StringWriter writer = new StringWriter();
97+
final YamlPrinter printer = new RtYamlPrinter(writer, true);
98+
try {
99+
printer.print(this);
100+
return writer.toString();
101+
} catch (final IOException ex) {
102+
throw new YamlPrintException(
103+
"IOException when printing YAML", ex
104+
);
105+
}
106+
}
89107
}

Diff for: src/main/java/com/amihaiemil/eoyaml/JsonYamlSequence.java

+18
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727
*/
2828
package com.amihaiemil.eoyaml;
2929

30+
import com.amihaiemil.eoyaml.exceptions.YamlPrintException;
31+
3032
import javax.json.JsonArray;
33+
import java.io.IOException;
34+
import java.io.StringWriter;
3135
import java.util.Collection;
3236
import java.util.stream.Collectors;
3337

@@ -73,4 +77,18 @@ public String value() {
7377
}
7478
};
7579
}
80+
81+
@Override
82+
public String toString() {
83+
final StringWriter writer = new StringWriter();
84+
final YamlPrinter printer = new RtYamlPrinter(writer, true);
85+
try {
86+
printer.print(this);
87+
return writer.toString();
88+
} catch (final IOException ex) {
89+
throw new YamlPrintException(
90+
"IOException when printing YAML", ex
91+
);
92+
}
93+
}
7694
}

Diff for: src/main/java/com/amihaiemil/eoyaml/RtYamlPrinter.java

+31-2
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,26 @@ final class RtYamlPrinter implements YamlPrinter {
4949
*/
5050
private final String lineSeparator;
5151

52+
/**
53+
* Print all the nodes in block-style.
54+
*/
55+
private final boolean alwaysBlock;
56+
5257
/**
5358
* Constructor.
5459
* @param writer Destination writer.
5560
*/
5661
RtYamlPrinter(final Writer writer) {
57-
this(writer, System.lineSeparator());
62+
this(writer, false);
63+
}
64+
65+
/**
66+
* Constructor.
67+
* @param writer Destination writer.
68+
* @param alwaysBlock Print all the nodes in block-style.
69+
*/
70+
RtYamlPrinter(final Writer writer, final boolean alwaysBlock) {
71+
this(writer, System.lineSeparator(), alwaysBlock);
5872
}
5973

6074
/**
@@ -63,15 +77,30 @@ final class RtYamlPrinter implements YamlPrinter {
6377
* @param lineSeparator Line separator.
6478
*/
6579
RtYamlPrinter(final Writer writer, final String lineSeparator) {
80+
this(writer, lineSeparator, false);
81+
}
82+
83+
/**
84+
* Constructor.
85+
* @param writer Destination writer.
86+
* @param lineSeparator Line separator.
87+
* @param alwaysBlock Print all the nodes in block-style.
88+
*/
89+
RtYamlPrinter(
90+
final Writer writer,
91+
final String lineSeparator,
92+
final boolean alwaysBlock
93+
) {
6694
this.writer = writer;
6795
this.lineSeparator = lineSeparator;
96+
this.alwaysBlock = alwaysBlock;
6897
}
6998

7099
@Override
71100
public void print(final YamlNode node) throws IOException {
72101
try {
73102
final YamlVisitor<String> visitor = new YamlPrintVisitor(
74-
this.lineSeparator
103+
this.lineSeparator, this.alwaysBlock
75104
);
76105
if (node.type().equals(Node.SCALAR)) {
77106
this.writer.append("---")

Diff for: src/main/java/com/amihaiemil/eoyaml/YamlPrintVisitor.java

+18-19
Original file line numberDiff line numberDiff line change
@@ -53,43 +53,41 @@ final class YamlPrintVisitor implements YamlVisitor<String> {
5353
private final String lineSeparator;
5454

5555
/**
56-
* Ctor. Defaults to indentation 2 and the System line separator.
56+
* Print each node as block-style always.
5757
*/
58-
YamlPrintVisitor() {
59-
this(2, System.lineSeparator());
60-
}
61-
62-
/**
63-
* Ctor.
64-
* @param indentation Number of indentation spaces.
65-
*/
66-
YamlPrintVisitor(final int indentation) {
67-
this(indentation, System.lineSeparator());
68-
}
58+
private final boolean alwaysBlock;
6959

7060
/**
7161
* Ctor.
7262
* @param lineSeparator Line separator.
63+
* @param alwaysBlock Print each node as block-style always.
7364
*/
74-
YamlPrintVisitor(final String lineSeparator) {
75-
this(2, lineSeparator);
65+
YamlPrintVisitor(final String lineSeparator, final boolean alwaysBlock) {
66+
this(2, lineSeparator, alwaysBlock);
7667
}
7768

7869
/**
7970
* Ctor.
8071
* @param indentation Number of spaces to use for indentation.
8172
* @param lineSeparator Line separator.
73+
* @param alwaysBlock Print each node as block-style always.
8274
*/
83-
YamlPrintVisitor(final int indentation, final String lineSeparator) {
75+
YamlPrintVisitor(
76+
final int indentation,
77+
final String lineSeparator,
78+
final boolean alwaysBlock
79+
) {
8480
this.indentation = indentation;
8581
this.lineSeparator = lineSeparator;
82+
this.alwaysBlock = alwaysBlock;
8683
}
8784
@Override
8885
public String visitYamlMapping(final YamlMapping node) {
8986
final StringWriter writer = new StringWriter();
9087
final String printed;
91-
if(node instanceof ReadFlowMapping
92-
|| node instanceof JsonYamlMapping) {
88+
if((node instanceof ReadFlowMapping
89+
|| node instanceof JsonYamlMapping)
90+
&& !this.alwaysBlock) {
9391
this.printFlowMapping(node, writer);
9492
printed = writer.toString();
9593
} else {
@@ -110,8 +108,9 @@ public String visitYamlMapping(final YamlMapping node) {
110108
public String visitYamlSequence(final YamlSequence node) {
111109
final StringWriter writer = new StringWriter();
112110
final String printed;
113-
if(node instanceof ReadFlowSequence
114-
|| node instanceof JsonYamlSequence) {
111+
if((node instanceof ReadFlowSequence
112+
|| node instanceof JsonYamlSequence)
113+
&& !this.alwaysBlock) {
115114
this.printFlowSequence(node, writer);
116115
printed = writer.toString();
117116
} else {

Diff for: src/test/java/com/amihaiemil/eoyaml/JsonYamlMappingTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ public void canMapJsonObject(){
132132
MatcherAssert.assertThat(
133133
jsonMapping, Matchers.equalTo(expected)
134134
);
135+
System.out.println(jsonMapping);
135136
}
136137

137138
/**

0 commit comments

Comments
 (0)