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

io.sphere.sdk.attributes.AttributeMapperImpl Maven / Gradle / Ivy

There is a newer version: 1.0.0-M12
Show newest version
package io.sphere.sdk.attributes;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.sphere.sdk.models.Base;
import io.sphere.sdk.client.JsonParseException;
import io.sphere.sdk.utils.JsonUtils;

import java.io.IOException;

class AttributeMapperImpl extends Base implements AttributeMapper {
    private static final ObjectMapper mapper = JsonUtils.newObjectMapper();
    private final TypeReference typeReference;

    AttributeMapperImpl(final TypeReference typeReference) {
        this.typeReference = typeReference;
    }

    @Override
    public T deserialize(final JsonNode value) {
        try {
            return  mapper.reader(typeReference).readValue(value);
        } catch (final JsonMappingException e) {
            throw new AttributeMappingException(e);
        } catch (final IOException e) {
            throw new JsonParseException(e);
        }
    }

    @Override
    public JsonNode serialize(final T value) {
        return mapper.valueToTree(value);
    }

    protected final ObjectMapper mapper() {
        return mapper;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy