Cyber.Utils.gSonArea.java Source code

Java tutorial

Introduction

Here is the source code for Cyber.Utils.gSonArea.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Cyber.Utils;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import java.lang.reflect.Type;

public class gSonArea implements JsonSerializer<Area>, JsonDeserializer<Area> {

    @Override
    public JsonElement serialize(Area t, Type type, JsonSerializationContext jsc) {
        JsonObject jo = new JsonObject();
        jo.addProperty("name", t.name);
        return jo;
    }

    @Override
    public Area deserialize(JsonElement je, Type type, JsonDeserializationContext jdc) throws JsonParseException {
        JsonObject jo = (JsonObject) je;
        if (!jo.has("name")) {
            return null;
        }
        String a = jo.get("name").getAsString();
        if (!Cyber.Main.areas.containsKey(a)) {
            return null;
        }
        return Cyber.Main.areas.get(a);
    }

}