Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed container deserialization error

  • Loading branch information...
commit 571e73e8551b82b046dc56fcb018220e338e5799 1 parent 7aecb66
Matt Wright authored June 24, 2011
22  spring-social-instagram/src/main/java/org/springframework/social/instagram/api/InstagramProfile.java
@@ -5,10 +5,14 @@
5 5
 public class InstagramProfile {
6 6
 	
7 7
 	private final long id;
8  
-	private final String fullName;
9 8
 	private final String profilePictureUrl;
10 9
 	private final String username;
11 10
 	private final Map<String,Integer> counts;
  11
+	private final String fullName;
  12
+	private String bio;
  13
+	private String website;
  14
+	private String firstName;
  15
+	private String lastName;
12 16
 	
13 17
 	public InstagramProfile(long id, String username, String fullName, String profilePictureUrl, Map<String,Integer> counts) {
14 18
 		this.id = id;
@@ -49,4 +53,20 @@ public int getFollowsCount() {
49 53
     public int getFollowedBy() {
50 54
         return counts.get("followed_by");
51 55
     }
  56
+
  57
+	public String getBio() {
  58
+		return bio;
  59
+	}
  60
+
  61
+	public String getWebsite() {
  62
+		return website;
  63
+	}
  64
+
  65
+	public String getFirstName() {
  66
+		return firstName;
  67
+	}
  68
+
  69
+	public String getLastName() {
  70
+		return lastName;
  71
+	}
52 72
 }
33  ...instagram/src/main/java/org/springframework/social/instagram/api/impl/AbstractInstagramDeserializer.java
... ...
@@ -0,0 +1,33 @@
  1
+package org.springframework.social.instagram.api.impl;
  2
+
  3
+import java.io.IOException;
  4
+
  5
+import org.codehaus.jackson.JsonParser;
  6
+import org.codehaus.jackson.JsonProcessingException;
  7
+import org.codehaus.jackson.JsonToken;
  8
+import org.codehaus.jackson.map.JsonDeserializer;
  9
+
  10
+abstract class AbstractInstagramDeserializer<T> extends JsonDeserializer<T> {
  11
+    
  12
+	public <C> C deserializeResponseObject(JsonParser jp, Class<C> container, Class<?> containee)
  13
+            throws IOException, JsonProcessingException {
  14
+	    
  15
+	    while (jp.nextToken() != JsonToken.END_OBJECT) {
  16
+            String fieldname = jp.getCurrentName();
  17
+            jp.nextToken();
  18
+            if("meta".equals(fieldname)) {
  19
+                jp.clearCurrentToken(); // we can ignore this for now
  20
+                jp.nextToken();
  21
+            } else if("data".equals(fieldname)) {
  22
+                try {
  23
+                    return container.getConstructor(containee).newInstance(jp.readValueAs(containee));
  24
+                } catch (Exception e) {
  25
+                    
  26
+                    return null;
  27
+                }
  28
+            }
  29
+        }
  30
+        return null;
  31
+	}
  32
+	
  33
+}
18  ...ial-instagram/src/main/java/org/springframework/social/instagram/api/impl/InstagramProfileContainer.java
... ...
@@ -0,0 +1,18 @@
  1
+package org.springframework.social.instagram.api.impl;
  2
+
  3
+import org.codehaus.jackson.map.annotate.JsonDeserialize;
  4
+import org.springframework.social.instagram.api.InstagramProfile;
  5
+
  6
+@JsonDeserialize(using=InstagramProfileContainerDeserializer.class)
  7
+public class InstagramProfileContainer {
  8
+	
  9
+	private InstagramProfile profile;
  10
+	
  11
+	public InstagramProfileContainer(InstagramProfile profile) {
  12
+		this.profile = profile;
  13
+	}
  14
+	
  15
+	public InstagramProfile getProfile() {
  16
+		return profile;
  17
+	}
  18
+}
18  ...m/src/main/java/org/springframework/social/instagram/api/impl/InstagramProfileContainerDeserializer.java
... ...
@@ -0,0 +1,18 @@
  1
+package org.springframework.social.instagram.api.impl;
  2
+
  3
+import java.io.IOException;
  4
+
  5
+import org.codehaus.jackson.JsonParser;
  6
+import org.codehaus.jackson.JsonProcessingException;
  7
+import org.codehaus.jackson.map.DeserializationContext;
  8
+import org.springframework.social.instagram.api.InstagramProfile;
  9
+
  10
+public class InstagramProfileContainerDeserializer extends AbstractInstagramDeserializer<InstagramProfileContainer> {
  11
+
  12
+	@Override
  13
+	public InstagramProfileContainer deserialize(JsonParser jp, DeserializationContext ctxt) 
  14
+			throws IOException, JsonProcessingException {
  15
+		return deserializeResponseObject(jp, InstagramProfileContainer.class, InstagramProfile.class);
  16
+	}
  17
+
  18
+}
12  ...-social-instagram/src/main/java/org/springframework/social/instagram/api/impl/InstagramProfileMixin.java
@@ -18,4 +18,16 @@
18 18
             @JsonProperty("full_name") String fullName, 
19 19
             @JsonProperty("profile_picture") String profilePictureUrl, 
20 20
             @JsonProperty("counts") Map<String,Integer> counts) {}
  21
+    
  22
+    @JsonProperty("bio")
  23
+    String bio;
  24
+    
  25
+    @JsonProperty("website")
  26
+    String website;
  27
+    
  28
+    @JsonProperty("first_name")
  29
+    String firstName;
  30
+    
  31
+    @JsonProperty("last_name")
  32
+    String lastName;
21 33
 }
4  spring-social-instagram/src/main/java/org/springframework/social/instagram/api/impl/UserTemplate.java
@@ -23,11 +23,11 @@ public UserTemplate(InstagramTemplate instagram, boolean isAuthorizedForUser) {
23 23
 
24 24
 	public InstagramProfile getUser() {
25 25
 		requireUserAuthorization();
26  
-		return get(buildUri(USERS_ENDPOINT + "self/"), InstagramProfile.class);
  26
+		return get(buildUri(USERS_ENDPOINT + "self/"), InstagramProfileContainer.class).getProfile();
27 27
 	}
28 28
 
29 29
 	public InstagramProfile getUser(long userId) {
30  
-		return get(buildUri(USERS_ENDPOINT + Long.toString(userId) + "/"), InstagramProfile.class);
  30
+		return get(buildUri(USERS_ENDPOINT + Long.toString(userId) + "/"), InstagramProfileContainer.class).getProfile();
31 31
 	}
32 32
 
33 33
 	public PagedMediaList getFeed() {
2  spring-social-instagram/src/test/java/org/springframework/social/instagram/api/impl/MediaTemplateTest.java
... ...
@@ -1,6 +1,7 @@
1 1
 package org.springframework.social.instagram.api.impl;
2 2
 
3 3
 import static org.junit.Assert.assertTrue;
  4
+import static org.junit.Assert.assertEquals;
4 5
 import static org.springframework.http.HttpMethod.GET;
5 6
 import static org.springframework.http.HttpMethod.POST;
6 7
 import static org.springframework.http.HttpMethod.DELETE;
@@ -27,6 +28,7 @@ public void getMedia() {
27 28
 			.andRespond(withResponse(new ClassPathResource("testdata/media.json", getClass()), responseHeaders));
28 29
 		
29 30
 		Media media = instagram.mediaOperations().getMedia(48904105);
  31
+		assertEquals(48904105, media.getId());
30 32
 		mockServer.verify();
31 33
 	}
32 34
 	
3  spring-social-instagram/src/test/java/org/springframework/social/instagram/api/impl/UserTemplateTest.java
... ...
@@ -1,5 +1,6 @@
1 1
 package org.springframework.social.instagram.api.impl;
2 2
 
  3
+import static org.junit.Assert.assertEquals;
3 4
 import static org.springframework.http.HttpMethod.GET;
4 5
 import static org.springframework.http.HttpMethod.POST;
5 6
 import static org.springframework.social.test.client.RequestMatchers.body;
@@ -25,6 +26,7 @@ public void getUser() {
25 26
 			.andRespond(withResponse(new ClassPathResource("testdata/user-profile.json", getClass()), responseHeaders));
26 27
 		
27 28
 		InstagramProfile user = instagram.userOperations().getUser();
  29
+		assertEquals("tomharman", user.getUsername());
28 30
 		mockServer.verify();
29 31
 	}
30 32
 	
@@ -35,6 +37,7 @@ public void getSpecificUser() {
35 37
 			.andRespond(withResponse(new ClassPathResource("testdata/user-profile.json", getClass()), responseHeaders));
36 38
 		
37 39
 		InstagramProfile user = instagram.userOperations().getUser(12345);
  40
+		assertEquals("tomharman", user.getUsername());
38 41
 		mockServer.verify();
39 42
 	}
40 43
 	
14  ...-social-instagram/src/test/java/org/springframework/social/instagram/api/impl/testdata/user-profile.json
@@ -4,13 +4,15 @@
4 4
    },
5 5
    "data":{
6 6
       "username":"tomharman",
  7
+      "bio":"",
  8
+      "website":"http://harmantom.com",
  9
+      "profile_picture":"http://images.instagram.com/profiles/profile_6948_75sq_1295839075.jpg",
  10
+      "full_name":"Tom Harman",
7 11
       "counts":{
8  
-         "media":69,
9  
-         "followed_by":68,
10  
-         "follows":58
  12
+         "media":108,
  13
+         "followed_by":93,
  14
+         "follows":70
11 15
       },
12  
-      "profile_picture":"http://images.instagram.com/profiles/profile_6948_75sq_1295839075.jpg",
13  
-      "id":"6948",
14  
-      "full_name":"Tom Harman"
  16
+      "id":"6948"
15 17
    }
16 18
 }

0 notes on commit 571e73e

Please sign in to comment.
Something went wrong with that request. Please try again.