title: How to only store non-default values with Gson
body: <p>I made a litte serialization system, that uses <code>Gson</code> and only affects <code>Fields</code> with a specific <code>Annotation</code>.</p>
  
  <pre><code>@Retention(RetentionPolicy.RUNTIME)
  @Target(ElementType.FIELD)
  public @interface Store  {
      String valueName() default "";
      boolean throwIfDefault() default false;
  }
  </code></pre>
  
  <p><code>throwIfDefault()</code> determines whether or not the <code>Field</code> should be saved to the file if it's value equals to the default value (I check that by comparing the field's value to the same field but in a static instance of the class). </p>
  
  <p>It works perfectly, but what I'm trying to achive, is that the same works for the <code>Map</code>, <code>Array</code> and <code>Set</code> objects: </p>
  
  <p>The entries of these <code>objects</code> should only be saved, if they are not contained in the default instatiation of that particular <code>Field</code>.</p>
  
  <p>It also has to work for deserialisation:</p>
  
  <p>The default values that are not yet in the loaded <code>object</code>, should be added during deserialisation or the default <code>object</code> is loaded first and then modified with the entries of the loaded <code>object</code>.</p>
  
  <p>Is this possible by creating a custom <code>Json(De)Serializer</code> for these <code>obejcts</code> or how would you do it?</p>
  
  <p>Here's the de-serialization part:</p>
  
  <pre><code>public void Load() throws FileNotFoundException {
      Type typeOfHashMap = new TypeToken&lt;LinkedHashMap&lt;String, Object&gt;&gt;(){}.getType();
      FileReader reader = new FileReader(file);
      HashMap&lt;String, Object&gt; loadedMap = mainGson.fromJson(reader,typeOfHashMap);
      for(Field f: this.getClass().getDeclaredFields()) {
          if (!f.isAnnotationPresent(Store.class)) {
              continue;
          }
          try {
              f.setAccessible(true);
              Store annotation = f.getAnnotation(Store.class);
              Object defaultValue = DefaultRegistrator.getDefault(this.getClass(),f);
              if (!loadedMap.containsKey(annotation.valueName())) {
                  f.set(this, defaultValue);
                  continue;
              }
              Object loadedValue = mainGson.fromJson(
                  loadedMap.get(annotation.valueName()).toString(), f.getType()
              );
              f.set(this, loadedValue);
          } catch(IllegalAccessException e) {
  
          } 
      }
  }
  </code></pre>