Example usage for com.google.gson JsonPrimitive JsonPrimitive

List of usage examples for com.google.gson JsonPrimitive JsonPrimitive

Introduction

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

Prototype

public JsonPrimitive(Character c) 

Source Link

Document

Create a primitive containing a character.

Usage

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 ww w  .ja  v 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.module.GsonDeserialiser.java

License:Open Source License

/**
 * Deserialize./*from  w  w  w .j  a  va2 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.StringUtilsMystTurn.java

License:Open Source License

/**
 * Execute.//from www. j a v  a2 s . c  o  m
 *
 * @param input the input
 * @param turn the turn
 * @return the json primitive
 */
private JsonPrimitive execute(String input, JsonObject turn) {
    JsonPrimitive transform = null;
    String action = mystiqueLever.asString(turn.get(MystiqueConstants.ACTION), MystiqueConstants.TRIM);
    switch (action) {
    case MystiqueConstants.TRIM:
        transform = new JsonPrimitive(StringUtils.trim(input));
        break;

    case MystiqueConstants.TRIM_TO_EMPTY:
        transform = new JsonPrimitive(StringUtils.trimToEmpty(input));
        break;

    case MystiqueConstants.SUBSTRING_AFTER_LAST:
        String separator = mystiqueLever.asString(turn.get(MystiqueConstants.SEPARATOR),
                MystiqueConstants.EMPTY);
        transform = new JsonPrimitive(StringUtils.substringAfterLast(input, separator));
        break;

    default:
        break;
    }
    return transform;
}

From source file:com.balajeetm.mystique.core.test.CustomMystTurn.java

License:Open Source License

@Override
protected JsonElement transmute(List<JsonElement> source, JsonObject deps, JsonObject aces, JsonObject turn) {
    StringBuilder stringBuilder = new StringBuilder();
    if (CollectionUtils.isNotEmpty(source)) {
        String separator = "~";
        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  w  w  .j a  v  a2s  . c  o  m*/
            stringBuilder.append(mystiqueLever.asString(granularSource, MystiqueConstants.EMPTY));
        }
    }
    return new JsonPrimitive(stringBuilder.toString());
}

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

License:Open Source License

/**
 * Gets the formatted date.//from w w  w.j  a  v  a2s . c o m
 *
 * @param date the date
 * @param dateFormat the date format
 * @return the formatted date
 */
public JsonElement getFormattedDate(Date date, String dateFormat) {
    JsonElement output;
    switch (dateFormat) {
    case MystiqueConstants.LONG:
        output = new JsonPrimitive(date.getTime());
        break;

    case MystiqueConstants.STRING:
        output = new JsonPrimitive(String.valueOf(date.getTime()));
        break;

    default:
        output = new JsonPrimitive(new SimpleDateFormat(dateFormat).format(date));
        break;
    }
    return output;
}

From source file:com.balajeetm.mystique.util.gson.GsonFactory.java

License:Open Source License

/**
 * Gets the gson builder.//from w  w  w .  java 2 s  .co  m
 *
 * @return the gson builder
 */
public GsonBuilder getGsonBuilder() {
    if (null == gsonBuilder) {
        gsonBuilder = new GsonBuilder();
        gsonBuilder.setDateFormat(DateFormat.LONG, DateFormat.LONG);
        gsonBuilder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {

            @Override
            public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                    throws JsonParseException {
                Date date = null;
                if (null != json && json.isJsonPrimitive()) {
                    date = new Date(json.getAsJsonPrimitive().getAsLong());
                }
                return date;
            }
        });
        gsonBuilder.registerTypeAdapter(XMLGregorianCalendar.class, new JsonSerializer<XMLGregorianCalendar>() {

            @Override
            public JsonElement serialize(XMLGregorianCalendar src, Type typeOfSrc,
                    JsonSerializationContext context) {
                Date date = null;
                if (null != src) {
                    date = src.toGregorianCalendar().getTime();
                }
                return new JsonPrimitive(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").format(date));
            }
        });
    }
    return gsonBuilder;
}

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

License:Open Source License

/**
 * Sets the string at the specified jpath.
 *
 * @param source the source//from  w w w .ja  v a 2 s .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]}}}. Strings that can be
 *     casted to numerals are presumed to be array indexes
 * @param value the value
 * @return the json element
 */
public JsonElement set(JsonElement source, String jpath, String value) {
    return set(source, jpath, new JsonPrimitive(value));
}

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

License:Open Source License

/**
 * Sets the string at the specified jpath.
 *
 * @param source the source//from   w  w w. j a v 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", true]) is {'a': {'b': {'c': [1, true, 3, 4]}}}. Strings that can be
 *     casted to numerals are presumed to be array indexes
 * @param value the value
 * @return the json element
 */
public JsonElement set(JsonElement source, String jpath, Boolean value) {
    return set(source, jpath, new JsonPrimitive(value));
}

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

License:Open Source License

/**
 * Sets the string at the specified jpath.
 *
 * @param source the source//from www.  j a v a 2 s  . 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]}}}. Strings that can be
 *     casted to numerals are presumed to be array indexes
 * @param value the value
 * @return the json element
 */
public JsonElement set(JsonElement source, String jpath, Number value) {
    return set(source, jpath, new JsonPrimitive(value));
}

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

License:Open Source License

/**
 * Sets the string at the specified jpath.
 *
 * @param source the source/*from   www  .ja va  2s .  c om*/
 * @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]}}}. Strings that can be
 *     casted to numerals are presumed to be array indexes
 * @param value the value
 * @return the json element
 */
public JsonElement set(JsonElement source, JsonArray jpath, String value) {
    return set(source, jpath, new JsonPrimitive(value));
}