Map JsonArray to Java Object

657 views
Skip to first unread message

johnfilo

unread,
Nov 17, 2011, 8:46:16 PM11/17/11
to RestFB
OK, I'm very new to JSON and RestFB, and I'm struggling to understand
how best to map a JsonArray to a Java Object.

It's all to do with getting permissions from the user to check that a
user has "Allowed" all my extended permissions.

So I am doing this to obtain the permissions with the Graph API:

FacebookClient fbClient = new DefaultFacebookClient(accessToken);
JsonObject results = fbClient.fetchObject("me/permissions",
JsonObject.class);

Which returns the following JSON:

{"data":[
{"installed":1,
"status_update":1,
"photo_upload":1,
"video_upload":1,
"offline_access":1,
"email":1,
"create_event":1,
"create_note":1,
"share_item":1,
"bookmarked":1,
"rsvp_event":1,
"read_stream":1,
"publish_stream":1,
"user_birthday":1,
"user_online_presence":1,
"user_events":1
}]
}

What I would like to do is create a Java Object that I can map to, say
called Permissions, so that I could do this:

FacebookClient fbClient = new DefaultFacebookClient(accessToken);
Permissions permissions = fbClient.fetchObject("me/permissions",
Permissions.class);

But I have no idea what the Permissions class is supposed to look
like? I have tried a number of things but I am getting no where.

Can someone please shed some light on what Permissions should look
like to map the "data" array correctly?

johnfilo

unread,
Nov 17, 2011, 10:43:03 PM11/17/11
to RestFB
OK, I managed to get something working as I think I got my head around
it after trial and error. Here is what I have now and it works. Anyone
care to comment to confirm if what I am doing it correct?

FacebookClient fbClient = new DefaultFacebookClient(accessToken);

Permissions permissions = fbClient.fetchObject("me/permissions",
Permissions.class);

** Now I can do the following:

permissions.getData().get(0).getOfflineAccess(); //returns 1 if
permission was allowed

*******************************************
Permissions.java
*******************************************

package models;

import java.util.List;

import com.restfb.Facebook;
import com.restfb.json.JsonObject;

public class Permissions {

public Permissions() {}

@Facebook
public List<Permission> data;

public List<Permission> getData() {
return data;
}
public void setData(List<Permission> data) {
this.data = data;
}

public class Permission {
@Facebook("email")
public String email;
@Facebook("user_birthday")
public String userBirthday;
@Facebook("read_stream")
public String readStream;
@Facebook("publish_stream")
public String publishStream;
@Facebook("user_online_presence")
public String userOnlinePresence;
@Facebook("user_events")
public String userEvents;
@Facebook("create_event")
public String createEvent;
@Facebook("offline_access")
public String offlineAccess;
@Facebook("rsvpEvent")
public String rsvp_event;
@Facebook("publish_actions")
public String publishActions;

public Permission() {}

public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUserBirthday() {
return userBirthday;
}
public void setUserBirthday(String userBirthday) {
this.userBirthday = userBirthday;
}
public String getReadStream() {
return readStream;
}
public void setReadStream(String readStream) {
this.readStream = readStream;
}
public String getPublishStream() {
return publishStream;
}
public void setPublishStream(String publishStream) {
this.publishStream = publishStream;
}
public String getUserOnlinePresence() {
return userOnlinePresence;
}
public void setUserOnlinePresence(String userOnlinePresence) {
this.userOnlinePresence = userOnlinePresence;
}
public String getUserEvents() {
return userEvents;
}
public void setUserEvents(String userEvents) {
this.userEvents = userEvents;
}
public String getCreateEvent() {
return createEvent;
}
public void setCreateEvent(String createEvent) {
this.createEvent = createEvent;
}
public String getOfflineAccess() {
return offlineAccess;
}
public void setOfflineAccess(String offlineAccess) {
this.offlineAccess = offlineAccess;
}
public String getRsvp_event() {
return rsvp_event;
}
public void setRsvp_event(String rsvp_event) {
this.rsvp_event = rsvp_event;
}
public String getPublishActions() {
return publishActions;
}
public void setPublishActions(String publishActions) {
this.publishActions = publishActions;

Mark Allen

unread,
Nov 18, 2011, 10:28:44 AM11/18/11
to RestFB
John, this is the correct approach. Two tweaks:

1. I think this is transposed:

@Facebook("rsvpEvent")
public String rsvp_event;

2. You can type your field values as Integer instead of String if
you'd like (reflects the original JSON better)

Thanks
Mark

johnfilo

unread,
Nov 21, 2011, 5:29:37 AM11/21/11
to RestFB
Hi Mark,

Thanks for letting me know I'm on the right track.

Yeah I had noticed I got rsvp event wrong. It should have been:

@Facebook("rsvp_event")
public String rsvpEvent;
...
public String getRsvpEvent() {
return rsvpEvent;
}
public void setRsvpEvent(String rsvpEvent) {
this.rsvpEvent = rsvpEvent;
}

Nice tip on typing the field to Integer, I have updated my code
already.

Cheers
John

Zhizhi Deng

unread,
Jun 28, 2013, 3:21:23 AM6/28/13
to res...@googlegroups.com, john...@layerm.com.au
Just drop in this thread because I run into the same issue. I think it is not very practical to hardcode the permission names in annotaitons since Facebook has like 50+ permissions on the list now and it keeps changing. Here's the Permission class I have eventually to map the permissions into a string set.

public class Permission
{
@SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
@Facebook
private List<String> data;

private Set<String> granted;

public Set<String> getGranted()
{
return granted;
}

@SuppressWarnings({"UnusedDeclaration"})
@JsonMappingCompleted
private void parseRawData(JsonMapper jsonMapper)
{
granted = new HashSet<String>();
if (data != null)
{
for (String item : data)
{
final JSONObject jsonObject = new JSONObject(item);
for (Object key : jsonObject.keySet())
{
final int flag = jsonObject.getInt((String) key);
if (flag > 0) granted.add((String) key);
Reply all
Reply to author
Forward
0 new messages