I have a HATEOAS (HAL) REST service and managed to talk to it with the code below (using halarious as a conversion engine) but when I try to merge the converters (stal
package ch.halarious.core;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* Custom Hal Deserializer
*
* @author jaren
*/
public class CustomHalDeserializer extends HalDeserializer {
/**
* Intialisiert ein HalDeserializer-Objekt
*
* @param targetType Typ, den wir eigentlich deserialisieren sollten
*/
public CustomHalDeserializer(Class> targetType) {
super(targetType);
}
class CustomArrayList extends ArrayList implements HalResource{}
public HalResource deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context, Class> targetType) throws JsonParseException {
// Es handelt sich um ein JSON-Objekt.
JsonObject jsonObject = json.getAsJsonObject();
JsonObject embeddedRoot = jsonObject.getAsJsonObject(HalConstants.EMBEDDED_ROOT);
if(embeddedRoot != null){
Set> set = embeddedRoot.entrySet();
if(set.toArray().length == 1){
JsonArray ja = embeddedRoot.getAsJsonArray(set.iterator().next().getKey());
if(ja.isJsonArray()) {
CustomArrayList arrayResult = new CustomArrayList();
Iterator i = ja.iterator();
while(i.hasNext()){
JsonElement je = i.next();
arrayResult.add(super.deserialize(je, typeOfT, context, targetType));
}
return arrayResult;
}
}
}
return super.deserialize(json, typeOfT, context, targetType);
}
}