Example usage for com.google.gson JsonObject get

List of usage examples for com.google.gson JsonObject get

Introduction

In this page you can find the example usage for com.google.gson JsonObject get.

Prototype

public JsonElement get(String memberName) 

Source Link

Document

Returns the member with the specified name.

Usage

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

License:Open Source License

@Override
public JsonElement transmute(List<JsonElement> source, JsonObject deps, JsonObject aces, JsonObject turn) {
    StringBuilder stringBuilder = new StringBuilder();
    if (CollectionUtils.isNotEmpty(source)) {
        String separator = mystiqueLever.asString(turn.get(MystiqueConstants.SEPARATOR),
                MystiqueConstants.EMPTY);
        for (int count = 0; count < source.size(); count++) {
            JsonElement granularSource = getGranularSource(source.get(count), turn, deps, aces);
            if (count != 0) {
                stringBuilder.append(separator);
            }//from   w  ww. ja  v  a2s  .  co m
            stringBuilder.append(StringUtils.strip(granularSource.toString(), MystiqueConstants.DOUBLE_QUOTES));
        }
    }
    return new JsonPrimitive(stringBuilder.toString());
}

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

License:Open Source License

@Override
protected JsonElement transmute(List<JsonElement> source, JsonObject deps, JsonObject aces, JsonObject turn) {
    JsonElement transform = null;/*from w  w  w .j av a 2s .c  o m*/
    JsonElement elementSource = mystiqueLever.getFirst(source);
    turn = mystiqueLever.asJsonObject(turn, new JsonObject());
    JsonElement granularSource = getGranularSource(elementSource, turn, deps, aces);
    JsonElement value = turn.get(MystiqueConstants.VALUE);
    Boolean equals = isEquals(granularSource, value);
    JsonObject defaultJson = mystiqueLever.asJsonObject(turn.get(String.valueOf(equals)));
    transform = mystiqueLever.isNull(defaultJson) ? new JsonPrimitive(equals)
            : transformToDefault(defaultJson, source, deps, aces);
    return transform;
}

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

License:Open Source License

@Override
public JsonElement transmute(List<JsonElement> source, JsonObject deps, JsonObject aces, JsonObject turn) {
    turn = mystiqueLever.asJsonObject(turn, new JsonObject());
    return turn.get(MystiqueConstants.VALUE);
}

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

License:Open Source License

/**
 * Gets the subset./*from w  w w .  ja  v a  2 s.  co m*/
 *
 * @param source the source
 * @param deps the deps
 * @param aces the aces
 * @param turn the turn
 * @return the subset
 */
private JsonElement getSubset(JsonElement source, JsonObject deps, JsonObject aces, JsonObject turn) {
    JsonElement value = null != turn ? turn.get(MystiqueConstants.VALUE) : null;
    value = mystiqueLever.isNull(value) ? null : value;
    return null != value ? mystiqueLever.subset(source, deps, aces, value) : source;
}

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

License:Open Source License

@Override
protected JsonElement transmute(List<JsonElement> source, JsonObject deps, JsonObject aces, JsonObject turn) {
    JsonElement elementSource = mystiqueLever.getFirst(source);
    turn = mystiqueLever.asJsonObject(turn, new JsonObject());
    JsonElement granularSource = getGranularSource(elementSource, turn, deps, aces);

    String action = StringUtils//from ww w .  ja  v a  2 s  .  c  om
            .trimToEmpty(mystiqueLever.asString(turn.get(MystiqueConstants.ACTION), MystiqueConstants.NOW))
            .toLowerCase();
    MystiqueFunction function = factory.getDateFunction(action);
    return function.execute(granularSource, turn);
}

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

License:Open Source License

@Override
protected JsonElement transmute(List<JsonElement> source, JsonObject deps, JsonObject aces, JsonObject turn) {

    JsonElement transform = JsonNull.INSTANCE;
    JsonElement elementSource = mystiqueLever.getFirst(source);
    if (null != elementSource) {
        // element source is key

        turn = mystiqueLever.asJsonObject(turn, new JsonObject());
        JsonElement granularSource = getGranularSource(elementSource, turn, deps, aces);
        JsonArray reference = mystiqueLever.getJpath(granularSource);

        /* Default is needed since the key can turn out to be null */
        JsonArray keyPath = mystiqueLever.asJsonArray(mystiqueLever.getJpath(turn.get(MystiqueConstants.KEY)),
                new JsonArray());
        JsonElement value = turn.get(MystiqueConstants.VALUE);
        value = mystiqueLever.isNull(value) ? new JsonArray() : value;

        // keymap - The source is deps
        JsonElement keyMap = mystiqueLever.getField(deps, keyPath, deps, aces);

        JsonElement actualElement = mystiqueLever.get(keyMap, reference);

        transform = mystiqueLever.subset(actualElement, deps, aces, value);
    }/*from   w  w w.j a va2 s.  co m*/
    return transform;
}

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

License:Open Source License

/**
 * Transform.//w w w.  java  2 s  . c o m
 *
 * @param source the source
 * @param tarotList the tarot list
 * @param dependencies the dependencies
 * @param parentAces the parent aces
 * @return the json element
 */
private JsonElement transform(JsonElement source, List<Tarot> tarotList, JsonObject dependencies,
        JsonObject parentAces) {
    JsonObject resultWrapper = new JsonObject();
    resultWrapper.add(MystiqueConstants.RESULT, JsonNull.INSTANCE);
    if (CollectionUtils.isNotEmpty(tarotList)) {
        List<CompletableFuture<JsonObject>> cfs = new ArrayList<>();
        for (Tarot tarot : tarotList) {
            JsonObject turn = tarot.getTurn();

            CompletableFuture<JsonObject> getAces = CompletableFuture.supplyAsync(() -> {
                JsonObject aces = tarot.getAces();
                JsonObject updatedAces = jsonLever.getUpdatedAces(source, aces, dependencies,
                        jsonLever.deepClone(aces));
                jsonLever.simpleMerge(updatedAces, parentAces);
                return updatedAces;
            }).exceptionally(e -> {
                String msg = String.format("Error updating aces for turn %s - %s", turn, e.getMessage());
                log.info(msg, e);
                return parentAces;
            });

            CompletableFuture<JsonElement> transformAsync = getAces.thenApplyAsync((aces) -> {
                JsonElement transform = JsonNull.INSTANCE;
                Spell spell = getSpell(source, jsonLever.getJpath(tarot.getFrom()), dependencies, aces, turn,
                        resultWrapper);
                MystTurn mystique = factory.getMystTurn(turn);
                transform = spell.cast(mystique);
                return transform;
            }).exceptionally((e) -> {
                String msg = String.format("Error transforming input with specification for turn %s - %s", turn,
                        e.getMessage());
                log.info(msg, e);
                return JsonNull.INSTANCE;
            });

            CompletableFuture<JsonObject> setResult = getAces
                    .thenCombine(transformAsync, (aces, transform) -> jsonLever.set(resultWrapper,
                            jsonLever.getJpath(tarot.getTo()), transform, aces, tarot.getOptional()))
                    .exceptionally(e -> {
                        String msg = String.format("Error setting output for turn %s - %s", turn,
                                e.getMessage());
                        log.info(msg, e);
                        return resultWrapper;
                    });

            cfs.add(setResult);
        }

        for (CompletableFuture<JsonObject> completableFuture : cfs) {
            completableFuture.join();
        }
    } else {
        log.info(String.format("Invalid tarots. Tarots cannot be empty"));
    }
    return resultWrapper.get(MystiqueConstants.RESULT);
}

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

License:Open Source License

/**
 * Update fields./*from w w w . j av  a2 s .co m*/
 *
 * @param source the source
 * @param dependencies the dependencies
 * @param aces the aces
 * @param fields the fields
 * @param path the path
 * @return the boolean
 */
public Boolean updateFields(JsonElement source, JsonObject dependencies, JsonObject aces,
        List<JsonElement> fields, JsonArray path) {
    Boolean isLoopy = Boolean.FALSE;
    try {
        JsonElement field = source;
        if (null != path) {
            if (path.size() > 0) {
                try {
                    for (int count = 0; count < path.size(); count++) {
                        JsonElement p = path.get(count);
                        if (isNumber(p)) {
                            field = get(field, p);
                        } else {
                            String key = asString(p);
                            if (count == 0 && MystiqueConstants.AT_DEPS.equals(key)) {
                                field = dependencies;
                                continue;
                            }
                            String ace = getAce(key);
                            if (null != ace) {
                                field = aces.get(ace);
                                continue;
                            }
                            if (isLoopy(key)) {
                                isLoopy = Boolean.TRUE;
                                fields.clear();
                                break;
                            } else {
                                p = getPathField(p, aces);
                                field = get(field, p);
                            }
                        }
                    }
                } catch (IllegalStateException e) {
                    log.info(String.format("Invalid json path %s for %s : %s", path, source, e.getMessage()),
                            e);
                    field = JsonNull.INSTANCE;
                }
            }
            fields.add(field);
        }
    }
    /** Would throw an exception for any invalid path, which is logged and ignored */
    catch (RuntimeException e) {
        log.warn(String.format("Error getting field from source for %s - %s. Skipping the same", path,
                e.getMessage()), e);
    }
    return isLoopy;
}

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

License:Open Source License

/**
 * Gets the field.//from w  w w  .  j a v  a  2  s . com
 *
 * @param source the source
 * @param jPath the j path
 * @param deps the deps
 * @param aces the aces
 * @return the field
 */
public JsonElement getField(JsonElement source, JsonArray jPath, JsonObject deps, JsonObject aces) {
    JsonElement field = null;
    try {
        field = source;
        if (null != jPath) {
            for (int count = 0; count < jPath.size(); count++) {
                JsonElement path = jPath.get(count);
                if (isNumber(path)) {
                    field = get(field, path);
                } else {
                    String key = asString(path);
                    if (count == 0 && MystiqueConstants.AT_DEPS.equals(key)) {
                        field = deps;
                        continue;
                    }
                    String ace = getAce(key);
                    if (null != ace) {
                        field = aces.get(ace);
                        continue;
                    }
                    path = getPathField(path, aces);
                    field = get(field, path);
                }
            }
        }
    }
    /** Would throw an exception for any invalid path, which is logged and ignored */
    catch (RuntimeException e) {
        log.warn(String.format("Error getting field from source for %s - %s. Skipping the same", jPath,
                e.getMessage()), e);
        field = null;
    }
    return field;
}

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

License:Open Source License

/**
 * Sets the field of a json source./*from  w  w  w .  j a  va 2s. 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;
    }
}