Last active
April 15, 2016 15:25
-
-
Save Gregadeaux/cbcc3781fda9d7fa6498 to your computer and use it in GitHub Desktop.
Revisions
-
Gregadeaux revised this gist
Jul 13, 2015 . 1 changed file with 5 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,9 +1,8 @@ package ai.cometandroid.network; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.internal.LinkedHashTreeMap; import java.io.BufferedReader; import java.io.IOException; @@ -79,10 +78,9 @@ private static String fromStream(InputStream in) throws IOException { } private static String fromJsonApi(String json) throws Exception { Gson gson = new Gson(); JsonApiResponse response = gson.fromJson(json, JsonApiResponse.class); List<Map<String, Object>> data = new ArrayList<>(); response.data() @@ -112,9 +110,9 @@ private static String fromJsonApi(String json) throws Exception { String formatted; if(data.size() == 1) { formatted = gson.toJson(data.get(0)); }else { formatted = gson.toJson(data); } return formatted; -
Gregadeaux revised this gist
Jul 13, 2015 . 2 changed files with 36 additions and 14 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -35,13 +35,11 @@ public class JsonApiConverter implements Converter { public Object fromBody(TypedInput body, Type type) throws ConversionException { try { InputStream in = body.in(); String json = fromJsonApi(fromStream(in)); if(String.class.equals(type)) return json; else return new Gson().fromJson(json, type); } catch (Exception e) { throw new ConversionException(e); } @@ -99,25 +97,24 @@ private static String fromJsonApi(String json) throws Exception { Observable<Map<String, Object>> inner; Object includedLinks = stringObjectMap.get(key); if (includedLinks instanceof List) inner = Observable.from((List<Map<String, Object>>) includedLinks); else inner = Observable.just((Map<String, Object>) includedLinks); inner.forEach(link -> response.included() .filter(included -> included.get("type").equals(link.get("type")) && included.get("id").equals(link.get("id"))) .first() .subscribe(included -> link.putAll((Map<String, Object>) included.get(ATTRIBUTES_KEY)))); }); stringObjectMap.remove(RELATIONSHIP_KEY); }) .subscribe(datum -> data.add(datum)); String formatted; if(data.size() == 1) { formatted = moshi.adapter(Map.class).toJson(data.get(0)); }else { formatted = moshi.adapter(List.class).toJson(data); } return formatted; This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,25 @@ package ai.cometandroid.models; import java.util.List; import java.util.Map; import rx.Observable; /** * Created by greg on 6/26/15. */ public class JsonApiResponse { private Object data; private List<Map<String, Object>> included; private Map<String, Object> meta; private Map<String, Object> links; public Observable<Map<String, Object>> included() { return Observable.from(included); } public Observable<Map<String, Object>> data() { if(data instanceof List) return Observable.from((List<Map<String,Object>>)data); else return Observable.just((Map<String,Object>)data); } } -
Gregadeaux revised this gist
Jul 12, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -108,7 +108,7 @@ private static String fromJsonApi(String json) throws Exception { .first() .subscribe(included -> link.putAll((Map<String, Object>) included.get(ATTRIBUTES_KEY)))); }); stringObjectMap.remove(RELATIONSHIP_KEY); }) .subscribe(datum -> data.add(datum)); -
Gregadeaux revised this gist
Jul 12, 2015 . 1 changed file with 6 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -90,7 +90,7 @@ private static String fromJsonApi(String json) throws Exception { response.data() .doOnNext(stringObjectMap -> { stringObjectMap.putAll((Map<String, Object>) stringObjectMap.get(ATTRIBUTES_KEY)); stringObjectMap.remove(ATTRIBUTES_KEY); Observable.from(((Map<String, Object>) stringObjectMap.get(RELATIONSHIP_KEY)).keySet()) .filter(key -> ((Map<String, Object>) stringObjectMap.get(RELATIONSHIP_KEY)).get(key) instanceof Map || ((Map<String, Object>) stringObjectMap.get(RELATIONSHIP_KEY)).get(key) instanceof List) .subscribe(key -> { @@ -99,13 +99,14 @@ private static String fromJsonApi(String json) throws Exception { Observable<Map<String, Object>> inner; Object includedLinks = stringObjectMap.get(key); if (includedLinks instanceof List) inner = Observable.from((List<Map<String, Object>>) includedLinks); else inner = Observable.just((Map<String, Object>) includedLinks); inner.forEach(link -> response.included() .filter(included -> included.get("type").equals(link.get("type")) && included.get("id").equals(link.get("id"))) .first() .subscribe(included -> link.putAll((Map<String, Object>) included.get(ATTRIBUTES_KEY)))); }); stringObjectMap.remove("relationships"); }) -
Gregadeaux created this gist
Jul 12, 2015 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,125 @@ package ai.cometandroid.network; import com.google.gson.Gson; import com.google.gson.internal.LinkedHashTreeMap; import com.squareup.moshi.JsonAdapter; import com.squareup.moshi.Moshi; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; import java.util.Map; import ai.cometandroid.models.JsonApiResponse; import retrofit.converter.ConversionException; import retrofit.converter.Converter; import retrofit.mime.TypedInput; import retrofit.mime.TypedOutput; import rx.Observable; /** * Created by greg on 6/26/15. */ public class JsonApiConverter implements Converter { private static String RELATIONSHIP_KEY = "relationships"; private static String ATTRIBUTES_KEY = "attributes"; @Override public Object fromBody(TypedInput body, Type type) throws ConversionException { try { InputStream in = body.in(); String json = fromStream(in); if(String.class.equals(type)) { return json; } else { return new Gson().fromJson(fromJsonApi(json), type); } } catch (Exception e) { throw new ConversionException(e); } } @Override public TypedOutput toBody(Object object) { try { Map<String, Object> jsonApi = new LinkedHashTreeMap<>(); jsonApi.put("data", object); return new JsonTypedOutput(new Gson().toJson(jsonApi).getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { throw new AssertionError(e); } } private static class JsonTypedOutput implements TypedOutput { private final byte[] jsonBytes; JsonTypedOutput(byte[] jsonBytes) { this.jsonBytes = jsonBytes; } @Override public String fileName() { return null; } @Override public String mimeType() { return "application/json; charset=UTF-8"; } @Override public long length() { return jsonBytes.length; } @Override public void writeTo(OutputStream out) throws IOException { out.write(jsonBytes); } } private static String fromStream(InputStream in) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(in)); StringBuilder out = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { out.append(line); out.append("\r\n"); } return out.toString(); } private static String fromJsonApi(String json) throws Exception { Moshi moshi = new Moshi.Builder().build(); JsonAdapter<JsonApiResponse> adapter = moshi.adapter(JsonApiResponse.class); JsonApiResponse response = adapter.fromJson(json); List<Map<String, Object>> data = new ArrayList<>(); response.data() .doOnNext(stringObjectMap -> { stringObjectMap.putAll((Map<String, Object>) stringObjectMap.get(ATTRIBUTES_KEY)); stringObjectMap.remove("attributes"); Observable.from(((Map<String, Object>) stringObjectMap.get(RELATIONSHIP_KEY)).keySet()) .filter(key -> ((Map<String, Object>) stringObjectMap.get(RELATIONSHIP_KEY)).get(key) instanceof Map || ((Map<String, Object>) stringObjectMap.get(RELATIONSHIP_KEY)).get(key) instanceof List) .subscribe(key -> { stringObjectMap.put(key, ((Map<String, Object>) ((Map<String, Object>) stringObjectMap.get(RELATIONSHIP_KEY)).get(key)).get("data")); Observable<Map<String, Object>> inner; Object includedLinks = stringObjectMap.get(key); if (includedLinks instanceof List) inner = Observable.from((List<Map<String, Object>>) includedLinks); else inner = Observable.just((Map<String, Object>) includedLinks); inner.forEach(link -> response.included() .filter(included -> included.get("type").equals(link.get("type")) && included.get("id").equals(link.get("id"))) .first() .subscribe(included -> link.putAll((Map<String, Object>) included.get(ATTRIBUTES_KEY)))); }); stringObjectMap.remove("relationships"); }) .subscribe(datum -> data.add(datum)); String formatted; if(data.size() > 1) { formatted = moshi.adapter(List.class).toJson(data); }else { formatted = moshi.adapter(Map.class).toJson(data.get(0)); } return formatted; } }