forked from blad/solid-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added New Search Endpoint and Test. Adjust deserializer to handle new…
… endpoint result.
- Loading branch information
Showing
5 changed files
with
73 additions
and
6 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
33 changes: 33 additions & 0 deletions
33
app/src/androidTest/java/com/btellez/solidandroid/test/model/DeserializeTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.btellez.solidandroid.test.model; | ||
|
||
import android.test.InstrumentationTestCase; | ||
|
||
import com.btellez.solidandroid.model.Icon; | ||
import com.btellez.solidandroid.model.IconParser; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.List; | ||
|
||
public class DeserializeTest extends InstrumentationTestCase { | ||
|
||
public void testSearchResultDeserialization() throws Exception { | ||
String jsonString = readFileFromAssets("search_results.json"); | ||
IconParser.GsonIconParser parser = new IconParser.GsonIconParser(); | ||
List<Icon> icons = parser.fromJson(jsonString, null); | ||
assertEquals(72, icons.size()); | ||
} | ||
|
||
private String readFileFromAssets(String fileName) { | ||
try { | ||
InputStream is = getInstrumentation().getContext().getAssets().open(fileName); | ||
int size = is.available(); | ||
byte[] buffer = new byte[size]; | ||
is.read(buffer); | ||
is.close(); | ||
return new String(buffer); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
34 changes: 31 additions & 3 deletions
34
app/src/main/java/com/btellez/solidandroid/model/IconParser.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,60 @@ | ||
package com.btellez.solidandroid.model; | ||
|
||
import com.btellez.solidandroid.utility.Strings; | ||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonDeserializationContext; | ||
import com.google.gson.JsonDeserializer; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParseException; | ||
import com.google.gson.JsonParser; | ||
|
||
import java.lang.reflect.Type; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import hugo.weaving.DebugLog; | ||
|
||
public interface IconParser { | ||
public List<Icon> fromJson(String jsonString, String dataKey); | ||
|
||
/** | ||
* Google Gson Implementation of our Icon Parser | ||
*/ | ||
public static class GsonIconParser implements IconParser { | ||
Gson gson = new Gson(); | ||
Gson gson = new GsonBuilder().registerTypeAdapter(Icon.class, new IconDeserializer()).create(); | ||
JsonParser parser = new JsonParser(); | ||
|
||
@Override | ||
@Override @DebugLog | ||
public List<Icon> fromJson(String jsonString, String dataKey) { | ||
JsonElement json = parser.parse(jsonString); | ||
JsonArray iconList = json.getAsJsonObject().getAsJsonArray(dataKey); | ||
JsonArray iconList; | ||
if (Strings.isEmpty(dataKey)) { | ||
iconList = json.getAsJsonArray(); | ||
} else { | ||
iconList = json.getAsJsonObject().getAsJsonArray(dataKey); | ||
} | ||
|
||
List<Icon> icons = new ArrayList<Icon>(iconList.size()); | ||
for (JsonElement item : iconList) { | ||
icons.add(gson.fromJson(item, Icon.class)); | ||
} | ||
return icons; | ||
} | ||
|
||
private class IconDeserializer implements JsonDeserializer<Icon> { | ||
Gson defaultGson = new Gson(); | ||
@Override | ||
public Icon deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { | ||
JsonElement uploader = json.getAsJsonObject().get("uploader"); | ||
if (!uploader.isJsonObject()) { | ||
json.getAsJsonObject().remove("uploader"); | ||
json.getAsJsonObject().add("uploader", new JsonObject()); | ||
} | ||
return defaultGson.fromJson(json, Icon.class); | ||
} | ||
} | ||
} | ||
} |
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
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