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.fatboyindustrial.gsonjodatime.LocalDateTimeConverter.java

License:Open Source License

/**
 * Gson invokes this call-back method during serialization when it encounters a field of the
 * specified type. <p>/*  www .  j a v a2 s  . c  o m*/
 *
 * In the implementation of this call-back method, you should consider invoking
 * {@link JsonSerializationContext#serialize(Object, Type)} method to create JsonElements for any
 * non-trivial field of the {@code src} object. However, you should never invoke it on the
 * {@code src} object itself since that will cause an infinite loop (Gson will call your
 * call-back method again).
 *
 * @param src the object that needs to be converted to Json.
 * @param typeOfSrc the actual type (fully genericized version) of the source object.
 * @return a JsonElement corresponding to the specified object.
 */
@Override
public JsonElement serialize(LocalDateTime src, Type typeOfSrc, JsonSerializationContext context) {
    final DateTimeFormatter fmt = DateTimeFormat.forPattern(PATTERN);
    return new JsonPrimitive(fmt.print(src));
}

From source file:com.fatboyindustrial.gsonjodatime.LocalTimeConverter.java

License:Open Source License

/**
 * Gson invokes this call-back method during serialization when it encounters a field of the
 * specified type. <p>//from  w w  w. ja  v a 2 s  .  c  om
 *
 * In the implementation of this call-back method, you should consider invoking
 * {@link JsonSerializationContext#serialize(Object, Type)} method to create JsonElements for any
 * non-trivial field of the {@code src} object. However, you should never invoke it on the
 * {@code src} object itself since that will cause an infinite loop (Gson will call your
 * call-back method again).
 *
 * @param src the object that needs to be converted to Json.
 * @param typeOfSrc the actual type (fully genericized version) of the source object.
 * @return a JsonElement corresponding to the specified object.
 */
@Override
public JsonElement serialize(LocalTime src, Type typeOfSrc, JsonSerializationContext context) {
    final DateTimeFormatter fmt = DateTimeFormat.forPattern(PATTERN);
    return new JsonPrimitive(fmt.print(src));
}

From source file:com.fatboyindustrial.gsonjodatime.PeriodConverter.java

License:Open Source License

/**
 * Gson invokes this call-back method during serialization when it encounters a field of the
 * specified type. <p>/* www  . j  a  v  a  2 s .c  o m*/
 *
 * In the implementation of this call-back method, you should consider invoking
 * {@link JsonSerializationContext#serialize(Object, Type)} method to create JsonElements for any
 * non-trivial field of the {@code src} object. However, you should never invoke it on the
 * {@code src} object itself since that will cause an infinite loop (Gson will call your
 * call-back method again).
 * @param src the object that needs to be converted to Json.
 * @param typeOfSrc the actual type (fully genericized version) of the source object.
 * @return a JsonElement corresponding to the specified object.
 */
@Override
public JsonElement serialize(Period src, Type typeOfSrc, JsonSerializationContext context) {
    final PeriodFormatter fmt = ISOPeriodFormat.standard();
    return new JsonPrimitive(fmt.print(src));
}

From source file:com.flipkart.android.proteus.builder.DataParsingLayoutBuilder.java

License:Apache License

private JsonElement findAndReplaceValues(LayoutHandler handler, ProteusView view, String attribute,
        String stringValue, JsonElement value) {
    ProteusViewManager viewManager = view.getViewManager();
    DataContext dataContext = viewManager.getDataContext();

    if (ProteusConstants.isLoggingEnabled()) {
        Log.d(TAG, "Find '" + stringValue + "' for " + attribute + " for view with "
                + Utils.getLayoutIdentifier(viewManager.getLayout()));
    }//from w  w  w.j  ava2  s .  c  o m

    char firstChar = TextUtils.isEmpty(stringValue) ? 0 : stringValue.charAt(0);
    String dataPath;
    Result result;
    switch (firstChar) {
    case ProteusConstants.DATA_PREFIX:
        JsonElement elementFromData;
        dataPath = stringValue.substring(1);
        result = Utils.readJson(dataPath, viewManager.getDataContext().getData(),
                viewManager.getDataContext().getIndex());

        if (result.isSuccess()) {
            elementFromData = result.element;
        } else {
            elementFromData = new JsonPrimitive(ProteusConstants.DATA_NULL);
        }

        if (elementFromData != null) {
            value = elementFromData;
        }
        addBinding(viewManager, dataPath, attribute, stringValue, false);
        break;
    case ProteusConstants.REGEX_PREFIX:
        Matcher regexMatcher = ProteusConstants.REGEX_PATTERN.matcher(stringValue);
        String finalValue = stringValue;
        while (regexMatcher.find()) {
            String matchedString = regexMatcher.group(0);
            String bindingName;
            if (regexMatcher.group(3) != null) {

                // has NO formatter
                dataPath = regexMatcher.group(3);
                result = Utils.readJson(dataPath, viewManager.getDataContext().getData(),
                        viewManager.getDataContext().getIndex());
                if (result.isSuccess() && null != result.element) {
                    finalValue = finalValue.replace(matchedString, result.element.getAsString());
                } else {
                    finalValue = dataPath;
                }
                bindingName = dataPath;
            } else {

                // has formatter
                dataPath = regexMatcher.group(1);
                String formatterName = regexMatcher.group(2);

                String formattedValue;
                result = Utils.readJson(dataPath, viewManager.getDataContext().getData(),
                        viewManager.getDataContext().getIndex());
                if (result.isSuccess() && null != result.element) {
                    formattedValue = format(result.element, formatterName);
                } else {
                    formattedValue = dataPath;
                }

                finalValue = finalValue.replace(matchedString, formattedValue != null ? formattedValue : "");
                bindingName = dataPath;
            }
            addBinding(viewManager, bindingName, attribute, stringValue, true);
        }

        // remove the REGEX_PREFIX
        finalValue = finalValue.substring(1);

        // return as a JsonPrimitive
        value = new JsonPrimitive(finalValue);

        break;
    }

    return value;
}

From source file:com.flipkart.android.proteus.DataContext.java

License:Apache License

public static DataContext updateDataContext(DataContext dataContext, JsonObject data, JsonObject scope,
        int dataIndex) {
    JsonObject reverseScope = new JsonObject();
    JsonObject newData = new JsonObject();

    dataContext.setIndex(dataIndex);/*from   w  ww.jav  a 2 s .c  o  m*/

    if (data == null) {
        data = new JsonObject();
    }

    for (Map.Entry<String, JsonElement> entry : scope.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue().getAsString();
        Result result = Utils.readJson(value, data, dataContext.getIndex());
        JsonElement element = result.isSuccess() ? result.element : new JsonObject();
        newData.add(key, element);
        String unAliasedValue = value.replace(ProteusConstants.INDEX, String.valueOf(dataContext.getIndex()));
        reverseScope.add(unAliasedValue, new JsonPrimitive(key));
    }

    Utils.addElements(newData, data, false);

    if (dataContext.getData() == null) {
        dataContext.setData(new JsonObject());
    } else {
        dataContext.setData(newData);
    }
    dataContext.setScope(scope);
    dataContext.setReverseScope(reverseScope);
    return dataContext;
}

From source file:com.flipkart.android.proteus.toolbox.Utils.java

License:Apache License

public static Result readJson(String path, JsonObject data, int index) {
    // replace INDEX reference with index value
    if (ProteusConstants.INDEX.equals(path)) {
        path = path.replace(ProteusConstants.INDEX, String.valueOf(index));
        return Result.success(new JsonPrimitive(path));
    } else {/*from   www .j a  v  a2s  .  co m*/
        StringTokenizer tokenizer = new StringTokenizer(path, ProteusConstants.DATA_PATH_DELIMITERS);
        JsonElement elementToReturn = data;
        JsonElement tempElement;
        JsonArray tempArray;

        while (tokenizer.hasMoreTokens()) {
            String segment = tokenizer.nextToken();
            if (elementToReturn == null) {
                return Result.NO_SUCH_DATA_PATH_EXCEPTION;
            }
            if (elementToReturn.isJsonNull()) {
                return Result.JSON_NULL_EXCEPTION;
            }
            if ("".equals(segment)) {
                continue;
            }
            if (elementToReturn.isJsonArray()) {
                tempArray = elementToReturn.getAsJsonArray();

                if (ProteusConstants.INDEX.equals(segment)) {
                    if (index < tempArray.size()) {
                        elementToReturn = tempArray.get(index);
                    } else {
                        return Result.NO_SUCH_DATA_PATH_EXCEPTION;
                    }
                } else if (ProteusConstants.ARRAY_DATA_LENGTH_REFERENCE.equals(segment)) {
                    elementToReturn = new JsonPrimitive(tempArray.size());
                } else if (ProteusConstants.ARRAY_DATA_LAST_INDEX_REFERENCE.equals(segment)) {
                    if (tempArray.size() == 0) {
                        return Result.NO_SUCH_DATA_PATH_EXCEPTION;
                    }
                    elementToReturn = tempArray.get(tempArray.size() - 1);
                } else {
                    try {
                        index = Integer.parseInt(segment);
                    } catch (NumberFormatException e) {
                        return Result.INVALID_DATA_PATH_EXCEPTION;
                    }
                    if (index < tempArray.size()) {
                        elementToReturn = tempArray.get(index);
                    } else {
                        return Result.NO_SUCH_DATA_PATH_EXCEPTION;
                    }
                }
            } else if (elementToReturn.isJsonObject()) {
                tempElement = elementToReturn.getAsJsonObject().get(segment);
                if (tempElement != null) {
                    elementToReturn = tempElement;
                } else {
                    return Result.NO_SUCH_DATA_PATH_EXCEPTION;
                }
            } else if (elementToReturn.isJsonPrimitive()) {
                return Result.INVALID_DATA_PATH_EXCEPTION;
            } else {
                return Result.NO_SUCH_DATA_PATH_EXCEPTION;
            }
        }
        if (elementToReturn.isJsonNull()) {
            return Result.JSON_NULL_EXCEPTION;
        }
        return Result.success(elementToReturn);
    }
}

From source file:com.flipkart.android.proteus.toolbox.Utils.java

License:Apache License

public static JsonElement merge(JsonElement oldJson, JsonElement newJson, boolean useCopy, Gson gson) {

    JsonElement newDataElement;/*from  w  w  w  .  j av  a2s . c o m*/
    JsonArray oldArray;
    JsonArray newArray;
    JsonElement oldArrayItem;
    JsonElement newArrayItem;
    JsonObject oldObject;

    if (oldJson == null || oldJson.isJsonNull()) {
        return useCopy ? gson.fromJson(newJson, JsonElement.class) : newJson;
    }

    if (newJson == null || newJson.isJsonNull()) {
        newJson = JsonNull.INSTANCE;
        return newJson;
    }

    if (newJson.isJsonPrimitive()) {
        JsonPrimitive value;
        if (!useCopy) {
            return newJson;
        }
        if (newJson.getAsJsonPrimitive().isBoolean()) {
            value = new JsonPrimitive(newJson.getAsBoolean());
        } else if (newJson.getAsJsonPrimitive().isNumber()) {
            value = new JsonPrimitive(newJson.getAsNumber());
        } else if (newJson.getAsJsonPrimitive().isString()) {
            value = new JsonPrimitive(newJson.getAsString());
        } else {
            value = newJson.getAsJsonPrimitive();
        }
        return value;
    }

    if (newJson.isJsonArray()) {
        if (!oldJson.isJsonArray()) {
            return useCopy ? gson.fromJson(newJson, JsonArray.class) : newJson;
        } else {
            oldArray = oldJson.getAsJsonArray();
            newArray = newJson.getAsJsonArray();

            if (oldArray.size() > newArray.size()) {
                while (oldArray.size() > newArray.size()) {
                    oldArray.remove(oldArray.size() - 1);
                }
            }

            for (int index = 0; index < newArray.size(); index++) {
                if (index < oldArray.size()) {
                    oldArrayItem = oldArray.get(index);
                    newArrayItem = newArray.get(index);
                    oldArray.set(index, merge(oldArrayItem, newArrayItem, useCopy, gson));
                } else {
                    oldArray.add(newArray.get(index));
                }
            }
        }
    } else if (newJson.isJsonObject()) {
        if (!oldJson.isJsonObject()) {
            return useCopy ? gson.fromJson(newJson, JsonObject.class) : newJson;
        } else {
            oldObject = oldJson.getAsJsonObject();
            for (Map.Entry<String, JsonElement> entry : newJson.getAsJsonObject().entrySet()) {
                newDataElement = merge(oldObject.get(entry.getKey()), entry.getValue(), useCopy, gson);
                oldObject.add(entry.getKey(), newDataElement);
            }
        }
    } else {
        return useCopy ? gson.fromJson(newJson, JsonElement.class) : newJson;
    }

    return oldJson;
}

From source file:com.flipkart.android.proteus.view.manager.ProteusViewManagerImpl.java

License:Apache License

private void handleBinding(Binding binding) {
    if (binding.hasRegEx()) {
        layoutBuilder.handleAttribute(layoutHandler, (ProteusView) view, binding.getAttributeKey(),
                new JsonPrimitive(binding.getAttributeValue()));
    } else {//from  w  ww .j a va2  s.  c  om
        Result result = Utils.readJson(binding.getBindingName(), dataContext.getData(), dataContext.getIndex());
        JsonElement dataValue = result.isSuccess() ? result.element : JsonNull.INSTANCE;
        layoutBuilder.handleAttribute(layoutHandler, (ProteusView) view, binding.getAttributeKey(), dataValue);
    }
}

From source file:com.flipkart.android.proteus.view.manager.ProteusViewManagerImpl.java

License:Apache License

public void set(String dataPath, String newValue) {
    set(dataPath, new JsonPrimitive(newValue));
}

From source file:com.flipkart.android.proteus.view.manager.ProteusViewManagerImpl.java

License:Apache License

public void set(String dataPath, Number newValue) {
    set(dataPath, new JsonPrimitive(newValue));
}