Java – Windows Azure SDK for Android – How do I exclude attributes of DTOs from serialization?

Windows Azure SDK for Android – How do I exclude attributes of DTOs from serialization?… here is a solution to the problem.

Windows Azure SDK for Android – How do I exclude attributes of DTOs from serialization?

I’m trying to use Azure Mobile Services to save data for an Android application that comes to self.
The problem I’m having now is that I have a data transfer object with several fields that correspond to columns in an Azure database table. I have a field and I don’t want to keep it. I’m trying to use @Expose annotations, but it doesn’t seem to work, I’m getting an exception from Azure saying that SubCategories has an invalid data type. What am I doing wrong?

package com.mycorp.myapp.model;
import java.util.*;
import com.google.gson.annotations.*;

public class Category {

public Category(){
        SubCategories = new ArrayList<Category>();
    }

public int Id;

public String Name;

public int ParentId;

@Expose(serialize = false, deserialize = false)
    List<Category> SubCategories;
}

The

following code returns a MobileServiceException({“code”:400,”error”: “The value of the Error: property ‘SubCategories’ belongs to type ‘object’, which is not a supported type.” })

Category category = new Category();     
category. Name = "new";
category. ParentId = 1;      
mClient.getTable(Category.class).insert(category, new TableOperationCallback<Category>() {          
        @Override
        public void onCompleted(Category entity, Exception exception, ServiceFilterResponse response) {
            if(exception!=null)
            {
                Log.e("Service error", exception.getMessage());
            }               
        }
    });

Solution

It turns out that if you use the default gson constructor as described, @Expose comments are ignored here .

I was able to solve my problem by removing Expose and making the field transient:

package com.mycorp.myapp.model;
import java.util.*;
import com.google.gson.annotations.*;

public class Category {

public Category(){
        SubCategories = new ArrayList<Category>();
    }

public int Id;

public String Name;

public int ParentId;

@Expose(serialize = false, deserialize = false)
    transient List<Category> SubCategories;
}

Related Problems and Solutions