All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.katharsis.jackson.deserializer.ResourceRelationshipsDeserializer Maven / Gradle / Ivy

There is a newer version: 3.0.2
Show newest version
package io.katharsis.jackson.deserializer;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import io.katharsis.jackson.exception.ParametersDeserializationException;
import io.katharsis.request.dto.LinkageData;
import io.katharsis.request.dto.ResourceRelationships;

import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Deserialize ResourceLinks field which can contain either a list of {@link LinkageData} or a single {@link LinkageData}.
 *
 * @see LinkageData
 */
public class ResourceRelationshipsDeserializer extends JsonDeserializer {
    private static final String DATA_FIELD_NAME = "data";

    @Override
    public ResourceRelationships deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
        JsonNode node = jp.readValueAsTree();
        if (node == null) {
            return null;
        }
        ResourceRelationships resourceRelationships = new ResourceRelationships();

        Iterator> fields = node.fields();
        while (fields.hasNext()) {
            Map.Entry field = fields.next();
            Object value;
            if (field.getValue() == null) {
                throw new ParametersDeserializationException("Attribute field cannot be null for: " + field.getKey());
            } else if (field.getValue().get(DATA_FIELD_NAME) == null) {
                value = null;
            } else if (field.getValue().get(DATA_FIELD_NAME).isArray()) {
                JsonNode fieldData = field.getValue().get(DATA_FIELD_NAME);
                Iterator nodeIterator = fieldData.iterator();
                List linkageDatas = new LinkedList<>();

                while (nodeIterator.hasNext()) {
                    LinkageData newLinkageData = jp.getCodec().treeToValue(nodeIterator.next(), LinkageData.class);
                    linkageDatas.add(newLinkageData);
                }
                value = linkageDatas;
            } else {
                value = jp.getCodec().treeToValue(field.getValue().get(DATA_FIELD_NAME), LinkageData.class);
            }
            resourceRelationships.setAdditionalProperty(field.getKey(), value);
        }

        return resourceRelationships;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy