Last active
September 7, 2015 11:09
-
-
Save kuFEAR/8986a0cf61d6c4346c6d to your computer and use it in GitHub Desktop.
Jackson Deserializer for a primitive JSON array like this {"photos": ["http://www.url.com/img.jpg", "http://www.url.com/img.jpg", "http://www.url.com/img.jpg"] } to using it with Realm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Created by Nikita Karnaukh on 07/09/15. | |
*/ | |
public class CustomArrayDeserializer extends JsonDeserializer<RealmList<UserPhoto>> { | |
@Override | |
public RealmList<UserPhoto> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { | |
RealmList<UserPhoto> photos = new RealmList<>(); | |
while (true) { | |
String value = jp.nextTextValue(); | |
if (value == null) { | |
JsonToken t = jp.getCurrentToken(); | |
if (t == JsonToken.END_ARRAY) { | |
break; | |
} | |
if (t != JsonToken.VALUE_NULL) { | |
value = null; | |
} | |
} | |
if (value != null) { | |
UserPhoto photo = new UserPhoto(); | |
photo.setPhoto(value); | |
photos.add(photo); | |
} | |
} | |
return photos; | |
} | |
} | |
public class UserPhoto extends RealmObject { | |
private String photo; | |
public String getPhoto() { | |
return photo; | |
} | |
public void setPhoto(String photo) { | |
this.photo = photo; | |
} | |
} | |
public class Friend extends RealmObject { | |
@JsonProperty @JsonDeserialize(using = CustomArrayDeserializer.class) | |
private RealmList<UserPhoto> photos; | |
public RealmList<UserPhoto> getPhotos() { | |
return photos; | |
} | |
public void setPhotos(RealmList<UserPhoto> photos) { | |
this.photos = photos; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment