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

com.urbanairship.api.attributelists.parse.AttributeListsViewReader Maven / Gradle / Ivy

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

package com.urbanairship.api.attributelists.parse;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.collect.ImmutableMap;
import com.urbanairship.api.attributelists.model.AttributeListsView;
import com.urbanairship.api.common.parse.APIParsingException;
import com.urbanairship.api.common.parse.JsonObjectReader;
import org.joda.time.DateTime;

import java.io.IOException;
import java.util.Map;

public class AttributeListsViewReader implements JsonObjectReader {
    private final AttributeListsView.Builder builder;

    public AttributeListsViewReader() {
        this.builder = AttributeListsView.newBuilder();
    }

    public void readOk(JsonParser jsonParser) throws IOException {
        builder.setOk(jsonParser.readValueAs(Boolean.class));
    }

    public void readName(JsonParser jsonParser) throws IOException {
        builder.setName(jsonParser.readValueAs(String.class));
    }

    public void readDescription(JsonParser jsonParser) throws IOException {
        builder.setDescription(jsonParser.readValueAs(String.class));
    }

    public void readExtras(JsonParser jsonParser) throws IOException {
        Map mutableExtras = jsonParser.readValueAs(new TypeReference>() {});
        ImmutableMap extras = immutableMapConverter(mutableExtras);
        builder.addAllExtras(extras);
    }

    public void readCreated(JsonParser jsonParser) throws IOException {
        builder.setCreated(jsonParser.readValueAs(DateTime.class));
    }

    public void readLastUpdated(JsonParser jsonParser) throws IOException {
        builder.setLastUpdated(jsonParser.readValueAs(DateTime.class));
    }

    public void readChannelCount(JsonParser jsonParser) throws IOException {
        builder.setChannelCount(jsonParser.readValueAs(int.class));
    }

    public void readErrorPath(JsonParser jsonParser) throws IOException {
        builder.setErrorPath(jsonParser.readValueAs(String.class));
    }

    public void readStatus(JsonParser jsonParser) throws IOException {
        builder.setStatus(jsonParser.readValueAs(String.class));
    }


    @Override
    public AttributeListsView validateAndBuild() throws IOException {
        try {
            return builder.build();
        } catch (Exception e) {
            throw new APIParsingException(e.getMessage());
        }
    }

    private static ImmutableMap immutableMapConverter(Map map) {
        ImmutableMap.Builder builder = ImmutableMap.builder();
        for (Map.Entry entry : map.entrySet()) {
            builder.put(entry.getKey(), entry.getValue());
        }
        return builder.build();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy