Skip to content

Commit

Permalink
improved json walker for no retain null
Browse files Browse the repository at this point in the history
  • Loading branch information
rainer-prosi committed Oct 11, 2024
1 parent 377b581 commit 2cfafa8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -308,7 +314,7 @@ protected String getArraySep()
@Override
public String toString()
{
return "JSONIndentWalker [singleIndent=" + singleIndent + ", indent=" + indent + "]";
return super.toString() + " [singleIndent=" + singleIndent + ", indent=" + indent + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}

0 comments on commit 2cfafa8

Please sign in to comment.