24

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?

Wandering Fool
  • 2,170
  • 3
  • 18
  • 48
Alberto Crespo
  • 2,429
  • 5
  • 28
  • 51
  • 1
    I have worked recently on Retrofit library. And as far as my knowledge is concerned if you remove 'text2' from your POJO class, the request json will not map that string value onto your model. In order words it will be ignored. – Faizan Tariq Aug 10 '15 at 14:34
  • Yes, of course, but I need this 'text2' variable in the POJO class to use it in the android app, I just need to ignore this 'text2' in the JSON. – Alberto Crespo Aug 10 '15 at 14:38

4 Answers4

28

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:

public class sendingPojo {
    long id;
    String text1;
    transient 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;
    }
}

I hope it helps

Alberto Crespo
  • 2,429
  • 5
  • 28
  • 51
  • 1
    It's "transient String text2" instead of "String transient text2". Thank you. – Alex Jul 05 '16 at 08:50
  • 2
    I'm not sure, that this anwser better than @nikhil.thakkar wrote. For example, current class can implement Serializable too and I want, for example, ignore field text2 in JSON and do not ignore on serialization. – Yura Shinkarev Sep 26 '17 at 08:48
17

Mark the desired fields with the @Expose annotation, such as:

@Expose private String id;

Leave out any fields that you do not want to serialize. Then just create your Gson object this way:

Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
nikhil.thakkar
  • 1,093
  • 7
  • 12
  • It could be a good alternative that maybe I will take to solve my problem, but what I would like to do is maintaining the Retrofit interface which builds the JSONs automatically. – Alberto Crespo Aug 11 '15 at 16:10
  • 1
    You can set a custom converter in retrofit. It's supported by default. Refer here http://square.github.io/retrofit/ – nikhil.thakkar Aug 11 '15 at 16:19
  • 4
    By using @Expose annotations on your fields and configure retrofit by using the GSON builder like this you can ignore the elements: .addConverterFactory(GsonConverterFactory.create(GsonBuilder().excludeFieldsWithoutExposeAnnotation().create())) – Bernard Arjan Draaisma Dec 26 '16 at 21:52
13

You can configure Retrofit by adding the GSON object from the GSONBuilder into your ConverterFactory, see my example below:

private static UsuarioService getUsuarioService(String url) {
    return new Retrofit.Builder().client(getClient()).baseUrl(url)
            .addConverterFactory(GsonConverterFactory.create(getGson())).build()
            .create(UsuarioService.class);
}

private static OkHttpClient getClient() {
    return new OkHttpClient.Builder().connectTimeout(5, MINUTES).readTimeout(5, MINUTES)
            .build();
}

private static Gson getGson() {
    return new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
}

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.

@Entity(indexes = {
        @Index(value = "id DESC", unique = true)
})
public class Usuario {

    @Id(autoincrement = true)
    @Expose(deserialize = false, serialize = false) 
    private Long pkey; // <- Ignored in JSON
    private Long id; // <- Ignored in JSON, no @Expose annotation
    @Index(unique = true)
    @Expose
    private String guid; // <- Only this field will be shown in JSON.
1

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.

data class  User(var id: String,  var name: string?)

val user = User()
user.id = "some id"
user.name = null

The Json generated would be

user{
"id": "some id"
}
MetaPlanet
  • 129
  • 4
  • Not the best approach if you want to maintain them for further use but simply don't send them. You'll need to then copy all body before sending, – htafoya Jan 17 '23 at 05:12