Search in sources :

Example 1 with UnmappedFieldsImpl

use of com.github.davidmoten.odata.client.internal.UnmappedFieldsImpl in project odata-client by davidmoten.

the class Serializer method deserializeToCollection.

private <T> CollectionInfo<T> deserializeToCollection(String json, Class<T> cls, ContextPath contextPath) {
    try {
        ObjectMapper m = MAPPER_EXCLUDE_NULLS;
        ObjectNode o = m.readValue(json, ObjectNode.class);
        List<T> list = new ArrayList<T>();
        for (JsonNode item : o.get("value")) {
            String text = m.writeValueAsString(item);
            Class<? extends T> subClass = RequestHelper.getSubClass(contextPath, contextPath.context().schemas(), cls, text);
            list.add(deserialize(text, subClass, contextPath, true));
        }
        // TODO support relative urls using odata.context if present
        Optional<String> nextLink = Optional.ofNullable(o.get("@odata.nextLink")).map(JsonNode::asText);
        Optional<String> deltaLink = Optional.ofNullable(o.get("@odata.deltaLink")).map(JsonNode::asText);
        @SuppressWarnings("unchecked") Map<String, Object> map = m.convertValue(o, HashMap.class);
        UnmappedFieldsImpl u = new UnmappedFieldsImpl();
        for (String fieldName : map.keySet()) {
            if (!COLLECTION_PAGE_FIELDS.contains(fieldName)) {
                u.put(fieldName, map.get(fieldName));
            }
        }
        return new CollectionInfo<T>(list, nextLink, deltaLink, u);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) UnmappedFieldsImpl(com.github.davidmoten.odata.client.internal.UnmappedFieldsImpl) JsonNode(com.fasterxml.jackson.databind.JsonNode) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 UnmappedFieldsImpl (com.github.davidmoten.odata.client.internal.UnmappedFieldsImpl)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 ArrayList (java.util.ArrayList)1