diff --git a/src/main/java/org/cip4/lib/jdf/jsonutil/rtf/JSONIndentWalker.java b/src/main/java/org/cip4/lib/jdf/jsonutil/rtf/JSONIndentWalker.java index 38e6474..13d90b4 100644 --- a/src/main/java/org/cip4/lib/jdf/jsonutil/rtf/JSONIndentWalker.java +++ b/src/main/java/org/cip4/lib/jdf/jsonutil/rtf/JSONIndentWalker.java @@ -72,6 +72,7 @@ import java.io.OutputStream; import java.io.PrintStream; +import org.apache.commons.io.output.NullPrintStream; import org.cip4.jdflib.ifaces.IStreamWriter; import org.cip4.jdflib.util.StringUtil; import org.cip4.lib.jdf.jsonutil.JSONObjHelper; @@ -217,6 +218,11 @@ protected String getBeginObj() @Override public void writeStream(final OutputStream os) throws IOException { + if (!isRetainNull()) + { + ps = NullPrintStream.INSTANCE; + walk(); + } ps = new PrintStream(os); writeHeader(); walk(); @@ -308,7 +314,7 @@ protected String getArraySep() @Override public String toString() { - return "JSONIndentWalker [singleIndent=" + singleIndent + ", indent=" + indent + "]"; + return super.toString() + " [singleIndent=" + singleIndent + ", indent=" + indent + "]"; } } diff --git a/src/test/java/org/cip4/lib/jdf/jsonutil/rtf/JSONIndentWalkerTest.java b/src/test/java/org/cip4/lib/jdf/jsonutil/rtf/JSONIndentWalkerTest.java index 05770ee..580c9c6 100644 --- a/src/test/java/org/cip4/lib/jdf/jsonutil/rtf/JSONIndentWalkerTest.java +++ b/src/test/java/org/cip4/lib/jdf/jsonutil/rtf/JSONIndentWalkerTest.java @@ -151,4 +151,35 @@ void testCondensedEmptyArray() throws IOException assertEquals(root, roundTrip); } + @Test + void testNoRetainNullEmpty() throws IOException + { + final JSONObjHelper root = new JSONObjHelper("{\"a\":{\"b\":[],\"c\":{}}}"); + final JSONIndentWalker w = new JSONIndentWalker(root); + w.setSingleIndent(0); + w.setCondensed(true); + w.setRetainNull(false); + final File file = new File(sm_dirTestDataTemp + "testretnull.json"); + FileUtil.writeFile(w, file); + assertTrue(w.isCondensed()); + final JSONObjHelper roundTrip = new JSONObjHelper(file); + assertEquals(new JSONObjHelper("{}"), roundTrip); + } + + @Test + void testNoRetainNull() throws IOException + { + final JSONObjHelper root = new JSONObjHelper("{\"a\":{\"b\":[{\"d\":1},{},{\"d\":1}],\"c\":{\"c1\":\"a\",\"c2\":{},\"c3\":\"a\"}}}"); + final JSONObjHelper root2 = new JSONObjHelper("{\"a\":{\"b\":[{\"d\":1},{\"d\":1}],\"c\":{\"c1\":\"a\",\"c3\":\"a\"}}}"); + final JSONIndentWalker w = new JSONIndentWalker(root); + w.setSingleIndent(0); + w.setCondensed(true); + w.setRetainNull(false); + final File file = new File(sm_dirTestDataTemp + "testret.json"); + FileUtil.writeFile(w, file); + assertTrue(w.isCondensed()); + final JSONObjHelper roundTrip = new JSONObjHelper(file); + assertEquals(root2, roundTrip); + } + }