Search in sources :

Example 11 with ValueCompositeType

use of org.qi4j.api.type.ValueCompositeType in project qi4j-sdk by Qi4j.

the class JSONResponseReader method readResponse.

@Override
public Object readResponse(Response response, Class<?> resultType) {
    if (response.getEntity().getMediaType().equals(MediaType.APPLICATION_JSON)) {
        if (ValueComposite.class.isAssignableFrom(resultType)) {
            String jsonValue = response.getEntityAsText();
            ValueCompositeType valueType = module.valueDescriptor(resultType.getName()).valueType();
            return valueDeserializer.deserialize(valueType, jsonValue);
        } else if (resultType.equals(Form.class)) {
            try {
                String jsonValue = response.getEntityAsText();
                JSONObject jsonObject = new JSONObject(jsonValue);
                Iterator<?> keys = jsonObject.keys();
                Form form = new Form();
                while (keys.hasNext()) {
                    Object key = keys.next();
                    form.set(key.toString(), jsonObject.get(key.toString()).toString());
                }
                return form;
            } catch (JSONException e) {
                throw new ResourceException(e);
            }
        }
    }
    return null;
}
Also used : JSONObject(org.json.JSONObject) Form(org.restlet.data.Form) Iterator(java.util.Iterator) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) ResourceException(org.restlet.resource.ResourceException) ValueCompositeType(org.qi4j.api.type.ValueCompositeType)

Aggregations

ValueCompositeType (org.qi4j.api.type.ValueCompositeType)11 ValueType (org.qi4j.api.type.ValueType)7 CollectionType (org.qi4j.api.type.CollectionType)5 ArrayList (java.util.ArrayList)4 LinkedHashMap (java.util.LinkedHashMap)4 List (java.util.List)4 Map (java.util.Map)4 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)4 MapType (org.qi4j.api.type.MapType)4 ValueSerializationException (org.qi4j.api.value.ValueSerializationException)4 HashMap (java.util.HashMap)3 QualifiedName (org.qi4j.api.common.QualifiedName)3 ValueComposite (org.qi4j.api.value.ValueComposite)3 Collection (java.util.Collection)2 LinkedHashSet (java.util.LinkedHashSet)2 Set (java.util.Set)2 Preferences (java.util.prefs.Preferences)2 URI (org.openrdf.model.URI)2 ValueFactory (org.openrdf.model.ValueFactory)2 EntityReference (org.qi4j.api.entity.EntityReference)2