-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve JSONifying behavior of cyclic structures.
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
Showing
4 changed files
with
116 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |