org.mule.modules.servicesource.ApiResponseDeserealizer.java Source code

Java tutorial

Introduction

Here is the source code for org.mule.modules.servicesource.ApiResponseDeserealizer.java

Source

/**
 * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com
 *
 * The software in this package is published under the terms of the CPAL v1.0
 * license, a copy of which has been included with this distribution in the
 * LICENSE.md file.
 */

package org.mule.modules.servicesource;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.mule.modules.servicesource.model.ApiResponse;
import org.mule.modules.servicesource.model.ServiceSourceEntity;

import com.google.gson.Gson;
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;

/**
 * 
 * @author mariano.gonzalez@mulesoft.com
 *
 */
public class ApiResponseDeserealizer implements JsonDeserializer<ApiResponse<?>> {

    @Override
    public ApiResponse<Object> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
            throws JsonParseException {

        ApiResponse<Object> response = new ApiResponse<Object>();
        JsonObject object = json.getAsJsonObject();

        this.deserealizeSuccess(object, response);
        this.deserealizeData(object, response);
        this.deserealizeRecordCount(object, response);

        return response;
    }

    private void deserealizeRecordCount(JsonObject json, ApiResponse<Object> response) {
        JsonElement element = json.get("recordCount");

        if (element != null) {
            JsonObject count = element.getAsJsonObject();
            Set<Map.Entry<String, JsonElement>> members = count.entrySet();

            if (members.isEmpty()) {
                response.setCount(0);
            } else {
                response.setCount(members.iterator().next().getValue().getAsInt());
            }
        }
    }

    private void deserealizeSuccess(JsonObject json, ApiResponse<Object> response) {
        JsonElement element = json.get("success");
        response.setSuccess(element == null ? true : element.getAsBoolean());
    }

    private void deserealizeData(JsonObject json, ApiResponse<Object> response) {
        JsonElement dataElement = json.get("data");

        if (dataElement == null) {
            return;
        }

        JsonObject data = dataElement.getAsJsonObject();

        Set<Map.Entry<String, JsonElement>> entrySet = data.entrySet();

        if (entrySet.isEmpty()) {
            throw new IllegalArgumentException("data element contained no elements");
        }

        List<Object> records = new ArrayList<Object>();

        Gson gson = GsonFactory.getGson();
        for (Map.Entry<String, JsonElement> entry : entrySet) {
            ServiceSourceCollection collection = ServiceSourceCollection.getByEntityName(entry.getKey());
            if (collection != null) {
                Class<?> type = collection.getType();
                for (JsonElement element : entry.getValue().getAsJsonArray()) {
                    JsonObject object = element.getAsJsonObject();

                    JsonElement extensionElement = element.getAsJsonObject().get("extensions");

                    if (extensionElement != null) {
                        object.remove("extensions");
                    }

                    ServiceSourceEntity entity = (ServiceSourceEntity) gson.fromJson(element, type);
                    records.add(entity);

                    if (extensionElement != null && extensionElement.isJsonObject()) {
                        entity.setExtensions(this.json2Map(extensionElement.getAsJsonObject()));
                    }
                }
            } else {
                JsonElement unknownElement = entry.getValue();

                if (unknownElement.isJsonArray()) {
                    for (JsonElement element : unknownElement.getAsJsonArray()) {
                        records.add(this.json2Map(element.getAsJsonObject()));
                    }
                } else if (unknownElement.isJsonObject()) {
                    records.add(this.json2Map(unknownElement.getAsJsonObject()));
                }
            }
        }

        response.setRecords(records);
    }

    private Map<String, Object> json2Map(JsonObject element) {
        Map<String, Object> record = new HashMap<String, Object>();

        for (Map.Entry<String, JsonElement> member : element.entrySet()) {
            JsonElement value = member.getValue();

            if (value.isJsonObject()) {
                record.put(member.getKey(), this.json2Map(value.getAsJsonObject()));
            } else if (value.isJsonArray()) {
                List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
                for (JsonElement child : value.getAsJsonArray()) {
                    if (!child.isJsonObject()) {
                        child = GsonFactory.getGson().toJsonTree(child);
                    }
                    list.add(this.json2Map(child.getAsJsonObject()));
                }
                record.put(member.getKey(), list);
            } else if (value.isJsonPrimitive()) {
                record.put(member.getKey(), value.getAsJsonPrimitive().getAsString());
            }
        }

        return record;
    }
}