com.pkrete.locationservice.admin.deserializers.LanguageJSONDeserializer.java Source code

Java tutorial

Introduction

Here is the source code for com.pkrete.locationservice.admin.deserializers.LanguageJSONDeserializer.java

Source

/**
 * This file is part of Location Service :: Admin. Copyright (C) 2014 Petteri
 * Kivimki
 *
 * Location Service :: Admin is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * Location Service :: Admin is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * Location Service :: Admin. If not, see <http://www.gnu.org/licenses/>.
 */
package com.pkrete.locationservice.admin.deserializers;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.pkrete.locationservice.admin.model.language.Language;
import java.io.IOException;

/**
 * Custom deserializer for Language objects.
 *
 * @author Petteri Kivimki
 */
public class LanguageJSONDeserializer extends JsonDeserializer<Language> {

    @Override
    public Language deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
            throws IOException {

        ObjectCodec oc = jsonParser.getCodec();
        JsonNode node = oc.readTree(jsonParser);
        int id = node.get("id") == null ? 0 : node.get("id").intValue();
        String name = node.get("name") == null ? "" : node.get("name").textValue();
        String code = node.get("code") == null ? "" : node.get("code").textValue();
        return new Language(id, name, code);
    }
}