Example usage for com.google.gson JsonNull INSTANCE

List of usage examples for com.google.gson JsonNull INSTANCE

Introduction

In this page you can find the example usage for com.google.gson JsonNull INSTANCE.

Prototype

JsonNull INSTANCE

To view the source code for com.google.gson JsonNull INSTANCE.

Click Source Link

Document

singleton for JsonNull

Usage

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  ww  . java2 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.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);
    }/*  w  w w  .  j av a 2s. co m*/
    return transform;
}

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

License:Open Source License

/**
 * Transform./* www .  j a  v a2s  .  c  om*/
 *
 * @param source the source
 * @param specName the spec name
 * @param deps the deps
 * @param aces the aces
 * @return the json element
 */
public JsonElement transform(JsonElement source, String specName, JsonObject deps, JsonObject aces) {
    JsonElement transform = JsonNull.INSTANCE;
    Map<String, List<Tarot>> map = mystiques.get(specName);
    deps = jsonLever.asJsonObject(deps, new JsonObject());
    if (null != map) {
        List<Tarot> depList = map.get(MystiqueConstants.DEPS);
        updateDependencies(source, depList, deps);
        List<Tarot> tarotList = map.get(MystiqueConstants.TAROTS);
        transform = transform(source, tarotList, deps, aces);
    }

    if (jsonLever.isNull(transform)) {
        log.info(String.format("Transformed value for spec %s is null", specName));
    }
    return transform;
}

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

License:Open Source License

/**
 * Transform.//w w w.j  a  va 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.//w w  w .  j a v a 2  s .com
 *
 * @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.LoopySpell.java

License:Open Source License

@Override
public JsonElement cast(MystTurn mystique) {
    JsonElement transform = JsonNull.INSTANCE;
    if (CollectionUtils.isNotEmpty(source)) {
        transform = new JsonArray();
        List<CompletableFuture<JsonElement>> cfs = new ArrayList<>();
        JsonArray jsonArray = source.get(0).getAsJsonArray();
        for (JsonElement jsonElement : jsonArray) {

            CompletableFuture<JsonElement> transformElement = CompletableFuture.supplyAsync(() -> mystique
                    .transform(Lists.newArrayList(jsonElement), dependencies, aces, turn, resultWrapper))
                    .exceptionally(e -> {
                        String msg = String.format(
                                "Error transforming one of the elements in the array for %s - %s", turn,
                                e.getMessage());
                        logger.info(msg, e);
                        return JsonNull.INSTANCE;
                    });// w  ww.  ja va2s .c o m
            cfs.add(transformElement);
        }

        for (CompletableFuture<JsonElement> completableFuture : cfs) {
            JsonElement element = null;
            try {
                element = completableFuture.get();
            } catch (InterruptedException | ExecutionException e) {
                String msg = String.format(
                        "Error getting transformed element for one of the elements in the array for %s - %s",
                        turn, e.getMessage());
                logger.info(msg, e);
                element = JsonNull.INSTANCE;
            }
            Boolean optional = jsonLever.asBoolean(turn.get(MystiqueConstants.OPTIONAL), Boolean.FALSE);
            if (jsonLever.isNotNull(element) || !optional) {
                transform.getAsJsonArray().add(element);
            }
        }
    }
    return transform;
}

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

License:Open Source License

/**
 * Deserialize./*  w  w w  .  j  a  v  a 2 s . c o m*/
 *
 * @param node the node
 * @return the json element
 */
@SuppressWarnings("unchecked")
private T deserialize(JsonNode node) {
    JsonElement result = JsonNull.INSTANCE;
    if (null != node && !node.isNull()) {
        if (node.isObject()) {
            ObjectNode onode = (ObjectNode) node;
            JsonObject jsonObject = new JsonObject();
            Iterator<Entry<String, JsonNode>> fields = onode.fields();
            while (fields.hasNext()) {
                Entry<String, JsonNode> next = fields.next();
                jsonObject.add(next.getKey(), deserialize(next.getValue()));
            }
            result = jsonObject;
        } else if (node.isArray()) {
            ArrayNode anode = (ArrayNode) node;
            JsonArray jsonArray = new JsonArray();
            Iterator<JsonNode> elements = anode.elements();
            while (elements.hasNext()) {
                jsonArray.add(deserialize(elements.next()));
            }
            result = jsonArray;
        } else if (node.isBoolean()) {
            result = new JsonPrimitive(node.asBoolean());
        } else if (node.isNumber()) {
            NumericNode nnode = (NumericNode) node;
            result = new JsonPrimitive(nnode.numberValue());
        } else if (node.isTextual()) {
            TextNode tnode = (TextNode) node;
            result = new JsonPrimitive(tnode.textValue());
        }
    }

    return (T) result;
}

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

License:Open Source License

@Override
public JsonElement cast(MystTurn mystique) {
    return null != mystique ? mystique.transform(source, dependencies, aces, turn, resultWrapper)
            : JsonNull.INSTANCE;
}

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) {/*w ww .j  a  v  a  2 s. c o m*/

    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.core.TransformFunction.java

License:Open Source License

@Override
public JsonElement execute(JsonElement source, JsonObject turn) {
    JsonElement result = JsonNull.INSTANCE;
    if (jsonLever.isNotNull(source)) {
        turn = jsonLever.asJsonObject(turn, new JsonObject());
        String formatIn = jsonLever.asString(turn.get(MystiqueConstants.INFORMAT), MystiqueConstants.DEFAULT);
        Date inputDate = null;/*from ww w .ja v a 2s  .  c om*/
        try {
            switch (formatIn) {
            case MystiqueConstants.DEFAULT: {
                Long time = jsonLever.asLong(source);
                time = time == null ? Long.valueOf(jsonLever.asString(source)) : time;
                inputDate = new Date(time);
                break;
            }

            case MystiqueConstants.LONG: {
                Long time = jsonLever.asLong(source);
                inputDate = new Date(time);
                break;
            }

            case MystiqueConstants.STRING: {
                String time = jsonLever.asString(source);
                inputDate = new Date(Long.valueOf(time));
                break;
            }

            default:
                inputDate = new SimpleDateFormat(formatIn).parse(jsonLever.asString(source));
                break;
            }
        } catch (ParseException e) {
            logger.warn(String.format("Error while parsing input date %s for format %s : %s", source, formatIn,
                    e.getMessage()), e);
        }
        String outFormat = jsonLever.asString(turn.get(MystiqueConstants.OUTFORMAT), MystiqueConstants.LONG);
        result = getFormattedDate(inputDate, outFormat);
    }
    return result;
}