Java – gson: parameter get serialized, even if it has @ expose (serialize = false)

I'm writing SDK for JSON API, and I encountered a seemingly strange problem The API is very strict in post data validation, and some parameters, such as ID, are not allowed when updating resources. For this reason, I added @ expose (serialize = false) the ID field of my resource class However, it seems that it still serializes the field, causing the request to be rejected The resource classes are as follows:

public class Organisation extends BaSEObject
{
    public static final Gson PRETTY_PRINT_JSON = new GsonBuilder()
            .setPrettyPrinting()
            .create();

    @Expose(serialize = false)
    @SerializedName("_id")
    private String id;

    @SerializedName("email")
    private String email;

    @SerializedName("name")
    private String name;

    @SerializedName("parent_id")
    private String parentId;

    public String toJson()
    {
        return PRETTY_PRINT_JSON.toJson(this);
    }
}

My unit test creates an organization instance through the API, saves the newly created instance as a class parameter to the test class, and calls the update method, which will test the update implementation of the SDK by updating new resources This is where it went wrong Even if the tojson () method is called on the new organization to serialize it into JSON to get the update request_ The ID field still exists, causing the API to reject the update The test code is as follows Note the comments in the code

@Test
public void testCreateUpdateAndDeleteOrganisation() throws RequestException
{
    Organisation organisation = new Organisation();
    organisation.setParentId(this.ORGANISATION_ID);
    organisation.setName("Java Test Organisation");

    Organisation newOrganisation = this.MySDK.organisation.create(organisation);
    this.testOrganisation(newOrganisation);
    this.newOrganisation = newOrganisation;

    this.testUpdateOrganisation();
}

public void testUpdateOrganisation() throws RequestException
{
    // I tried setting ID to null,but that doesn't work either
    // even though I've set Gson to not serialise null values
    this.newOrganisation.setId(null);
    this.newOrganisation.setName(this.newName);

    // For debugging
    System.out.println(this.newOrganisation.toJson());

    Organisation updatedOrganisation = this.MySDK.organisation.update(this.newOrganisation.getId(),this.newOrganisation);

    this.testOrganisation(updatedOrganisation);
    assertEquals(newOrganisation.getName(),this.newName);

    this.testDeleteOrganisation();
}

Who can find out what I did wrong? I have a feeling that it is related to the fact that the instance already has / has an ID value, but it shouldn't matter if I explicitly tell it not to serialize it?

Thank you for your help

Edit: in this MySDK. organisation. update(this.newOrganisation.getId(),this. newOrganisation);, It does not edit organization instances The given ID is only added to the URL to which the SDK will post (post / organization / {ID})

Solution

As you mentioned in your comments, @ expose should be a better choice than transient It is important to note that the default gson instance does not consider the @ expose annotation! Whatever option you set, it ignores it

If you want to activate the @ expose option, you need to customize gson According to your above code, change it to:

public static final Gson PRETTY_PRINT_JSON = new GsonBuilder()
        .setPrettyPrinting()
        .excludeFieldsWithoutExposeAnnotation();
        .create();

Your @ expose (serialize = false) should be active and excluded during serialization

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>