Skip to content

Instantly share code, notes, and snippets.

@dziad
Last active February 15, 2016 21:00
Show Gist options
  • Save dziad/adde0ea13baf95bacdbf to your computer and use it in GitHub Desktop.
Save dziad/adde0ea13baf95bacdbf to your computer and use it in GitHub Desktop.
JsonApi Gson Custom Converter
package com.travelpath.android.api;
import com.desmart.Inflector;
import com.google.gson.*;
import com.travelpath.android.api.responses.AuthenticateResponse;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
/**
* Created by Brodaty on 02.12.2015.
*
* //GsonBuilder gsonBuilder = new GsonBuilder();
* //gsonBuilder.registerTypeAdapter(AuthenticateResponse.class, new CustomDeserializer<>(AuthenticateResponse.class));
* //Gson gson = gsonBuilder.create();
*/
class CustomDeserializer<T> implements JsonDeserializer<T> {
private static final String INCLUDED_KEY = "included";
private static final String RELATIONSHIPS_KEY = "relationships";
private static final String ATTRIBUTES_KEY = "attributes";
private static final String TYPE_KEY = "type";
private static final String ID_KEY = "id";
private Class<T> mClass;
private String mKey;
private JsonElement je;
private JsonElement includes;
public CustomDeserializer(Class<T> targetClass) {
mClass = targetClass;
mKey = "data";
}
@Override
public T deserialize(JsonElement je, Type type, JsonDeserializationContext jdc)
throws JsonParseException {
this.je = je;
setIncludes();
JsonElement jsonElement = getBaseJsonElement();
return new Gson().fromJson(jsonElement, mClass);
}
private void setIncludes() {
includes = this.je.getAsJsonObject().get(INCLUDED_KEY);
JsonElement newIncludes = new JsonArray();
for (int i = 0; i < includes.getAsJsonArray().size(); i++) {
JsonElement jsonElement = includes.getAsJsonArray().get(i);
jsonElement = getElementWithAttributes(jsonElement);
addRelations(jsonElement);
newIncludes.getAsJsonArray().add(jsonElement);
}
includes = newIncludes;
}
private JsonElement getBaseJsonElement() {
JsonElement jsonElement = getElementWithAttributes(this.je.getAsJsonObject().get(mKey));
addRelations(jsonElement);
return jsonElement;
}
private void addRelations(JsonElement jsonElement) {
if (false == hasRelationshipsKey(jsonElement)) {
return;
}
for (int i = 0; i < includes.getAsJsonArray().size(); i++) {
addRelation(jsonElement, i);
}
jsonElement.getAsJsonObject().remove(RELATIONSHIPS_KEY);
}
private void addRelation(JsonElement jsonElement, int i) {
extractSingleRelationship(jsonElement, i);
extractManyRelationship(jsonElement, i);
}
private void extractManyRelationship(JsonElement jsonElement, int i) {
Inflector inflector = new Inflector();
JsonObject relationships = (JsonObject) jsonElement.getAsJsonObject().get(RELATIONSHIPS_KEY);
String typeString = includes.getAsJsonArray().get(i).getAsJsonObject().get(TYPE_KEY).getAsString();
String pluralizedTypeString = inflector.pluralize(inflector.camelCase(typeString, false));
if(relationships.has(pluralizedTypeString)) {
JsonArray tmpJson = (JsonArray) relationships.get(pluralizedTypeString).getAsJsonObject().get(mKey);
for (int x = 0; x < tmpJson.getAsJsonArray().size(); x++) {
addKeyToJsonElement(jsonElement, pluralizedTypeString);
String tmpElementID = tmpJson.getAsJsonArray().get(x).getAsJsonObject().get(ID_KEY).getAsString();
String includeId = includes.getAsJsonArray().get(i).getAsJsonObject().get(ID_KEY).getAsString();
if (tmpElementID.equals(includeId) && false == jsonElement.getAsJsonObject().get(pluralizedTypeString).getAsJsonArray().contains(getElementWithAttributes(includes.getAsJsonArray().get(i)))) {
jsonElement.getAsJsonObject().get(pluralizedTypeString).getAsJsonArray().add(getElementWithAttributes(includes.getAsJsonArray().get(i)));
}
}
}
}
private void extractSingleRelationship(JsonElement jsonElement, int i) {
String typeString = includes.getAsJsonArray().get(i).getAsJsonObject().get(TYPE_KEY).getAsString();
JsonObject relationships = (JsonObject) jsonElement.getAsJsonObject().get(RELATIONSHIPS_KEY);
if(null != relationships && relationships.has(typeString)) {
String tmpElementID = relationships.get(typeString).getAsJsonObject().get(mKey).getAsJsonObject().get(ID_KEY).getAsString();
String includeId = includes.getAsJsonArray().get(i).getAsJsonObject().get(ID_KEY).getAsString();
if (tmpElementID.equals(includeId)) {
jsonElement.getAsJsonObject().add(
typeString,
includes.getAsJsonArray().get(i)
);
}
}
}
private boolean hasRelationshipsKey(JsonElement jsonElement) {
return jsonElement.getAsJsonObject().has(RELATIONSHIPS_KEY);
}
private void addKeyToJsonElement(JsonElement jsonElement, String pluralizedTypeString) {
if (false == jsonElement.getAsJsonObject().has(pluralizedTypeString)) {
jsonElement.getAsJsonObject().add(pluralizedTypeString, new JsonArray());
}
}
private JsonElement getElementWithAttributes(JsonElement content) {
JsonElement baseElement = content.getAsJsonObject().get(ATTRIBUTES_KEY);
baseElement.getAsJsonObject().add(ID_KEY, content.getAsJsonObject().get(ID_KEY));
baseElement.getAsJsonObject().add(TYPE_KEY, content.getAsJsonObject().get(TYPE_KEY));
if (true == hasRelationshipsKey(content)) {
baseElement.getAsJsonObject().add(RELATIONSHIPS_KEY, content.getAsJsonObject().get(RELATIONSHIPS_KEY));
}
return baseElement;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment