package com.newsblur.serialization;

import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.newsblur.domain.Feed;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Special handler for the "feeds" field that appears in API responses and is sometimes
 * a list and sometimes a set and sometimes null and sometimes an empty object.
 */
public class FeedListTypeAdapter implements JsonDeserializer<List<Feed>> {

    @Override
    public List<Feed> deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {

        List<Feed> result = new ArrayList<Feed>();

        if (jsonElement.isJsonObject()) {
            Feed feed = (Feed) jsonDeserializationContext.deserialize(jsonElement, Feed.class);
            result.add(feed);
        } else if (jsonElement.isJsonArray()) {
            for (JsonElement arrayMember : jsonElement.getAsJsonArray()) {
                Feed feed = (Feed) jsonDeserializationContext.deserialize(arrayMember, Feed.class);
                result.add(feed);
            } 
        }

        return result;
    }
}

@@ -194,16 +194,51 @@
 lement;%0A
+import com.google.gson.JsonObject;%0A
 import c
@@ -272,16 +272,16 @@
 eption;%0A
-
 import c
@@ -387,16 +387,38 @@
 il.List;
+%0Aimport java.util.Map;
 %0A%0A/**%0A *
@@ -921,24 +921,246 @@
 Object()) %7B%0A
+            // the feeds member is a map of feed IDs to feed objects.  just grab the objects%0A            JsonObject o = jsonElement.getAsJsonObject();%0A            for (Map.Entry%3CString,JsonElement%3E e : o.entrySet()) %7B%0A    
             
@@ -1217,27 +1217,28 @@
 rialize(
-jsonElement
+e.getValue()
 , Feed.c
@@ -1236,32 +1236,36 @@
 ), Feed.class);%0A
+    
             resu
@@ -1270,32 +1270,46 @@
 sult.add(feed);%0A
+            %7D%0A
         %7D else i
@@ -1332,32 +1332,106 @@
 sJsonArray()) %7B%0A
+            // the feeds member is a list of objects. parse them as usual%0A
             for 

Make StoriesReponse parser accept both list-type and dict-type supplemental feed blocks to fix loading of starred stories. (#570)