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

com.urbanairship.api.common.parse.StandardObjectDeserializer Maven / Gradle / Ivy

There is a newer version: 9.4.2
Show newest version
/*
 * Copyright (c) 2013-2016.  Urban Airship and Contributors
 */

package com.urbanairship.api.common.parse;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.google.common.base.Supplier;

import java.io.IOException;
import java.util.Optional;

public class StandardObjectDeserializer> {

    private final FieldParserRegistry registry;
    private final Supplier readerSupplier;

    public StandardObjectDeserializer(FieldParserRegistry registry, Supplier readerSupplier) {
        this.registry = registry;
        this.readerSupplier = readerSupplier;
    }

    public T deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
        JsonToken token = jp.getCurrentToken();
        if (token == JsonToken.START_OBJECT) {
            token = jp.nextToken();
        }

        R reader = readerSupplier.get();
        while (token != null && token != JsonToken.END_OBJECT) {
            if (token != JsonToken.FIELD_NAME) {
                APIParsingException.raise("Parsing of json failed.  Expected to be at field name token but was " + token.name(), jp);
            }

            String name = jp.getCurrentName();
            jp.nextToken();

            Optional> fieldParser = registry.getFieldParser(name);
            if (fieldParser.isPresent()) {
                fieldParser.get().parse(reader, jp, ctxt);
            }
            else {
                jp.skipChildren();

            }

            token = jp.nextToken();
        }

        return reader.validateAndBuild();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy