android


retrofit : deal with property that can be either empty string or an array


I'm using Retrofit2 and I have been asked to consume the following json:
{ "status":"success",
payload{
"prop1": 1234,
"prop2": "" } }
The problem is that prop2 can be either an empty string or an array of objects. ( Empty array is not an option for them)
I don't remember Retrofit having a mechanism to deal with with this sort of type inconsistency. I'm looking for a recipe to maybe receive this property as some kind of generic Object any maybe use GSon to parse it later or some other alternative way to make it work.
You can try this to check if prop2 is either an array or an empty string
JsonObject jsonObject = new Gson().fromJson("{ \"status\":\"success\", \"payload\": { \"prop1\": 1234, \"prop2\": \"\" } }", JsonObject.class);
JsonObject payload = jsonObject.getAsJsonObject("payload");
JsonElement jsonElement = payload.get("prop2");
if (jsonElement.isJsonArray()) {
// value of prop2 is an array
} else if (jsonElement.isJsonPrimitive()) {
JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive();
if (jsonPrimitive.isString() && "".equals(jsonPrimitive.getAsString())) {
// value of prop2 is an empty String
}
}

Related Links

StateListDrawable doesn't work in a custom view
No such file or directory : app.ap_
How to share internal storage file with Gmail Client
Why is BitmapRegionDecoder allocating so much memory?
facebook FQL get friend attending to event
How to show the alrert dialog after onDestory()?
Android Bluetooth one scan after another
There wan an error attempting to initialize the monoAndroidPackage
Where do i use the TextWatcher implementation?
How to put non Duplicate values in SQLite & Access?
Android Tablet, menu overflow position
How to check internet access (INCLUDING tethering!) on Android?
Alert Dialog Builder stop responding
The size of image returned by Aviary is smaller than original
Google map doesn't work on app from google play
Error using Ksoap2 library in Android

Categories

HOME
algorithm
beautifulsoup
time-complexity
codenvy
apache-flink
phpmyadmin
angular-formly
package
seo
database-normalization
permissions
limit
interpolation
google-admin-sdk
vert.x
syntaxnet
mql5
java.util.logging
alamofire
google-form
screen-readers
dreamweaver
bourbon
parcelable
hta
short-url
dynamically-generated
roundup
beautifier
bulletphysics
quantlib
user-defined-functions
mql
quickfixj
gojs
tex
nohup
urlsession
hackintosh
laravel-elixir
show-hide
pom.xml
serde
hammer.js
insight
openblas
trigger.io
vibrate
bootstrap-tags-input
stripe-connect
reporting-services-2012
windows-azure-queues
jodd
drawstring
android-dateutils
piracy-prevention
phpgrid
toran-proxy
r-package
forwarding
vacuum
canvg
code-documentation
openbabel
showdialog
addressing-mode
sonarqube-5.0
device-tree
mdichild
with-statement
renderer
menubar
ora-06550
c++builder-xe5
swfobject
preload
argouml
device-width
joomla3.1
saleslogix
wcf-configuration
dajaxice
listitem
cuda-gdb
jsr286
relative
multiplatform
http-daemon
cloudfiles
nhprof
objective-j
drupal-fivestar
duplex
wcf-instancing
scrubyt

Resources

Encrypt Message