Example usage for com.google.gson JsonElement isJsonNull

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

Introduction

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

Prototype

public boolean isJsonNull() 

Source Link

Document

provides check for verifying if this element represents a null value or not.

Usage

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

License:Apache License

@Override
protected void prepareHandlers() {
    super.prepareHandlers();

    addHandler(Attributes.ProgressBar.ProgressTint, new JsonDataProcessor<T>() {
        @Override//from   w w w .  ja  v a2s .c om
        public void handle(String key, JsonElement attributeValue, T view) {
            if (!attributeValue.isJsonObject() || attributeValue.isJsonNull()) {
                return;
            }
            JsonObject data = attributeValue.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));
        }
    });
}

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// ww  w  .  j av  a  2  s .c  o m
        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  2s .c  om
            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.parser.custom.ViewGroupParser.java

License:Apache License

private void handleDataDrivenChildren(LayoutBuilder builder, ProteusView parent, ProteusViewManager viewManager,
        JsonObject children, JsonObject data, Styles styles, int dataIndex) {

    String dataPath = children.get(ProteusConstants.DATA).getAsString().substring(1);
    viewManager.setDataPathForChildren(dataPath);

    Result result = Utils.readJson(dataPath, data, dataIndex);
    JsonElement element = result.isSuccess() ? result.element : null;

    JsonObject childLayout = children.getAsJsonObject(ProteusConstants.LAYOUT);

    viewManager.setChildLayout(childLayout);

    if (null == element || element.isJsonNull()) {
        return;//from   w ww  .ja v  a2  s .  c  o m
    }

    int length = element.getAsJsonArray().size();

    ProteusView child;
    for (int index = 0; index < length; index++) {
        child = builder.build((ViewGroup) parent, childLayout, data, index, styles);
        if (child != null) {
            this.addView(parent, child);
        }
    }
}

From source file:com.flipkart.android.proteus.parser.ParseHelper.java

License:Apache License

public static int parseVisibility(JsonElement element) {
    Integer returnValue = null;/*from ww  w .  ja  v a 2 s . c  o  m*/
    if (element.isJsonPrimitive()) {
        String attributeValue = element.getAsString();
        returnValue = sVisibilityMode.get(attributeValue);
        if (null == returnValue && (attributeValue.isEmpty() || FALSE.equals(attributeValue)
                || ProteusConstants.DATA_NULL.equals(attributeValue))) {
            returnValue = View.GONE;
        }
    } else if (element.isJsonNull()) {
        returnValue = View.GONE;
    }
    return returnValue == null ? View.VISIBLE : returnValue;
}

From source file:com.flipkart.android.proteus.parser.ParseHelper.java

License:Apache License

public static int parseInvisibility(JsonElement element) {
    Integer returnValue = null;/*from ww  w.j a va  2 s  . c  om*/
    if (element.isJsonPrimitive()) {
        String attributeValue = element.getAsString();
        returnValue = sVisibilityMode.get(attributeValue);
        if (null == returnValue && (attributeValue.isEmpty() || FALSE.equals(attributeValue)
                || ProteusConstants.DATA_NULL.equals(attributeValue))) {
            returnValue = View.VISIBLE;
        }
    } else if (element.isJsonNull()) {
        returnValue = View.VISIBLE;
    }

    return returnValue == null ? View.GONE : returnValue;
}

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 {//  w  w  w .j av a 2s  . c om
        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;//ww w .j av 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 String getPropertyAsString(JsonObject object, String property) {
    if (object == null || object.isJsonNull()) {
        return null;
    }/*from  www .  j  ava2s  . 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;
}

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;
    }//from ww  w.  jav a  2 s  .  c  o  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;
}