Example usage for com.google.gson JsonElement isJsonObject

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

Introduction

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

Prototype

public boolean isJsonObject() 

Source Link

Document

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

Usage

From source file:com.flipkart.android.proteus.parser.custom.ProgressBarParser.java

License:Apache License

@Override
protected void prepareHandlers() {
    super.prepareHandlers();
    addHandler(Attributes.ProgressBar.Max, new StringAttributeProcessor<T>() {
        @Override//w  w w.  java 2s. c  om
        public void handle(String attributeKey, String attributeValue, T view) {
            view.setMax((int) ParseHelper.parseDouble(attributeValue));
        }
    });
    addHandler(Attributes.ProgressBar.Progress, new StringAttributeProcessor<T>() {
        @Override
        public void handle(String attributeKey, String attributeValue, T view) {
            view.setProgress((int) ParseHelper.parseDouble(attributeValue));
        }
    });

    addHandler(Attributes.ProgressBar.ProgressTint, new JsonDataProcessor<T>() {
        @Override
        public void handle(String key, JsonElement valueElement, T view) {
            if (!valueElement.isJsonObject() || valueElement.isJsonNull()) {
                return;
            }
            JsonObject data = valueElement.getAsJsonObject();
            int background = Color.TRANSPARENT;
            int progress = Color.TRANSPARENT;

            String value = Utils.getPropertyAsString(data, "background");
            if (value != null) {
                background = ParseHelper.parseColor(value);
            }
            value = Utils.getPropertyAsString(data, "progress");
            if (value != null) {
                progress = ParseHelper.parseColor(value);
            }

            view.setProgressDrawable(getLayerDrawable(progress, background));
        }
    });

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        addHandler(Attributes.ProgressBar.SecondaryProgressTint, new ColorResourceProcessor<T>() {
            @Override
            public void setColor(T view, int color) {

            }

            @Override
            public void setColor(T view, ColorStateList colors) {
                //noinspection AndroidLintNewApi
                view.setSecondaryProgressTintList(colors);
            }
        });
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        addHandler(Attributes.ProgressBar.IndeterminateTint, new ColorResourceProcessor<T>() {
            @Override
            public void setColor(T view, int color) {

            }

            @Override
            public void setColor(T view, ColorStateList colors) {
                //noinspection AndroidLintNewApi
                view.setIndeterminateTintList(colors);
            }
        });
    }
}

From source file:com.flipkart.android.proteus.parser.custom.ViewGroupParser.java

License:Apache License

@Override
public boolean handleChildren(ProteusView view) {
    ProteusViewManager viewManager = view.getViewManager();
    LayoutBuilder builder = viewManager.getLayoutBuilder();
    JsonObject layout = viewManager.getLayout();
    JsonElement children = layout.get(ProteusConstants.CHILDREN);
    JsonObject data = viewManager.getDataContext().getData();
    int dataIndex = viewManager.getDataContext().getIndex();
    Styles styles = view.getViewManager().getStyles();

    if (null != children && !children.isJsonNull()) {
        if (children.isJsonArray()) {
            ProteusView child;//from  ww w.j a  v  a  2 s.co  m
            for (JsonElement jsonElement : children.getAsJsonArray()) {
                child = builder.build((ViewGroup) view, jsonElement.getAsJsonObject(), data, dataIndex, styles);
                addView(view, child);
            }
        } else if (children.isJsonObject()) {
            handleDataDrivenChildren(builder, view, viewManager, children.getAsJsonObject(), data, styles,
                    dataIndex);
        }
    }

    return true;
}

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   ww w .  ja  va  2  s  .com
        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.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  a2 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.com*/
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 ww .  j  a va 2s  . com
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;/*  ww  w.  j a  v  a 2 s  . c  om*/
    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 {/*  ww w. java2  s. 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  .  jav a 2  s. c om*/
    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 Drawable getBorderDrawable(JsonElement attributeValue, Context context) {

    if (!attributeValue.isJsonObject() || attributeValue.isJsonNull()) {
        return null;
    }// w w  w .ja va2s  .co m

    float cornerRadius = 0;
    int borderWidth = 0, borderColor = Color.TRANSPARENT, bgColor = Color.TRANSPARENT;
    JsonObject data = attributeValue.getAsJsonObject();

    String value = Utils.getPropertyAsString(data, ATTRIBUTE_BG_COLOR);
    if (value != null && !value.equals("-1")) {
        bgColor = ParseHelper.parseColor(value);
    }

    value = Utils.getPropertyAsString(data, ATTRIBUTE_BORDER_COLOR);
    if (value != null) {
        borderColor = ParseHelper.parseColor(value);
    }

    value = Utils.getPropertyAsString(data, ATTRIBUTE_BORDER_RADIUS);
    if (value != null) {
        cornerRadius = ParseHelper.parseDimension(value, context);
    }

    value = Utils.getPropertyAsString(data, ATTRIBUTE_BORDER_WIDTH);
    if (value != null) {
        borderWidth = (int) ParseHelper.parseDimension(value, context);
    }

    GradientDrawable border = new GradientDrawable();
    border.setCornerRadius(cornerRadius);
    border.setShape(GradientDrawable.RECTANGLE);
    border.setStroke(borderWidth, borderColor);
    border.setColor(bgColor);

    return border;
}