Example usage for com.google.gson JsonElement getAsString

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

Introduction

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

Prototype

public String getAsString() 

Source Link

Document

convenience method to get this element as a string value.

Usage

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

License:Apache License

public static int parseInvisibility(JsonElement element) {
    Integer returnValue = null;//from   w ww  .  j a v a  2  s .co 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.VISIBLE;
        }
    } else if (element.isJsonNull()) {
        returnValue = View.VISIBLE;
    }

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

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

License:Apache License

public static Pair<int[], JsonElement> parseState(JsonObject stateObject) {

    //drawable/*from   www.j  a va2s .  c  om*/
    JsonElement drawableJson = stateObject.get(DRAWABLE_STR);
    if (null != drawableJson) {

        //states
        Set<Map.Entry<String, JsonElement>> entries = stateObject.entrySet();
        List<Integer> statesToReturn = new ArrayList<>();
        for (Map.Entry<String, JsonElement> entry : entries) {
            JsonElement value = entry.getValue();
            String state = entry.getKey();
            Integer stateInteger = sStateMap.get(state);
            if (stateInteger != null) {
                String stateValue = value.getAsString();
                //e.g state_pressed = true state_pressed = false
                statesToReturn.add(ParseHelper.parseBoolean(stateValue) ? stateInteger : -stateInteger);
            }
        }

        int[] statesToReturnInteger = new int[statesToReturn.size()];
        for (int i = 0; i < statesToReturn.size(); i++) {
            statesToReturnInteger[i] = statesToReturn.get(i);
        }

        return new Pair<>(statesToReturnInteger, drawableJson);
    }
    return null;
}

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

License:Apache License

/**
 * Parses a single layer item (represented by {@param child}) inside a layer list and gives
 * a pair of android:id and a string for the drawable path.
 *
 * @param child/*from ww  w  .  ja v a2s .c o  m*/
 * @return The layer info as a {@link Pair}
 */
public static Pair<Integer, JsonElement> parseLayer(JsonObject child) {

    JsonElement id = child.get(ID_STR);
    int androidResIdByXmlResId = View.NO_ID;
    String idAsString = null;
    if (id != null) {
        idAsString = id.getAsString();
    }
    if (idAsString != null) {
        androidResIdByXmlResId = getAndroidResIdByXmlResId(idAsString);
    }
    JsonElement drawableElement = child.get(DRAWABLE_STR);
    return (drawableElement != null) ? new Pair<>(androidResIdByXmlResId, drawableElement) : null;
}

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

License:Apache License

/**
 * @param view View/*from w ww .ja  v a  2  s .co 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   w  ww .j  ava 2 s  .  c  o m*/
        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.DrawableResourceProcessor.java

License:Apache License

/**
 * This block handles different drawables.
 * Selector and LayerListDrawable are handled here.
 * Override this to handle more types of drawables
 *
 * @param attributeKey//  w  w w.  j a va2  s.c  o m
 * @param attributeValue
 * @param view
 */
protected void handleElement(String attributeKey, JsonElement attributeValue, V view) {

    JsonObject jsonObject = attributeValue.getAsJsonObject();

    JsonElement type = jsonObject.get(TYPE);
    String drawableType = type.getAsString();
    JsonElement childrenElement = null;
    switch (drawableType) {
    case DRAWABLE_SELECTOR:
        final StateListDrawable stateListDrawable = new StateListDrawable();
        childrenElement = jsonObject.get(CHILDREN);
        if (childrenElement != null) {
            JsonArray children = childrenElement.getAsJsonArray();
            for (JsonElement childElement : children) {
                JsonObject child = childElement.getAsJsonObject();
                final Pair<int[], JsonElement> state = ParseHelper.parseState(child);
                if (state != null) {
                    DrawableResourceProcessor<V> processor = new DrawableResourceProcessor<V>() {
                        @Override
                        public void setDrawable(V view, Drawable drawable) {
                            stateListDrawable.addState(state.first, drawable);
                        }
                    };
                    processor.handle(attributeKey, state.second, view);
                }

            }
        }
        setDrawable(view, stateListDrawable);
        break;
    case DRAWABLE_SHAPE:
        GradientDrawable gradientDrawable = loadGradientDrawable(view.getContext(), jsonObject);
        if (null != gradientDrawable) {
            setDrawable(view, gradientDrawable);
        }
        break;
    case DRAWABLE_LAYER_LIST:
        final List<Pair<Integer, Drawable>> drawables = new ArrayList<>();
        childrenElement = jsonObject.get(CHILDREN);
        if (childrenElement != null) {
            JsonArray children = childrenElement.getAsJsonArray();
            for (JsonElement childElement : children) {
                JsonObject child = childElement.getAsJsonObject();
                final Pair<Integer, JsonElement> layerPair = ParseHelper.parseLayer(child);
                if (null != layerPair) {
                    DrawableResourceProcessor<V> processor = new DrawableResourceProcessor<V>() {
                        @Override
                        public void setDrawable(V view, Drawable drawable) {
                            drawables.add(new Pair<>(layerPair.first, drawable));
                            onLayerDrawableFinish(view, drawables);
                        }
                    };
                    processor.handle(attributeKey, layerPair.second, view);
                }
            }
        }
        break;
    case DRAWABLE_LEVEL_LIST:
        final LevelListDrawable levelListDrawable = new LevelListDrawable();
        childrenElement = jsonObject.get(CHILDREN);
        if (childrenElement != null) {
            JsonArray children = childrenElement.getAsJsonArray();
            for (JsonElement childElement : children) {
                LayerListDrawableItem layerListDrawableItem = sGson.fromJson(childElement,
                        LayerListDrawableItem.class);
                layerListDrawableItem.addItem(view.getContext(), levelListDrawable);
            }
        }
        break;
    case DRAWABLE_RIPPLE:
        Drawable rippleDrawable = loadRippleDrawable(view.getContext(), jsonObject, attributeKey, view);
        if (null != rippleDrawable) {
            setDrawable(view, rippleDrawable);
        }
        break;
    }
}

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

License:Apache License

/**
 * @param view View//from   ww  w .j av a2  s. c o  m
 */
@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  ww .  j a va2  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

private static Animation handleElement(Context c, JsonObject value) {
    Animation anim = null;//from   w  w  w.  j ava 2 s .c  o m
    JsonElement type = value.get(TYPE);
    String animationType = type.getAsString();
    AnimationProperties animationProperties = null;
    if (SET.equalsIgnoreCase(animationType)) {
        animationProperties = sGson.fromJson(value, AnimationSetProperties.class);
    } else if (ALPHA.equalsIgnoreCase(animationType)) {
        animationProperties = sGson.fromJson(value, AlphaAnimProperties.class);
    } else if (SCALE.equalsIgnoreCase(animationType)) {
        animationProperties = sGson.fromJson(value, ScaleAnimProperties.class);
    } else if (ROTATE.equalsIgnoreCase(animationType)) {
        animationProperties = sGson.fromJson(value, RotateAnimProperties.class);
    } else if (TRANSLATE.equalsIgnoreCase(animationType)) {
        animationProperties = sGson.fromJson(value, TranslateAnimProperties.class);
    }

    if (null != animationProperties) {
        anim = animationProperties.instantiate(c);
    }

    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   w  w w.ja va  2 s .co m*/
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;
}