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

de.ingogriebsch.spring.hateoas.siren.SirenPagedModelDeserializer Maven / Gradle / Ivy

Go to download

A library implementing Siren as a custom Spring HATEOAS hypermedia type. Siren is a hypermedia specification for representing entities.

The newest version!
/*-
 * Copyright 2019-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.ingogriebsch.spring.hateoas.siren;

import static com.fasterxml.jackson.core.JsonToken.FIELD_NAME;
import static com.fasterxml.jackson.core.JsonToken.START_OBJECT;
import static com.fasterxml.jackson.databind.type.TypeFactory.defaultInstance;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonDeserializer;
import org.springframework.hateoas.PagedModel;
import org.springframework.hateoas.PagedModel.PageMetadata;
import org.springframework.lang.Nullable;

/**
 * {@link JsonDeserializer} implementation which is able to deserialize a Siren entity into a {@link PagedModel}.
 * 
 * @author Ingo Griebsch
 * @since 1.0.0
 * @see PagedModel
 */
class SirenPagedModelDeserializer extends AbstractSirenDeserializer> {

    private static final long serialVersionUID = 4364222303241126575L;
    private static final JavaType TYPE = defaultInstance().constructType(PagedModel.class);

    SirenPagedModelDeserializer(SirenConfiguration configuration, SirenDeserializerFacilities deserializerFacilities) {
        this(configuration, deserializerFacilities, TYPE);
    }

    SirenPagedModelDeserializer(SirenConfiguration configuration, SirenDeserializerFacilities deserializerFacilities,
        JavaType contentType) {
        super(configuration, deserializerFacilities, contentType);
    }

    @Override
    public JsonDeserializer createContextual(DeserializationContext ctxt, @Nullable BeanProperty property) {
        JavaType contentType = property == null ? ctxt.getContextualType() : property.getType().getContentType();
        return new SirenPagedModelDeserializer(configuration, deserializerFacilities, contentType);
    }

    @Override
    protected PagedModel deserializeModel(JsonParser jp, DeserializationContext ctxt) throws IOException {
        SirenPagedModelBuilder builder =
            SirenPagedModelBuilder.builder(contentType, getRepresentationModelFactories().forPagedModel(), getLinkConverter());

        while (jp.nextToken() != null) {
            if (FIELD_NAME.equals(jp.currentToken())) {
                if ("properties".equals(jp.getText())) {
                    builder.metadata(deserializeMetadata(jp, ctxt));
                }

                if ("entities".equals(jp.getText())) {
                    builder.content(deserializeEntities(jp, ctxt));
                }

                if ("links".equals(jp.getText())) {
                    builder.links(deserializeLinks(jp, ctxt));
                }

                if ("actions".equals(jp.getText())) {
                    builder.actions(deserializeActions(jp, ctxt));
                }
            }
        }
        return builder.build();
    }

    private PageMetadata deserializeMetadata(JsonParser jp, DeserializationContext ctxt) throws IOException {
        JsonDeserializer deserializer = getDeserializer(defaultInstance().constructType(PageMetadata.class), jp, ctxt);

        JsonToken nextToken = jp.nextToken();
        if (!START_OBJECT.equals(nextToken)) {
            throw new JsonParseException(jp, String.format("Token does not represent '%s' [but '%s']!", START_OBJECT, nextToken));
        }

        return (PageMetadata) deserializer.deserialize(jp, ctxt);
    }
}