Example usage for com.google.gson JsonElement isJsonPrimitive

List of usage examples for com.google.gson JsonElement isJsonPrimitive

Introduction

In this page you can find the example usage for com.google.gson JsonElement isJsonPrimitive.

Prototype

public boolean isJsonPrimitive() 

Source Link

Document

provides check for verifying if this element is a primitive or not.

Usage

From source file:com.flipkart.android.proteus.processor.DimensionAttributeProcessor.java

License:Apache License

/**
 * @param view View//from www.j a v  a2  s.c  o  m
 */
@Override
public final void handle(String key, JsonElement value, T view) {
    if (value != null && value.isJsonPrimitive()) {
        float dimension = ParseHelper.parseDimension(value.getAsString(), view.getContext());
        setDimension(dimension, view, key, value);
    }
}

From source file:com.flipkart.android.proteus.processor.DrawableResourceProcessor.java

License:Apache License

@Override
public void handle(String key, JsonElement value, V view) {
    if (value.isJsonPrimitive()) {
        handleString(key, value.getAsString(), view);
    } else if (value.isJsonObject()) {
        handleElement(key, value, view);
    } else {/*from   www.j  a  va 2 s  . c  om*/
        if (ProteusConstants.isLoggingEnabled()) {
            Log.e(TAG, "Resource for key: " + key + " must be a primitive or an object. value -> "
                    + value.toString());
        }
    }
}

From source file:com.flipkart.android.proteus.processor.StringAttributeProcessor.java

License:Apache License

/**
 * @param view View//from w  ww .  j a v a 2 s  .  com
 */
@Override
public void handle(String key, JsonElement value, V view) {
    if (value.isJsonPrimitive()) {
        handle(key, getStringFromAttribute(view, value.getAsString()), view);
    } else {
        handle(key, value.toString(), view);
    }
}

From source file:com.flipkart.android.proteus.toolbox.AnimationUtils.java

License:Apache License

/**
 * Loads an {@link Animation} object from a resource
 *
 * @param context Application context used to access resources
 * @param value   JSON representation of the Animation
 * @return The animation object reference by the specified id
 * @throws android.content.res.Resources.NotFoundException when the animation cannot be loaded
 *///from   w  w w  .j  a  v  a 2  s . co m
public static Animation loadAnimation(Context context, JsonElement value) throws Resources.NotFoundException {
    Animation anim = null;
    if (value.isJsonPrimitive()) {
        anim = handleString(context, value.getAsString());
    } else if (value.isJsonObject()) {
        anim = handleElement(context, value.getAsJsonObject());
    } else {
        if (ProteusConstants.isLoggingEnabled()) {
            Log.e(TAG, "Could not load animation for : " + value.toString());
        }
    }
    return anim;
}

From source file:com.flipkart.android.proteus.toolbox.AnimationUtils.java

License:Apache License

/**
 * Loads an {@link Interpolator} object from a resource
 *
 * @param context Application context used to access resources
 * @param value   Json representation of the Interpolator
 * @return The animation object reference by the specified id
 * @throws android.content.res.Resources.NotFoundException
 *///from   ww w  .j  a v a  2s.  c  om
public static Interpolator loadInterpolator(Context context, JsonElement value)
        throws Resources.NotFoundException {
    Interpolator interpolator = null;
    if (value.isJsonPrimitive()) {
        interpolator = handleStringInterpolator(context, value.getAsString());
    } else if (value.isJsonObject()) {
        interpolator = handleElementInterpolator(context, value.getAsJsonObject());
    } else {
        if (ProteusConstants.isLoggingEnabled()) {
            Log.e(TAG, "Could not load interpolator for : " + value.toString());
        }
    }
    return interpolator;
}

From source file:com.flipkart.android.proteus.toolbox.ColorUtils.java

License:Apache License

/**
 * @param context                Application context used to access resources
 * @param value                  JSON representation of the Color
 * @param colorCallback          Callback for return Value if it is a Color Resource
 * @param colorStateListCallback Callback for return Value if it is a ColorStateList
 * @throws android.content.res.Resources.NotFoundException when the animation cannot be loaded
 *///w w w . j a v  a  2  s  . co  m
public static void loadColor(Context context, JsonElement value, ValueCallback<Integer> colorCallback,
        ValueCallback<ColorStateList> colorStateListCallback) throws Resources.NotFoundException {
    if (value.isJsonPrimitive()) {
        handleString(context, value.getAsString(), colorCallback, colorStateListCallback);
    } else if (value.isJsonObject()) {
        handleElement(context, value.getAsJsonObject(), colorCallback, colorStateListCallback);
    } else {
        if (ProteusConstants.isLoggingEnabled()) {
            Log.e(TAG, "Could not color for : " + value.toString());
        }
    }
}

From source file:com.flipkart.android.proteus.toolbox.ColorUtils.java

License:Apache License

private static ColorStateList inflateFromJson(Context context, JsonObject jsonObject) {
    ColorStateList result = null;//from w ww.java  2  s. c  o  m
    JsonElement type = jsonObject.get("type");
    if (null != type && type.isJsonPrimitive()) {
        String colorType = type.getAsString();
        if (TextUtils.equals(colorType, "selector")) {
            JsonElement childrenElement = jsonObject.get("children");

            if (null != childrenElement && childrenElement.isJsonArray()) {
                JsonArray children = childrenElement.getAsJsonArray();
                int listAllocated = 20;
                int listSize = 0;
                int[] colorList = new int[listAllocated];
                int[][] stateSpecList = new int[listAllocated][];

                for (int idx = 0; idx < children.size(); idx++) {
                    JsonElement itemObject = children.get(idx);
                    if (!itemObject.isJsonObject()) {
                        continue;
                    }

                    Set<Map.Entry<String, JsonElement>> entrySet = ((JsonObject) itemObject).entrySet();
                    if (entrySet.size() == 0) {
                        continue;
                    }

                    int j = 0;
                    Integer baseColor = null;
                    float alphaMod = 1.0f;

                    int[] stateSpec = new int[entrySet.size() - 1];
                    boolean ignoreItem = false;
                    for (Map.Entry<String, JsonElement> entry : entrySet) {
                        if (ignoreItem) {
                            break;
                        }
                        if (!entry.getValue().isJsonPrimitive()) {
                            continue;
                        }
                        Integer attributeId = getAttribute(entry.getKey());
                        if (null != attributeId) {
                            switch (attributeId) {
                            case android.R.attr.type:
                                if (!TextUtils.equals("item", entry.getValue().getAsString())) {
                                    ignoreItem = true;
                                }
                                break;
                            case android.R.attr.color:
                                String colorRes = entry.getValue().getAsString();
                                if (!TextUtils.isEmpty(colorRes)) {
                                    baseColor = getColorFromAttributeValue(context, colorRes);
                                }
                                break;
                            case android.R.attr.alpha:
                                String alphaStr = entry.getValue().getAsString();
                                if (!TextUtils.isEmpty(alphaStr)) {
                                    alphaMod = Float.parseFloat(alphaStr);
                                }
                                break;
                            default:
                                stateSpec[j++] = entry.getValue().getAsBoolean() ? attributeId : -attributeId;
                                break;
                            }
                        }
                    }
                    if (!ignoreItem) {
                        stateSpec = StateSet.trimStateSet(stateSpec, j);
                        if (null == baseColor) {
                            throw new IllegalStateException("No Color Specified");
                        }

                        if (listSize + 1 >= listAllocated) {
                            listAllocated = idealIntArraySize(listSize + 1);
                            int[] ncolor = new int[listAllocated];
                            System.arraycopy(colorList, 0, ncolor, 0, listSize);
                            int[][] nstate = new int[listAllocated][];
                            System.arraycopy(stateSpecList, 0, nstate, 0, listSize);
                            colorList = ncolor;
                            stateSpecList = nstate;
                        }

                        final int color = modulateColorAlpha(baseColor, alphaMod);

                        colorList[listSize] = color;
                        stateSpecList[listSize] = stateSpec;
                        listSize++;
                    }
                }
                if (listSize > 0) {
                    int[] colors = new int[listSize];
                    int[][] stateSpecs = new int[listSize][];
                    System.arraycopy(colorList, 0, colors, 0, listSize);
                    System.arraycopy(stateSpecList, 0, stateSpecs, 0, listSize);
                    result = new ColorStateList(stateSpecs, colors);
                }
            }
        }
    }
    return result;
}

From source file:com.flipkart.android.proteus.toolbox.Utils.java

License:Apache License

public static Result readJson(String path, JsonObject data, int index) {
    // replace INDEX reference with index value
    if (ProteusConstants.INDEX.equals(path)) {
        path = path.replace(ProteusConstants.INDEX, String.valueOf(index));
        return Result.success(new JsonPrimitive(path));
    } else {//from  w  ww  . j  av  a  2s  .  c o  m
        StringTokenizer tokenizer = new StringTokenizer(path, ProteusConstants.DATA_PATH_DELIMITERS);
        JsonElement elementToReturn = data;
        JsonElement tempElement;
        JsonArray tempArray;

        while (tokenizer.hasMoreTokens()) {
            String segment = tokenizer.nextToken();
            if (elementToReturn == null) {
                return Result.NO_SUCH_DATA_PATH_EXCEPTION;
            }
            if (elementToReturn.isJsonNull()) {
                return Result.JSON_NULL_EXCEPTION;
            }
            if ("".equals(segment)) {
                continue;
            }
            if (elementToReturn.isJsonArray()) {
                tempArray = elementToReturn.getAsJsonArray();

                if (ProteusConstants.INDEX.equals(segment)) {
                    if (index < tempArray.size()) {
                        elementToReturn = tempArray.get(index);
                    } else {
                        return Result.NO_SUCH_DATA_PATH_EXCEPTION;
                    }
                } else if (ProteusConstants.ARRAY_DATA_LENGTH_REFERENCE.equals(segment)) {
                    elementToReturn = new JsonPrimitive(tempArray.size());
                } else if (ProteusConstants.ARRAY_DATA_LAST_INDEX_REFERENCE.equals(segment)) {
                    if (tempArray.size() == 0) {
                        return Result.NO_SUCH_DATA_PATH_EXCEPTION;
                    }
                    elementToReturn = tempArray.get(tempArray.size() - 1);
                } else {
                    try {
                        index = Integer.parseInt(segment);
                    } catch (NumberFormatException e) {
                        return Result.INVALID_DATA_PATH_EXCEPTION;
                    }
                    if (index < tempArray.size()) {
                        elementToReturn = tempArray.get(index);
                    } else {
                        return Result.NO_SUCH_DATA_PATH_EXCEPTION;
                    }
                }
            } else if (elementToReturn.isJsonObject()) {
                tempElement = elementToReturn.getAsJsonObject().get(segment);
                if (tempElement != null) {
                    elementToReturn = tempElement;
                } else {
                    return Result.NO_SUCH_DATA_PATH_EXCEPTION;
                }
            } else if (elementToReturn.isJsonPrimitive()) {
                return Result.INVALID_DATA_PATH_EXCEPTION;
            } else {
                return Result.NO_SUCH_DATA_PATH_EXCEPTION;
            }
        }
        if (elementToReturn.isJsonNull()) {
            return Result.JSON_NULL_EXCEPTION;
        }
        return Result.success(elementToReturn);
    }
}

From source file:com.flipkart.android.proteus.toolbox.Utils.java

License:Apache License

public static JsonElement merge(JsonElement oldJson, JsonElement newJson, boolean useCopy, Gson gson) {

    JsonElement newDataElement;/*  w w w  .  java 2 s.  c o  m*/
    JsonArray oldArray;
    JsonArray newArray;
    JsonElement oldArrayItem;
    JsonElement newArrayItem;
    JsonObject oldObject;

    if (oldJson == null || oldJson.isJsonNull()) {
        return useCopy ? gson.fromJson(newJson, JsonElement.class) : newJson;
    }

    if (newJson == null || newJson.isJsonNull()) {
        newJson = JsonNull.INSTANCE;
        return newJson;
    }

    if (newJson.isJsonPrimitive()) {
        JsonPrimitive value;
        if (!useCopy) {
            return newJson;
        }
        if (newJson.getAsJsonPrimitive().isBoolean()) {
            value = new JsonPrimitive(newJson.getAsBoolean());
        } else if (newJson.getAsJsonPrimitive().isNumber()) {
            value = new JsonPrimitive(newJson.getAsNumber());
        } else if (newJson.getAsJsonPrimitive().isString()) {
            value = new JsonPrimitive(newJson.getAsString());
        } else {
            value = newJson.getAsJsonPrimitive();
        }
        return value;
    }

    if (newJson.isJsonArray()) {
        if (!oldJson.isJsonArray()) {
            return useCopy ? gson.fromJson(newJson, JsonArray.class) : newJson;
        } else {
            oldArray = oldJson.getAsJsonArray();
            newArray = newJson.getAsJsonArray();

            if (oldArray.size() > newArray.size()) {
                while (oldArray.size() > newArray.size()) {
                    oldArray.remove(oldArray.size() - 1);
                }
            }

            for (int index = 0; index < newArray.size(); index++) {
                if (index < oldArray.size()) {
                    oldArrayItem = oldArray.get(index);
                    newArrayItem = newArray.get(index);
                    oldArray.set(index, merge(oldArrayItem, newArrayItem, useCopy, gson));
                } else {
                    oldArray.add(newArray.get(index));
                }
            }
        }
    } else if (newJson.isJsonObject()) {
        if (!oldJson.isJsonObject()) {
            return useCopy ? gson.fromJson(newJson, JsonObject.class) : newJson;
        } else {
            oldObject = oldJson.getAsJsonObject();
            for (Map.Entry<String, JsonElement> entry : newJson.getAsJsonObject().entrySet()) {
                newDataElement = merge(oldObject.get(entry.getKey()), entry.getValue(), useCopy, gson);
                oldObject.add(entry.getKey(), newDataElement);
            }
        }
    } else {
        return useCopy ? gson.fromJson(newJson, JsonElement.class) : newJson;
    }

    return oldJson;
}

From source file:com.flipkart.android.proteus.toolbox.Utils.java

License:Apache License

public static String getPropertyAsString(JsonObject object, String property) {
    if (object == null || object.isJsonNull()) {
        return null;
    }//  w w  w.j a va2 s. c o m
    JsonElement element = object.get(property);
    if (element == null) {
        return null;
    }
    String string;
    if (!element.isJsonNull() && element.isJsonPrimitive()) {
        string = element.getAsString();
    } else {
        string = element.toString();
    }
    return string;
}