Example usage for com.google.gson JsonElement getAsJsonObject

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

Introduction

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

Prototype

public JsonObject getAsJsonObject() 

Source Link

Document

convenience method to get this element as a JsonObject .

Usage

From source file:com.baidu.rigel.biplatform.ac.util.deserialize.MetaConditionDeserialize.java

License:Open Source License

@Override
public MetaCondition deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (json.isJsonObject()) {
        JsonObject conditionObj = json.getAsJsonObject();

        MetaType metaType = context.deserialize(conditionObj.get("metaType"), MetaType.class);
        if (metaType.equals(MetaType.Dimension)) {
            return context.deserialize(json, DimensionCondition.class);
        } else {//from  ww  w .  ja v a2  s .c o  m
            return context.deserialize(json, MeasureCondition.class);
        }
    }
    return null;
}

From source file:com.balajeetm.mystique.core.ChainMystTurn.java

License:Open Source License

@Override
public JsonElement transform(List<JsonElement> source, JsonObject deps, JsonObject aces, JsonObject turn,
        JsonObject resultWrapper) {// w w  w . j  a v a2  s  .c  om

    JsonElement transform = JsonNull.INSTANCE;
    turn = jsonLever.asJsonObject(turn, new JsonObject());
    JsonArray turnArray = jsonLever.asJsonArray(turn.get(MystiqueConstants.TURNS), new JsonArray());

    for (JsonElement turnObject : turnArray) {
        if (jsonLever.isObject(turnObject)) {
            JsonObject asJsonObject = turnObject.getAsJsonObject();
            MystTurn mystique = factory.getMystTurn(asJsonObject);
            if (null != mystique) {
                mystique.transform(source, deps, aces, asJsonObject, resultWrapper);
            }
        }
    }
    return transform;
}

From source file:com.balajeetm.mystique.core.lever.MystiqueLever.java

License:Open Source License

/**
 * Gets the subset.//from  ww  w  .  ja v  a2s . c om
 *
 * @param source the source
 * @param deps the deps
 * @param aces the aces
 * @param valueObject the value object
 * @return the subset
 */
public JsonElement subset(JsonElement source, JsonObject deps, JsonObject aces, JsonElement valueObject) {
    JsonElement finalValue = null;
    if (valueObject.isJsonArray()) {
        finalValue = subset(source, deps, aces, valueObject.getAsJsonArray());
    } else if (isString(valueObject)) {
        finalValue = subset(source, deps, aces, getJpath(valueObject));
    } else if (isObject(valueObject)) {
        // This is a turn
        JsonObject valueJson = valueObject.getAsJsonObject();
        MystTurn mystique = factory().getMystTurn(valueJson);
        finalValue = mystique.transform(Lists.newArrayList(source), deps, aces, valueJson, new JsonObject());
    }
    return finalValue;
}

From source file:com.balajeetm.mystique.core.lever.MystiqueLever.java

License:Open Source License

/**
 * Subset.//from w  w  w .  j  a  va  2  s . c  o m
 *
 * @param source the source
 * @param deps the deps
 * @param aces the aces
 * @param jPathArray the j path array
 * @return the json element
 */
private JsonElement subset(JsonElement source, JsonObject deps, JsonObject aces, JsonArray jPathArray) {
    JsonElement finalValue = null;
    if (jPathArray.size() == 0) {
        finalValue = getField(source, jPathArray, deps, aces);
    } else {
        JsonElement first = getFirst(jPathArray);
        if (isArray(first)) {
            finalValue = new JsonObject();
            for (JsonElement jsonElement : jPathArray) {
                JsonArray pathArray = asJsonArray(jsonElement);
                JsonElement subset = getField(source, pathArray, deps, aces);
                set(finalValue.getAsJsonObject(), pathArray, subset, aces);
            }
            finalValue = finalValue.getAsJsonObject().get(MystiqueConstants.RESULT);
        } else {
            finalValue = getField(source, jPathArray, deps, aces);
        }
    }
    return finalValue;
}

From source file:com.balajeetm.mystique.core.lever.MystiqueLever.java

License:Open Source License

/**
 * Sets the field of a json source./*from   w ww .ja v a2 s.  c om*/
 *
 * @param resultWrapper the json object that wraps the result json. The result is wrapped to
 *     ensure it passed across by reference and fields are updated appropriately
 * @param to the jPath json array defining the full qualified json path to the destination field
 *     where the value must be set
 * @param value the json value that needs to be set to the destination. This can be an Object,
 *     Array or a Primitive
 * @param aces the pre-processed dependency list in the form of key value pair (json)
 * @param optional the flag that determines if a null value must be set in the destination. If
 *     optional is TRUE, null values are not set in the destination
 * @return the json result wraper object which contains the result in the field called "result"
 */
public JsonObject set(JsonObject resultWrapper, JsonArray to, JsonElement value, JsonObject aces,
        Boolean optional) {

    /**
     * Holding a mutex on result wrapper and not making the method synchronized because, when
     * multiple unrelated threads might be calling mystique for transformation. The resource of
     * contention is only the result wrapper
     */
    synchronized (resultWrapper) {
        if (optional && isNull(value)) {
            // Do Not update result wrapper
            return resultWrapper;
        }
        if (isNotNull(to)) {
            JsonElement result = resultWrapper.get(MystiqueConstants.RESULT);
            JsonElement field = result;
            if (to.size() > 0) {
                JsonElement previousPath = null;
                JsonElement currentPath = null;
                Iterator<JsonElement> iterator = to.iterator();
                if (iterator.hasNext()) {
                    previousPath = getPathField(iterator.next(), aces);
                }

                while (iterator.hasNext()) {
                    currentPath = getPathField(iterator.next(), aces);

                    // get the field
                    field = getRepleteField(field, previousPath, currentPath);
                    result = updateResult(result, field);
                    field = isNumber(previousPath) ? field.getAsJsonArray().get(previousPath.getAsInt())
                            : field.getAsJsonObject().get(previousPath.getAsString());
                    previousPath = currentPath;
                }

                field = setField(field, previousPath, value);
                result = updateResult(result, field);
            } else {
                result = merge(result, value);
            }
            resultWrapper.add(MystiqueConstants.RESULT, result);
        }
        return resultWrapper;
    }
}

From source file:com.balajeetm.mystique.core.module.GsonSerialiser.java

License:Open Source License

@Override
public void serialize(JsonElement value, JsonGenerator gen, SerializerProvider provider) throws IOException {
    if (jsonLever.isNull(value)) {
        gen.writeNull();//from   ww w . ja v a  2 s  .  co m
    } else if (jsonLever.isObject(value)) {
        gen.writeStartObject();
        JsonObject jsonObject = value.getAsJsonObject();
        for (Entry<String, JsonElement> entry : jsonObject.entrySet()) {
            gen.writeFieldName(entry.getKey());
            serialize(entry.getValue(), gen, provider);
        }
        gen.writeEndObject();
    } else if (jsonLever.isArray(value)) {
        gen.writeStartArray();
        JsonArray jsonArray = value.getAsJsonArray();
        for (JsonElement jsonElement : jsonArray) {
            serialize(jsonElement, gen, provider);
        }
        gen.writeEndArray();
    } else if (jsonLever.isPrimitive(value)) {
        JsonPrimitive jsonPrimitive = value.getAsJsonPrimitive();
        if (jsonPrimitive.isBoolean()) {
            gen.writeBoolean(jsonPrimitive.getAsBoolean());
        }
        if (jsonPrimitive.isNumber()) {
            Number nnode = jsonPrimitive.getAsNumber();
            if (nnode instanceof LazilyParsedNumber) {
                gen.writeNumber(nnode.toString());
            } else if (nnode instanceof Integer) {
                gen.writeNumber(nnode.intValue());
            } else if (nnode instanceof Short) {
                gen.writeNumber(nnode.shortValue());
            } else if (nnode instanceof BigInteger || nnode instanceof Long) {
                gen.writeNumber(nnode.longValue());
            } else if (nnode instanceof Float) {
                gen.writeNumber(nnode.floatValue());
            } else if (nnode instanceof Double || nnode instanceof BigDecimal) {
                gen.writeNumber(nnode.doubleValue());
            }
        }
        if (jsonPrimitive.isString()) {
            gen.writeString(jsonPrimitive.getAsString());
        }
    }
}

From source file:com.balajeetm.mystique.core.ToggleMystTurn.java

License:Open Source License

@Override
public JsonElement transform(List<JsonElement> source, JsonObject deps, JsonObject aces, JsonObject turn,
        JsonObject resultWrapper) {/*from w  w w . j  a v  a  2  s  .c om*/

    JsonElement transform = JsonNull.INSTANCE;
    turn = jsonLever.asJsonObject(turn, new JsonObject());
    JsonArray turnArray = jsonLever.asJsonArray(turn.get(MystiqueConstants.TURNS), new JsonArray());
    for (JsonElement turnObject : turnArray) {
        if (jsonLever.isObject(turnObject)) {
            JsonObject asJsonObject = turnObject.getAsJsonObject();
            MystTurn mystique = factory.getMystTurn(asJsonObject);
            if (null != mystique) {
                transform = mystique.transform(source, deps, aces, asJsonObject, resultWrapper);
            }
            if (jsonLever.isNotNull(transform)) {
                break;
            }
        }
    }
    return transform;
}

From source file:com.balajeetm.mystique.util.gson.lever.JsonLever.java

License:Open Source License

/**
 * Gets the json element in the specified jpath.
 *
 * @param source the source/*from w w w. j av a  2 s  .com*/
 * @param jpath the fully qualified json path to the field required. eg get({'a': {'b': {'c': [1,
 *     2, 3, 4]}}}, ["a", "b" "c", 1]) is '2'. Array indexes need to be specified as numerals.
 *     Strings are always presumed to be field names.
 * @return the json element returns 'Null' for any invalid path return the source if the input
 *     jpath is '.'
 */
public JsonElement get(JsonElement source, JsonArray jpath) {
    JsonElement result = JsonNull.INSTANCE;
    if (isNotNull(jpath)) {
        result = source;
        for (JsonElement path : jpath) {
            try {
                if (isNumber(path)) {
                    result = result.getAsJsonArray().get(path.getAsInt());
                } else {
                    result = result.getAsJsonObject().get(path.getAsString());
                }
            } catch (Exception e) {
                return JsonNull.INSTANCE;
            }
        }
    }

    return result;
}

From source file:com.balajeetm.mystique.util.gson.lever.JsonLever.java

License:Open Source License

/**
 * Sets the json element at the specified jpath.
 *
 * @param source the source//from   w  w w . j  av a 2s.  c o  m
 * @param jpath the fully qualified json path to the field required. eg set({'a': {'b': {'c': [1,
 *     2, 3, 4]}}}, ["a", "b" "c", 1, 5]) is {'a': {'b': {'c': [1, 5, 3, 4]}}}. Array indexes need
 *     to be specified as numerals. Strings are always presumed to be field names.
 * @param value the value
 * @return the json element
 */
public JsonElement set(JsonElement source, JsonArray jpath, JsonElement value) {
    JsonElement result = JsonNull.INSTANCE;
    if (isNotNull(jpath)) {
        result = source;
        JsonElement field = result;
        if (jpath.size() > 0) {
            JsonElement previousPath = null;
            JsonElement currentPath = null;
            Iterator<JsonElement> iterator = jpath.iterator();
            if (iterator.hasNext()) {
                previousPath = iterator.next();
            }

            while (iterator.hasNext()) {
                currentPath = iterator.next();
                // get the field
                field = getRepleteField(field, previousPath, currentPath);
                result = updateResult(result, field);
                field = isNumber(previousPath) ? field.getAsJsonArray().get(previousPath.getAsInt())
                        : field.getAsJsonObject().get(previousPath.getAsString());
                previousPath = currentPath;
            }

            field = setField(field, previousPath, value);
        }
    }
    return result;
}

From source file:com.balajeetm.mystique.util.gson.lever.JsonLever.java

License:Open Source License

/**
 * Returns the source json as a json object if possible. Else returns the default json
 *
 * @param source the source json element
 * @param defaultJson the default json/* w  ww  . j a  v  a 2 s. co  m*/
 * @return the source json as a json object
 */
public JsonObject asJsonObject(JsonElement source, JsonObject defaultJson) {
    return isObject(source) ? source.getAsJsonObject() : defaultJson;
}