Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I rely on <a href="http://code.google.com/p/google-gson/" rel="noreferrer">Google JSON</a> To handle this kind of issue by using The feature</p> <blockquote> <p><a href="http://sites.google.com/site/gson/gson-user-guide#TOC-User-Defined-Exclusion-Strategies" rel="noreferrer">Excluding Fields From Serialization and Deserialization</a></p> </blockquote> <p>Suppose a bi-directional relationship between A and B class as follows</p> <pre><code>public class A implements Serializable { private B b; } </code></pre> <p>And B</p> <pre><code>public class B implements Serializable { private A a; } </code></pre> <p>Now use GsonBuilder To get a custom Gson object as follows (Notice <strong>setExclusionStrategies</strong> method)</p> <pre><code>Gson gson = new GsonBuilder() .setExclusionStrategies(new ExclusionStrategy() { public boolean shouldSkipClass(Class&lt;?&gt; clazz) { return (clazz == B.class); } /** * Custom field exclusion goes here */ public boolean shouldSkipField(FieldAttributes f) { return false; } }) /** * Use serializeNulls method if you want To serialize null values * By default, Gson does not serialize null values */ .serializeNulls() .create(); </code></pre> <p>Now our circular reference</p> <pre><code>A a = new A(); B b = new B(); a.setB(b); b.setA(a); String json = gson.toJson(a); System.out.println(json); </code></pre> <p>Take a look at <a href="http://google-gson.googlecode.com/svn/tags/1.3/docs/javadocs/com/google/gson/GsonBuilder.html" rel="noreferrer">GsonBuilder</a> class </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload