me.trashout.model.serialize.UserActivityTypeSerializer.java Source code

Java tutorial

Introduction

Here is the source code for me.trashout.model.serialize.UserActivityTypeSerializer.java

Source

/*
 * TrashOut is an environmental project that teaches people how to recycle
 * and showcases the worst way of handling waste - illegal dumping.All you need is a smart phone.
 * 
 * 
 * There are 10 types of programmers - those who are helping TrashOut and those who are not.
 * Clean up our code, so we can clean up our planet.
 * Get in touch with us: help@trashout.ngo
 * 
 * Copyright 2017 TrashOut, n.f.
 * 
 * This file is part of the TrashOut project.
 * 
 * This program 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.
 * 
 * This program 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.
 * 
 * See the GNU General Public License for more details: <https://www.gnu.org/licenses/>.
 */

package me.trashout.model.serialize;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import java.lang.reflect.Type;

import me.trashout.model.Constants;

/**
 * @author Miroslav Cupalka
 * @package me.trashout.model.serialize
 * @since 17.02.2017
 */
public class UserActivityTypeSerializer
        implements JsonSerializer<Constants.UserActivityType>, JsonDeserializer<Constants.UserActivityType> {
    @Override
    public Constants.UserActivityType deserialize(JsonElement json, Type typeOfT,
            JsonDeserializationContext context) throws JsonParseException {
        return Constants.UserActivityType.getUserActivityTypeByName(json.getAsString());
    }

    @Override
    public JsonElement serialize(Constants.UserActivityType src, Type typeOfSrc, JsonSerializationContext context) {
        if (src == null)
            return JsonNull.INSTANCE;
        return new JsonPrimitive(src.getName());
    }
}