Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In addressing this problem, I took the following approach (standardizing the process across my application, making the code clear and reusable):</p> <ol> <li>Create an annotation class to be used on fields you'd like excluded</li> <li>Define a class which implements Google's ExclusionStrategy interface</li> <li>Create a simple method to generate the GSON object using the GsonBuilder (similar to Arthur's explanation)</li> <li>Annotate the fields to be excluded as needed</li> <li>Apply the serialization rules to your com.google.gson.Gson object</li> <li>Serialize your object</li> </ol> <p>Here's the code:</p> <p>1)</p> <pre><code>import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD, ElementType.METHOD}) public @interface GsonExclude { } </code></pre> <p>2)</p> <pre><code>import com.google.gson.ExclusionStrategy; import com.google.gson.FieldAttributes; public class GsonExclusionStrategy implements ExclusionStrategy{ private final Class&lt;?&gt; typeToExclude; public GsonExclusionStrategy(Class&lt;?&gt; clazz){ this.typeToExclude = clazz; } @Override public boolean shouldSkipClass(Class&lt;?&gt; clazz) { return ( this.typeToExclude != null &amp;&amp; this.typeToExclude == clazz ) || clazz.getAnnotation(GsonExclude.class) != null; } @Override public boolean shouldSkipField(FieldAttributes f) { return f.getAnnotation(GsonExclude.class) != null; } } </code></pre> <p>3)</p> <pre><code>static Gson createGsonFromBuilder( ExclusionStrategy exs ){ GsonBuilder gsonbuilder = new GsonBuilder(); gsonbuilder.setExclusionStrategies(exs); return gsonbuilder.serializeNulls().create(); } </code></pre> <p>4)</p> <pre><code>public class MyObjectToBeSerialized implements Serializable{ private static final long serialVersionID = 123L; Integer serializeThis; String serializeThisToo; Date optionalSerialize; @GsonExclude @ManyToOne(fetch=FetchType.LAZY, optional=false) @JoinColumn(name="refobj_id", insertable=false, updatable=false, nullable=false) private MyObjectThatGetsCircular dontSerializeMe; ...GETTERS AND SETTERS... } </code></pre> <p>5) </p> <p>In the first case, null is supplied to the constructor, you can specify another class to be excluded - both options are added below</p> <pre><code>Gson gsonObj = createGsonFromBuilder( new GsonExclusionStrategy(null) ); Gson _gsonObj = createGsonFromBuilder( new GsonExclusionStrategy(Date.class) ); </code></pre> <p>6)</p> <pre><code>MyObjectToBeSerialized _myobject = someMethodThatGetsMyObject(); String jsonRepresentation = gsonObj.toJson(_myobject); </code></pre> <p>or, to exclude the Date object</p> <pre><code>String jsonRepresentation = _gsonObj.toJson(_myobject); </code></pre>
    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.
    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