com.ga2sa.utils.ArrayToStringDeserializer.java Source code

Java tutorial

Introduction

Here is the source code for com.ga2sa.utils.ArrayToStringDeserializer.java

Source

/**
 * This document is a part of the source code and related artifacts
 * for GA2SA, an open source code for Google Analytics to 
 * Salesforce Analytics integration.
 *
 * Copyright  2015 Cervello Inc.,
 *
 *
 * 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.
 */
package com.ga2sa.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;

/**
 * @author Sergey Legostaev
 *
 */
public class ArrayToStringDeserializer extends JsonDeserializer<String> {

    /* (non-Javadoc)
     * @see com.fasterxml.jackson.databind.JsonDeserializer#deserialize(com.fasterxml.jackson.core.JsonParser, com.fasterxml.jackson.databind.DeserializationContext)
     */
    @Override
    public String deserialize(JsonParser jsonParser, DeserializationContext context)
            throws IOException, JsonProcessingException {
        List<String> array = new ArrayList<String>();
        JsonNode tree = jsonParser.readValueAsTree();
        if (tree.isArray()) {
            tree.forEach(obj -> array.add(obj.textValue()));
        } else {
            array.add(tree.textValue());
        }

        return StringUtils.join(array, ",");
    }

}