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

brooklyn.event.feed.http.JsonUtil.workinprogress Maven / Gradle / Ivy

There is a newer version: 0.7.0-M1
Show newest version
package brooklyn.event.feed.http;

import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

public class JsonUtil {

    private JsonUtil() {} // instead use static utility methods
    
    public static JsonElement asJson(String input) {
        return new JsonParser().parse(input);
    }

    public static  Function> forEach(final Function func) {
        return new Function>() {
            @Override public List apply(JsonElement input) {
                JsonArray array = (JsonArray) input;
                List result = Lists.newArrayList();
                for (int i = 0; i < array.size(); i++) {
                    result.add(func.apply(array.get(i)));
                }
                return result;
            }
        };
    }

    public static JsonElement walk(JsonElement input, String... elements) {
        JsonElement curr = input;
        for (String element : elements) {
            curr = curr.getAsJsonObject().get(element);
        }
        return curr;
    }
    
    public static  T cast(JsonElement input, final Class expected) {
        if (input == null) {
            return (T) null;
        } else if (input.isJsonNull()) {
            return (T) null;
        } else if (expected == boolean.class || expected == Boolean.class) {
            return (T) (Boolean) input.getAsBoolean();
        } else if (expected == char.class || expected == Character.class) {
            return (T) (Character) input.getAsCharacter();
        } else if (expected == byte.class || expected == Byte.class) {
            return (T) (Byte) input.getAsByte();
        } else if (expected == short.class || expected == Short.class) {
            return (T) (Short) input.getAsShort();
        } else if (expected == int.class || expected == Integer.class) {
            return (T) (Integer) input.getAsInt();
        } else if (expected == long.class || expected == Long.class) {
            return (T) (Long) input.getAsLong();
        } else if (expected == float.class || expected == Float.class) {
            return (T) (Float) input.getAsFloat();
        } else if (expected == double.class || expected == Double.class) {
            return (T) (Double) input.getAsDouble();
        } else if (expected == BigDecimal.class) {
            return (T) input.getAsBigDecimal();
        } else if (expected == BigInteger.class) {
            return (T) input.getAsBigInteger();
        } else if (Number.class.isAssignableFrom(expected)) {
            // TODO Will result in a class-cast if it's an unexpected sub-type of Number not handled above
            return (T) input.getAsNumber();
        } else if (expected == String.class) {
            return (T) input.getAsString();
        } else if (expected.isArray()) {
            JsonArray array = input.getAsJsonArray();
            Class componentType = expected.getComponentType();
            if (JsonElement.class.isAssignableFrom(componentType)) {
                JsonElement[] result = new JsonElement[array.size()];
                for (int i = 0; i < array.size(); i++) {
                    result[i] = array.get(i);
                }
                return (T) result;
            } else {
                Object[] result = (Object[]) Array.newInstance(componentType, array.size());
                for (int i = 0; i < array.size(); i++) {
                    result[i] = cast(array.get(i), componentType);
                }
                return (T) result;
            }
        } else {
            throw new IllegalArgumentException("Cannot cast json element to type "+expected);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy