com.mycompany.gsontesting.EnemyArrayDeserialiser.java Source code

Java tutorial

Introduction

Here is the source code for com.mycompany.gsontesting.EnemyArrayDeserialiser.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 com.mycompany.gsontesting;

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 java_packages.CharacterEnemy;
import java.lang.reflect.Type;

/**
 *
 * @author rory.crickmore
 */
public class EnemyArrayDeserialiser implements JsonDeserializer<JsonEnemies> {

    @Override
    public JsonEnemies deserialize(final JsonElement json, final Type typeOfT,
            final JsonDeserializationContext context) throws JsonParseException {

        //Create generic Json object.
        final JsonObject jsonObject = json.getAsJsonObject();

        CharacterEnemy[] enemy = context.deserialize(jsonObject.get("Enemy"), CharacterEnemy[].class);
        final JsonEnemies arrObj = new JsonEnemies();

        arrObj.setJsonItems(enemy);

        return arrObj;
    }

}