Retrofit: Posting JSON object body with a null property value

Created on 26 Dec 2014  路  10Comments  路  Source: square/retrofit

{"foo":{"bar":null}}

I'm trying to post such json with "bar": being null;
But retrofit ignores null properties in java object passed to @Body

gson .serializeNulls() couldn't save me because I have 2 other properties on the object I pass to @Body shouldn't be serialized as json null's.
https://sites.google.com/site/gson/gson-user-guide#TOC-Null-Object-Support

What's the most practical and "clean / less-hacky" way to serialize single property value as json null when it's java null using Retrofit? Is it providing a custom gson serializer, would it be overkill?

Thanks.

Most helpful comment

or change
new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create())

to
new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create(new GsonBuilder().serializeNulls().create()))

All 10 comments

I ended up posting as raw JSON string by this method if anyone's interested;
http://stackoverflow.com/a/21423093/2172057

Retrofit has no interaction here. This is entirely Gson. If you want to conditionally serialize nulls you can register a TypeAdapter globally on the Gson instance or use @JsonAdapter. More information on doing that is available on Gson's documentation website.

eralpkaraduman Could you resolve your problem with null property value ?. I am dealing with the same issue without success so far.

public class RetrofitAdapterBuilder extends RestAdapter.Builder {
public RetrofitAdapterBuilder(RetrofitInterceptor retrofitInterceptor) {
this.setConverter(new GsonConverter(new GsonBuilder().serializeNulls().create()))
...

or change
new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create())

to
new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create(new GsonBuilder().serializeNulls().create()))

@mplzgta ,
Thats what I was looking for .
Thanks

mplzgta you are the man, you save my ass... sorry for vocabulary but, that is happen to me, only this handle work for me tks.

@mplzgta I am using Retrofit. Tested Gson converter outside Retrift context and conversion was as intended (fields with null values were serialized to JSON), but when using it in Retrofit's GET request they don't get serialized (fields with null value get omitted in conversion). What could be a possible problem?

GsonConverterFactory.create(new GsonBuilder().serializeNulls().create()))

finally fouhnd solution

can i use both factory object :

addConverterFactory(new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create())
addConverterFactory(new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create(new GsonBuilder().serializeNulls().create())))

according to my need i use both pattern but issue is that only one working which is written on top, how could i resolved this

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chriskessel picture chriskessel  路  3Comments

Ne1c picture Ne1c  路  3Comments

zheangrybear picture zheangrybear  路  3Comments

MetaiR picture MetaiR  路  3Comments

jpshelley picture jpshelley  路  4Comments