Skip to content

Commit

Permalink
Improve JSONifying behavior of cyclic structures.
Browse files Browse the repository at this point in the history
One of the things svenson does not do for you is to take care of cyclic
structure. Instead it expects you to make sure that you
@JSONPropery(ignore=true) the right back references to solve the issue.

It would just fail with a StackoverflowError when you fail to do so or
when you use types that are cyclic by nature.

See also #1 (not actually the
first issue, just the first one since we moved to github)

One reason why the StackOverflowError is so annoying lies in the its
very nature of being a stack overflow and its stack trace being a
loooong chain of repetitive method invocations.

Thankfully there is one easy fix we can do right now without deciding on
the more complicated issues outline in the bug.

We already do the JSONifying in a private recursive method which is
called from public non-recursive methods. Which means that if we catch
the stack overflow error there, we can wrap it in another exception
whose stack trace then shows us the entry point into the JSONification
process, greatly aiding debugging.
  • Loading branch information
fforw committed Nov 7, 2015
1 parent 7613207 commit 5209c98
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 2 deletions.
25 changes: 25 additions & 0 deletions src/main/java/org/svenson/CyclicStructureException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.svenson;

public class CyclicStructureException
extends SvensonRuntimeException
{
private static final long serialVersionUID = -4937201415177884428L;


public CyclicStructureException(String message, Throwable cause)
{
super(message, cause);
}


public CyclicStructureException(String message)
{
super(message);
}


public CyclicStructureException(Throwable cause)
{
super(cause);
}
}
18 changes: 16 additions & 2 deletions src/main/java/org/svenson/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,14 @@ private static void newLine(StringBuilder sb, int cnt)
*/
public void dumpObject(JSONCharacterSink out, Object o)
{
dumpObject(out, o, '\0', ignoredProperties);
try
{
dumpObject(out, o, '\0', ignoredProperties);
}
catch(StackOverflowError e)
{
throw new CyclicStructureException("Cyclic JSON structure", e);
}
}

/**
Expand All @@ -263,7 +270,14 @@ public void dumpObject(JSONCharacterSink out, Object o)
public void dumpObject(JSONCharacterSink out, Object o,
Collection<String> ignoredProps)
{
dumpObject(out, o, '\0', ignoredProps);
try
{
dumpObject(out, o, '\0', ignoredProps);
}
catch(StackOverflowError e)
{
throw new CyclicStructureException("Cyclic JSON structure", e);
}
}

/**
Expand Down
39 changes: 39 additions & 0 deletions src/test/java/org/svenson/CyclicStructureTestCase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.svenson;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.notification.Failure;
import org.svenson.test.CyclicBean;

import java.io.PrintWriter;
import java.io.StringWriter;

import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.*;

public class CyclicStructureTestCase
{
@Test
public void testCycle() throws Exception
{

CyclicBean bean = new CyclicBean();
bean.setInner(new CyclicBean.Inner(bean));

try
{
JSON.defaultJSON().forValue(bean);

}
catch(CyclicStructureException e)
{
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));

// make sure the stacktrace shows this method
assertThat(sw.getBuffer().toString(),containsString("org.svenson.CyclicStructureTestCase.testCycle"));
return;
}
Assert.fail("Should have thrown a CyclicStructureException");
}
}
36 changes: 36 additions & 0 deletions src/test/java/org/svenson/test/CyclicBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.svenson.test;

public class CyclicBean
{
private Inner inner;


public void setInner(Inner inner)
{
this.inner = inner;
}


public Inner getInner()
{
return inner;
}


public static class Inner
{
private final CyclicBean cyclicBean;


public Inner(CyclicBean cyclicBean)
{
this.cyclicBean = cyclicBean;
}


public CyclicBean getCyclicBean()
{
return cyclicBean;
}
}
}

0 comments on commit 5209c98

Please sign in to comment.