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

com.urbanairship.api.nameduser.parse.NamedUserViewReader 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.nameduser.parse;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.urbanairship.api.channel.model.ChannelView;
import com.urbanairship.api.common.parse.APIParsingException;
import com.urbanairship.api.common.parse.JsonObjectReader;
import com.urbanairship.api.common.parse.StringFieldDeserializer;
import com.urbanairship.api.nameduser.model.NamedUserView;

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

public class NamedUserViewReader implements JsonObjectReader {
    ObjectMapper mapper = NamedUserObjectMapper.getInstance();
    private NamedUserView.Builder builder;

    public NamedUserViewReader() {
        builder = NamedUserView.newBuilder();
    }

    public void readNamedUserId(JsonParser jsonParser) throws IOException {
        builder.setNamedUserId(StringFieldDeserializer.INSTANCE.deserialize(jsonParser, "named_user_id"));
    }

    public void readNamedUserTags(JsonParser jsonParser) throws IOException {
        Map> mutableTags = jsonParser.readValueAs(new TypeReference>>() {});
        ImmutableMap> tags = immutableMapConverter(mutableTags);
        builder.setNamedUserTags(tags);
    }

    public void readChannelView(JsonParser jsonParser) throws  IOException {
        Set channels = jsonParser.readValueAs(new TypeReference>() {});
        builder.setChannelViews(ImmutableSet.copyOf(channels));
    }

    public void readAttributes(JsonParser jsonParser) throws IOException {
        Map result = mapper.readValue(jsonParser, HashMap.class);
        builder.addAllAttributes(result);
    }

    public void readUserAttributes(JsonParser jsonParser) throws IOException {
        Map result = mapper.readValue(jsonParser, HashMap.class);
        builder.addAllUserAttributes(result);
    }

    @Override
    public NamedUserView 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(), ImmutableSet.copyOf(entry.getValue()));
        }
        return builder.build();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy