Example usage for com.google.gson JsonObject remove

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

Introduction

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

Prototype

public JsonElement remove(String property) 

Source Link

Document

Removes the property from this JsonObject .

Usage

From source file:com.microsoft.windowsazure.mobileservices.MobileServiceJsonTable.java

License:Open Source License

/**
 * Updates the JsonObject to have an id property
 * @param json/*from w  ww .  j  a va2 s .  c  om*/
 *            the element to evaluate
 */
private void updateIdProperty(final JsonObject json) throws IllegalArgumentException {
    for (Map.Entry<String, JsonElement> entry : json.entrySet()) {
        String key = entry.getKey();
        if (key.equalsIgnoreCase("id")) {
            JsonElement element = entry.getValue();
            if (isValidTypeId(element)) {
                if (!key.equals("id")) {
                    //force the id name to 'id', no matter the casing 
                    json.remove(key);
                    // Create a new id property using the given property name
                    json.addProperty("id", entry.getValue().getAsNumber());
                }
                return;
            } else {
                throw new IllegalArgumentException("The id must be numeric");
            }
        }
    }
}

From source file:com.microsoft.windowsazure.mobileservices.MobileServiceTable.java

License:Open Source License

/**
 * Changes returned JSon object's id property name to match with type's id property name.
 * @param element// w ww.  j  a  v  a  2s.  c  o m
 * @param propertyName
 */
private void changeIdPropertyName(JsonObject element, String propertyName) {
    // If the property name is id or if there's no id defined, then return without performing changes
    if (propertyName.equals("id") || propertyName.length() == 0)
        return;

    // Get the current id value and remove the JSon property
    String value = element.get("id").getAsString();
    element.remove("id");

    // Create a new id property using the given property name
    element.addProperty(propertyName, value);
}

From source file:com.microsoft.windowsazure.mobileservices.table.MobileServiceJsonTable.java

License:Open Source License

/**
 * Updates the Version System Property in the Json Object with the ETag
 * information/*from www.  j a  va2s.  c  om*/
 *
 * @param response The response containing the ETag Header
 * @param json     The JsonObject to modify
 */
private static void updateVersionFromETag(ServiceFilterResponse response, JsonObject json) {
    if (response != null && response.getHeaders() != null) {
        for (Header header : response.getHeaders()) {
            if (header.getName().equalsIgnoreCase("ETag")) {
                json.remove(VersionSystemPropertyName);
                json.addProperty(VersionSystemPropertyName, getValueFromEtag(header.getValue()));
                break;
            }
        }
    }
}

From source file:com.microsoft.windowsazure.mobileservices.table.MobileServiceJsonTable.java

License:Open Source License

/**
 * Validates the Id property from a JsonObject on an Insert Action
 *
 * @param json The JsonObject to modify//  www  .  java  2s. com
 */
private Object validateIdOnInsert(final JsonObject json) {
    // Remove id property if exists
    String[] idPropertyNames = new String[] { "id", "Id", "iD", "ID" };

    for (int i = 0; i < 4; i++) {
        String idProperty = idPropertyNames[i];

        if (json.has(idProperty)) {
            JsonElement idElement = json.get(idProperty);

            if (isStringType(idElement)) {
                String id = getStringValue(idElement);

                if (!isValidStringId(id)) {
                    throw new IllegalArgumentException(
                            "The entity to insert has an invalid string value on " + idProperty + " property.");
                }

                return id;
            } else if (isNumericType(idElement)) {
                long id = getNumericValue(idElement);

                if (!isDefaultNumericId(id)) {
                    throw new IllegalArgumentException("The entity to insert should not have a numeric "
                            + idProperty + " property defined.");
                }

                json.remove(idProperty);

                return id;

            } else if (idElement.isJsonNull()) {
                json.remove(idProperty);

                return null;

            } else {
                throw new IllegalArgumentException("The entity to insert should not have an " + idProperty
                        + " defined with an invalid value");
            }
        }
    }

    return null;
}

From source file:com.microsoft.windowsazure.mobileservices.table.MobileServiceTableBase.java

License:Open Source License

/**
 * Removes all system properties (name start with '__') from the instance if
 * the instance is determined to have a string id and therefore be for table
 * that supports system properties./*from   w  ww . j  a  v  a  2s  .  c  o m*/
 *
 * @param instance The instance to remove the system properties from.
 * @param version  Set to the value of the version system property before it is
 *                 removed.
 * @return The instance with the system properties removed.
 */
protected static JsonObject removeSystemProperties(JsonObject instance) {
    boolean haveCloned = false;

    for (Entry<String, JsonElement> property : instance.entrySet()) {
        if (SystemPropertyNameToEnum.containsKey(property.getKey())) {
            // We don't want to alter the original JsonObject passed in by
            // the caller
            // so if we find a system property to remove, we have to clone
            // first
            if (!haveCloned) {
                instance = (JsonObject) new JsonParser().parse(instance.toString());
                haveCloned = true;
            }

            instance.remove(property.getKey());
        }
    }

    return instance;
}

From source file:com.microsoft.windowsazure.mobileservices.table.MobileServiceTableBase.java

License:Open Source License

/**
 * Updates the JsonObject to have an id property
 *
 * @param json the element to evaluate//from  w  w  w .j  a  v a  2  s .co m
 */
protected void updateIdProperty(final JsonObject json) throws IllegalArgumentException {
    for (Entry<String, JsonElement> entry : json.entrySet()) {
        String key = entry.getKey();

        if (key.equalsIgnoreCase("id")) {
            JsonElement element = entry.getValue();

            if (isValidTypeId(element)) {
                if (!key.equals("id")) {
                    // force the id name to 'id', no matter the casing
                    json.remove(key);
                    // Create a new id property using the given property
                    // name

                    JsonPrimitive value = entry.getValue().getAsJsonPrimitive();
                    if (value.isNumber()) {
                        json.addProperty("id", value.getAsLong());
                    } else {
                        json.addProperty("id", value.getAsString());
                    }
                }

                return;
            } else {
                throw new IllegalArgumentException("The id must be numeric or string");
            }
        }
    }
}

From source file:com.microsoft.windowsazure.mobileservices.table.serialization.JsonEntityParser.java

License:Open Source License

/**
 * Changes returned JSon object's id property name to match with type's id
 * property name./*from  ww  w . j  a va  2 s . com*/
 *
 * @param element
 * @param propertyName
 */
private static void changeIdPropertyName(JsonObject element, String propertyName) {
    // If the property name is id or if there's no id defined, then return
    // without performing changes
    if (propertyName.equals("id") || propertyName.length() == 0)
        return;

    if (element.has("id")) {

        JsonElement idElement = element.get("id");

        String value = idElement.isJsonNull() ? null : idElement.getAsString();
        element.remove("id");

        // Create a new id property using the given property name
        element.addProperty(propertyName, value);
    }
}

From source file:com.microsoft.windowsazure.mobileservices.table.sync.MobileServiceJsonSyncTable.java

License:Open Source License

private JsonObject validateIdOnInsert(JsonObject item) throws JsonSyntaxException, IllegalArgumentException {
    JsonObject newItem = (JsonObject) new JsonParser().parse(item.toString());
    String itemId = null;//from  w  w  w .j  ava2 s  . co  m
    String idProperty = getIdProperty(newItem);

    if (idProperty != null) {
        if (newItem.get(idProperty).isJsonPrimitive()
                && newItem.get(idProperty).getAsJsonPrimitive().isString()) {
            itemId = newItem.get(idProperty).getAsJsonPrimitive().getAsString();

            if (!isValidStringId(itemId)) {
                throw new IllegalArgumentException(
                        "The entity to insert has an invalid string value on " + idProperty + " property.");
            }
        } else if (newItem.get(idProperty).isJsonNull()) {
            itemId = UUID.randomUUID().toString();
        } else {
            throw new IllegalArgumentException("The entity to insert should not have an " + idProperty
                    + " defined with a non string value");
        }

        newItem.remove(idProperty);
        newItem.addProperty("id", itemId);
    } else {
        itemId = UUID.randomUUID().toString();
        newItem.addProperty("id", itemId);
    }

    return newItem;
}

From source file:com.microsoft.windowsazure.mobileservices.table.sync.MobileServiceJsonSyncTable.java

License:Open Source License

private JsonObject validateIdOnUpdateOrDelete(JsonObject item)
        throws JsonSyntaxException, IllegalArgumentException {
    JsonObject newItem = (JsonObject) new JsonParser().parse(item.toString());
    String itemId = null;//from  w  w w.j  a  va 2s.co m
    String idProperty = getIdProperty(newItem);

    if (idProperty != null) {
        if (newItem.get(idProperty).isJsonPrimitive()
                && newItem.get(idProperty).getAsJsonPrimitive().isString()) {
            itemId = newItem.get(idProperty).getAsJsonPrimitive().getAsString();

            if (!isValidStringId(itemId)) {
                throw new IllegalArgumentException("The entity to update/delete has an invalid string value on "
                        + idProperty + " property.");
            }
        } else if (newItem.get(idProperty).isJsonNull()) {
            throw new IllegalArgumentException(
                    "The entity to update/delete should have an id defined with a string value");
        } else {
            throw new IllegalArgumentException("The entity to update/delete should not have an " + idProperty
                    + " defined with a non string value");
        }

        newItem.remove(idProperty);
        newItem.addProperty("id", itemId);
    } else {
        throw new IllegalArgumentException(
                "The entity to update/delete should have an id defined with a string value");
    }

    return newItem;
}

From source file:com.microsoft.windowsazure.mobileservices.table.sync.operations.RemoteTableOperationProcessor.java

License:Open Source License

private static JsonObject removeSystemProperties(JsonObject instance) {
    boolean haveCloned = false;

    for (Entry<String, JsonElement> property : instance.entrySet()) {
        if (property.getKey().startsWith("__")) {
            if (!haveCloned) {
                instance = (JsonObject) new JsonParser().parse(instance.toString());
                haveCloned = true;//from www. j a va  2  s  . c o  m
            }

            instance.remove(property.getKey());
        }
    }

    return instance;
}