Java Gson Exclude fields during serialization : <p>I have a <code>ConfigInstance</code> class which contains a <code>password</code> and a <code>password_hash</code>.
Now I want to serialize the object using gson but exclude the <code>password</code> field.</p>

<pre><code>public class ConfigInstance {
    public String database_address;
    public int database_port;
    public String database_user;

    @Expose(serialize = false)
    private String database_pass;
    public String database_pass_hash;

    public String GetPass() { return database_pass; }

    public void Encrypt() { /* Creates the hash before serializing*/ }

    public void Decrypt() { /* Creates the password after deserializing */}
}
</code></pre>

<p>As you can see, I have tried using <code>@Expose(serialize = false)</code> but it doesn't seem to do anything. Also I already set the field to private since I figured that this would "override" the <code>@Expose</code></p>

<p>but running following code:</p>

<pre><code>private void toFile(File file, ConfigInstance map) {
    map.Encrypt();
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    String jsonConfig = gson.toJson(map);
    FileWriter writer;
    try {
        writer = new FileWriter(file);
        writer.write(jsonConfig);
        writer.flush();
        writer.close();
    } catch (IOException e) {
        System.out.println("Error exporting config: " + e.toString());
    }
}
</code></pre>

<p>still results in the following file content without Errors:</p>

<pre><code>{
  "database_address": "127.0.0.1",
  "database_port": 1521,
  "database_user": "test",
  "database_pass": "test1234",
  "database_pass_hash": "B9FE2C011B59F0D0D383D70073E48A19"
}
</code></pre>

<p>So what am I doing wrong? I am pretty clueless right now and would appreciate any help since <a href="http://www.javacreed.com/gson-annotations-example/" rel="noreferrer">THIS</a> doesn't seem to work.</p>

<p>Thanks in advance.</p>
