I am developing an Android App which is sending a JSON using Android Retrofit (it converts a POJO class in a JSON). It is working fine, but I need to ignore in the sending of JSON one element from the POJO class.
Does anyone know any Android Retrofit annotation?
Example
POJO Class:
public class sendingPojo
{
long id;
String text1;
String text2;//--> I want to ignore that in the JSON
getId(){return id;}
setId(long id){
this.id = id;
}
getText1(){return text1;}
setText1(String text1){
this.text1 = text1;
}
getText2(){return text2;}
setText2(String text2){
this.text2 = text2;
}
}
Interface Sender ApiClass
public interface SvcApi {
@POST(SENDINGPOJO_SVC_PATH)
public sendingPojo addsendingPojo(@Body sendingPojo sp);
}
Any idea how to ignore text2?
I found an alternative solution if you don't want to use
new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create()
.Just including
transient
in the variable I need to ignore.So, the POJO class finally:
I hope it helps
Mark the desired fields with the @Expose annotation, such as:
Leave out any fields that you do not want to serialize. Then just create your Gson object this way:
You can configure Retrofit by adding the GSON object from the GSONBuilder into your ConverterFactory, see my example below:
To ignore the field elements simply add @Expose(deserialize = false, serialize = false) to your properties or none, and for (de)serialize your fields elements you can add the @Expose() annotations with empty values to your properties.
If you are using Kotlin + Retrofit + Moshi (I tested this) In case where you want to conditionally ignore fields, you can set it to null.
The Json generated would be