All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kaltura.client.utils.GsonParser Maven / Gradle / Ivy

Go to download

KalturaClient is a library of Java classes that can be used to interact with the Kaltura REST API. More information about the REST API can be found at http://corp.kaltura.com/Products/Kaltura-API Many of the Java classes in this library are auto-generated from a schema that defines the objects that are used to interect with the API. The current schema can be found at http://www.kaltura.com/api_v3/api_schema.php

There is a newer version: 19.3.0
Show newest version
package com.kaltura.client.utils;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSyntaxException;
import com.kaltura.client.types.APIException;
import com.kaltura.client.types.APIException.FailureStep;
import com.kaltura.client.types.ListResponse;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tehilarozin on 24/07/2016.
 */
public class GsonParser {

	private static final String ResultKey = "result";
	private static final String ObjectTypeKey = "objectType";

    private static Gson gson = new Gson();

    @SuppressWarnings("unchecked")
	public static  Class getObjectClass(String objectType, Class defaultClass) {
        String className = "com.kaltura.client.types." + objectType.replaceAll("^Kaltura", "");

		try {
			return (Class) Class.forName(className);
		} catch (ClassNotFoundException e) {
			return defaultClass;
		}
    }

    public static  T parseObject(String result, Class clz) throws APIException {
        JsonParser jsonParser = new JsonParser();
        JsonElement jsonElement;
        try{
        	jsonElement = jsonParser.parse(result);
        }
        catch(JsonSyntaxException | IllegalStateException e) {
        	throw new APIException(FailureStep.OnResponse, "Invalid JSON response: " + result);
        }
        
        if(jsonElement.isJsonObject()) {
        	JsonObject jsonObject = jsonElement.getAsJsonObject();
        	if(jsonObject.get(ResultKey) != null && jsonObject.get(ObjectTypeKey) == null) {
        		jsonElement = jsonObject.get(ResultKey);
				if (!jsonElement.isJsonPrimitive()) {
                    jsonObject = jsonElement.getAsJsonObject();
                }
        	}
        	if(jsonObject.get("error") != null && jsonObject.get(ObjectTypeKey) == null) {
        		jsonElement = jsonObject.getAsJsonObject("error");
        	}
        }
    	return parseObject(jsonElement, clz);
    }

    @SuppressWarnings("unchecked")
	public static  T parseObject(JsonElement jsonElement, Class clz) throws APIException {
        if(jsonElement.isJsonNull()) {
        	return null;
        }
        
        if(jsonElement.isJsonPrimitive()) {
        	if(clz == String.class) {
        		return (T)jsonElement.getAsString();
        	}
        	if(clz == Integer.class) {
        		return (T)(Integer)jsonElement.getAsInt();
        	}
        	if(clz == Long.class) {
        		return (T)(Long)jsonElement.getAsLong();
        	}
        	if(clz == Boolean.class) {
        		return (T)(Boolean)jsonElement.getAsBoolean();
        	}
        	if(clz == Double.class) {
        		return (T)(Double)jsonElement.getAsDouble();
        	}
        	
        	return (T) (Object) gson.fromJson(jsonElement, Object.class);
        }

        if(jsonElement.isJsonArray()) {
        	return (T) parseArray(jsonElement.getAsJsonArray(), clz);
        }
        
    	return parseObject(jsonElement.getAsJsonObject(), clz);
    }

    public static  T parseObject(JsonObject jsonObject, Class clz) throws APIException {
    	if(jsonObject == null)
    	{
    		return null;
    	}

        JsonPrimitive objectTypeElement = jsonObject.getAsJsonPrimitive(ObjectTypeKey);
        if(objectTypeElement == null && jsonObject.has("error")) {
        	throw parseException(jsonObject.getAsJsonObject("error"));
        }
        else if(objectTypeElement != null) {
	        String objectType = objectTypeElement.getAsString();
	        if(objectType.equals("KalturaAPIException")) {
	        	throw parseException(jsonObject);
	        }
	        clz = getObjectClass(objectType, clz);
        }
        if(clz == Void.class) {
        	return null;
        }
        
        try {
        	Constructor constructor = clz.getConstructor(JsonObject.class);
	        return constructor.newInstance(jsonObject);

        } catch (NoSuchMethodException | SecurityException | InstantiationException |
				IllegalAccessException | IllegalArgumentException | InvocationTargetException |
				ClassCastException | IllegalStateException e) {
			throw new APIException(FailureStep.OnResponse, e);
		}
    }

    public static List parseArray(String result, Class[] types) throws APIException {
        JsonParser jsonParser = new JsonParser();
        JsonElement jsonElement;
        try{
        	jsonElement = jsonParser.parse(result);
        }
        catch(JsonSyntaxException | IllegalStateException e) {
        	throw new APIException(FailureStep.OnResponse, "Invalid JSON response: " + result);
        }

        if(jsonElement.isJsonObject()) {
        	JsonObject jsonObject = jsonElement.getAsJsonObject();
        	if(jsonObject.get(ResultKey) != null && jsonObject.get(ObjectTypeKey) == null) {
        		jsonElement = jsonObject.get(ResultKey);
        	}
        	if(jsonObject.get("error") != null && jsonObject.get(ObjectTypeKey) == null) {
        		jsonElement = jsonObject.getAsJsonObject("error");
        	}
        }
        
        if(jsonElement.isJsonObject()) {
        	JsonObject jsonObject = jsonElement.getAsJsonObject();
	        String objectType = jsonObject.getAsJsonPrimitive(ObjectTypeKey).getAsString();
	        if(objectType.equals("KalturaAPIException")) {
	        	throw parseException(jsonObject);
	        }
        }
        else if(jsonElement.isJsonArray()) {
        	return parseArray(jsonElement.getAsJsonArray(), types);
        }

       	throw new APIException(FailureStep.OnResponse, "Invalid JSON response type, expected array: " + result);
    }

    public static List parseArray(JsonArray jsonArray, Class[] types) throws APIException {
    	if(jsonArray == null)
    	{
    		return null;
    	}
    	
    	List array = new ArrayList();
    	int index = 0;
    	for(JsonElement jsonElement : jsonArray) {
    		try{
    			array.add(parseObject(jsonElement, types[index++]));
    		}
    		catch(APIException e) {
    			array.add(e);
    		}
    	}
    	
    	return array;
    }

    public static  List parseArray(String result, Class clz) throws APIException {
        JsonParser jsonParser = new JsonParser();
        JsonElement jsonElement;
        try{
        	jsonElement = jsonParser.parse(result);
        }
        catch(JsonSyntaxException | IllegalStateException e) {
        	throw new APIException(FailureStep.OnResponse, "Invalid JSON response: " + result);
        }

        if(jsonElement.isJsonObject()) {
			JsonObject jsonObject = jsonElement.getAsJsonObject();
			if (jsonObject.has(ResultKey)){
				jsonElement = jsonObject.getAsJsonArray(ResultKey);
			}
		}

        if(jsonElement.isJsonObject()) {
        	JsonObject jsonObject = jsonElement.getAsJsonObject();
	        String objectType = jsonObject.getAsJsonPrimitive(ObjectTypeKey).getAsString();
	        if(objectType.equals("KalturaAPIException")) {
	        	throw parseException(jsonObject);
	        }
        }
        else if(jsonElement.isJsonArray()) {
        	return parseArray(jsonElement.getAsJsonArray(), clz);
        }

       	throw new APIException(FailureStep.OnResponse, "Invalid JSON response type, expected array of " + clz.getName() + ": " + result);
    }

    public static  List parseArray(JsonArray jsonArray, Class clz) throws APIException {
    	if(jsonArray == null)
    	{
    		return null;
    	}
    	
    	List array = new ArrayList();
    	for(JsonElement jsonElement : jsonArray) {
	        array.add(parseObject(jsonElement, clz));
    	}
    	
    	return array;
    }

    public static  ListResponse parseListResponse(String result, Class clz) throws APIException {
        JsonParser jsonParser = new JsonParser();
        JsonObject jsonObject;
        try{
        	jsonObject = jsonParser.parse(result).getAsJsonObject();
        }
        catch(JsonSyntaxException | IllegalStateException e) {
        	throw new APIException(FailureStep.OnResponse, "Invalid JSON response: " + result);
        }

    	if(jsonObject.get(ResultKey) != null && jsonObject.get(ObjectTypeKey) == null) {
    		jsonObject = jsonObject.getAsJsonObject(ResultKey);
    	}
    	if(jsonObject.get("error") != null && jsonObject.get(ObjectTypeKey) == null) {
    		jsonObject = jsonObject.getAsJsonObject("error");
    	}
        
        String objectType = jsonObject.getAsJsonPrimitive(ObjectTypeKey).getAsString();
        if(objectType.equals("KalturaAPIException")) {
        	throw parseException(jsonObject);
        }
        
        ListResponse listResponse = new ListResponse();
         JsonPrimitive totalCount = jsonObject.getAsJsonPrimitive("totalCount");
        if (null != totalCount) {
            listResponse.setTotalCount(totalCount.getAsInt());
        }
        listResponse.setObjects(parseArray(jsonObject.getAsJsonArray("objects"), clz));

        return listResponse;
    }

    public static APIException parseException(String result) {
        JsonParser jsonParser = new JsonParser();
        JsonElement jsonElement;
        try{
        	jsonElement = jsonParser.parse(result);
        }
        catch(JsonSyntaxException | IllegalStateException e) {
        	return new APIException(FailureStep.OnResponse, "Invalid JSON response: " + result);
        }

        if(jsonElement.isJsonObject()) {
        	return parseException(jsonElement.getAsJsonObject());
        }
        
        return null;
    }

    private static APIException parseException(JsonObject jsonObject) {
        String objectType = jsonObject.getAsJsonPrimitive(ObjectTypeKey).getAsString();
        if(objectType.equals("KalturaAPIException")) {
        	return gson.fromJson(jsonObject, APIException.class);
        }
        
        return null;
    }

    public static String parseString(JsonElement jsonElement) {
    	if(jsonElement == null)
    	{
    		return null;
    	}
    	
    	return jsonElement.getAsString();
    }

    public static Integer parseInt(JsonElement jsonElement) {
    	if(jsonElement == null)
    	{
    		return null;
    	}
    	
    	return jsonElement.getAsInt();
    }

    public static Boolean parseBoolean(JsonElement jsonElement) {
    	if(jsonElement == null)
    	{
    		return null;
    	}
    	
    	return jsonElement.getAsBoolean();
    }

    public static Double parseDouble(JsonElement jsonElement) {
    	if(jsonElement == null)
    	{
    		return null;
    	}
    	
    	return jsonElement.getAsDouble();
    }

    public static Long parseLong(JsonElement jsonElement) {
    	if(jsonElement == null)
    	{
    		return null;
    	}
    	
    	return jsonElement.getAsLong();
    }

    public static  Map parseMap(JsonObject jsonMap, Class clz) throws APIException {
    	if(jsonMap == null)
    	{
    		return null;
    	}
    	
    	JsonObject jsonObject;
    	String objectType;
    	Map map = new HashMap();
    	
		for (Map.Entry entry : jsonMap.entrySet()) {
    		jsonObject = entry.getValue().getAsJsonObject();
	        objectType = jsonObject.getAsJsonPrimitive(ObjectTypeKey).getAsString();
	        if(objectType.equals("KalturaAPIException")) {
	        	throw parseException(jsonObject);
	        }
	        clz = getObjectClass(objectType, clz);
			map.put(entry.getKey(), parseObject(jsonObject, clz));
		}
    	
    	return map;
    }
}