Example usage for com.google.gson JsonElement isJsonPrimitive

List of usage examples for com.google.gson JsonElement isJsonPrimitive

Introduction

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

Prototype

public boolean isJsonPrimitive() 

Source Link

Document

provides check for verifying if this element is a primitive or not.

Usage

From source file:com.ls.util.ObjectComparator.java

License:Open Source License

@Nullable
private static Object getDifferencesObject(JsonElement origin, JsonElement patched) {
    if (origin != null && origin.equals(patched)) {
        return UNCHANGED;
    }// w  w w  .  j av a  2  s .c o m

    if (patched == null || patched.isJsonNull()) {
        return null;
    }

    if (origin == null || origin.isJsonNull()) {
        return convertElementToStringRepresentation(patched);
    }

    if (origin.isJsonArray()) {
        if (patched.isJsonArray()) {
            return getDifferencesForArrays((JsonArray) origin, (JsonArray) patched);
        } else {
            return convertElementToStringRepresentation(patched);
        }
    }

    if (origin.isJsonObject()) {
        if (patched.isJsonObject()) {
            return getDifferencesMapForObjects((JsonObject) origin, (JsonObject) patched);
        } else {
            return convertElementToStringRepresentation(patched);
        }
    }

    if (origin.isJsonPrimitive()) {
        if (patched.isJsonPrimitive()) {
            return getDifferencesForPrimitives((JsonPrimitive) origin, (JsonPrimitive) patched);
        } else {
            return convertElementToStringRepresentation(patched);
        }
    }

    return convertElementToStringRepresentation(patched);
}

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

License:Open Source License

/**
 * Validates if the id property is numeric.
 * @param element/*from   w w  w  .  j  a  va2s. com*/
 * @return
 */
private boolean isValidTypeId(JsonElement element) {
    return element.isJsonPrimitive() && element.getAsJsonPrimitive().isNumber();
}

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

License:Open Source License

/**
 * Validates if the object represents a string value.
 *
 * @param o/*w  w  w .j  a  v a 2 s .co  m*/
 * @return
 */
protected boolean isStringType(Object o) {
    boolean result = (o instanceof String);

    if (o instanceof JsonElement) {
        JsonElement json = (JsonElement) o;

        if (json.isJsonPrimitive()) {
            JsonPrimitive primitive = json.getAsJsonPrimitive();
            result = primitive.isString();
        }
    }

    return result;
}

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

License:Open Source License

/**
 * Returns the string value represented by the object.
 *
 * @param o/*  w w  w  .  ja v a2s.  c o  m*/
 * @return
 */
protected String getStringValue(Object o) {
    String result;

    if (o instanceof String) {
        result = (String) o;
    } else if (o instanceof JsonElement) {
        JsonElement json = (JsonElement) o;

        if (json.isJsonPrimitive()) {
            JsonPrimitive primitive = json.getAsJsonPrimitive();

            if (primitive.isString()) {
                result = primitive.getAsString();
            } else {
                throw new IllegalArgumentException("Object does not represent a string value.");
            }
        } else {
            throw new IllegalArgumentException("Object does not represent a string value.");
        }
    } else {
        throw new IllegalArgumentException("Object does not represent a string value.");
    }

    return result;
}

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

License:Open Source License

/**
 * Validates if the object represents a numeric value.
 *
 * @param o/*from   w  w  w.  j a va2 s  .c  om*/
 * @return
 */
protected boolean isNumericType(Object o) {
    boolean result = (o instanceof Integer) || (o instanceof Long);

    if (o instanceof JsonElement) {
        JsonElement json = (JsonElement) o;

        if (json.isJsonPrimitive()) {
            JsonPrimitive primitive = json.getAsJsonPrimitive();
            result = primitive.isNumber();
        }
    }

    return result;
}

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

License:Open Source License

/**
 * Returns the numeric value represented by the object.
 *
 * @param o/*  w w w .  j a v  a  2s  .  c  o m*/
 * @return
 */
protected long getNumericValue(Object o) {
    long result;

    if (o instanceof Integer) {
        result = (Integer) o;
    } else if (o instanceof Long) {
        result = (Long) o;
    } else if (o instanceof JsonElement) {
        JsonElement json = (JsonElement) o;

        if (json.isJsonPrimitive()) {
            JsonPrimitive primitive = json.getAsJsonPrimitive();

            if (primitive.isNumber()) {
                result = primitive.getAsLong();
            } else {
                throw new IllegalArgumentException("Object does not represent a string value.");
            }
        } else {
            throw new IllegalArgumentException("Object does not represent a string value.");
        }
    } else {
        throw new IllegalArgumentException("Object does not represent a string value.");
    }

    return result;
}

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

License:Open Source License

private void appendInsertValuesSql(StringBuilder sql, List<Object> parameters,
        Map<String, ColumnDataInfo> tableDefinition, JsonObject item, boolean fromServer) {
    sql.append("(");
    int colCount = 0;

    for (Entry<String, JsonElement> property : item.entrySet()) {

        if (fromServer && !tableDefinition.containsKey(normalizeColumnName(property.getKey()))) {
            continue;
        }//from  w  w w.  j  a v a  2s . c  o  m

        if (colCount > 0)
            sql.append(",");

        String paramName = "@p" + parameters.size();

        JsonElement value = property.getValue();

        if (value.isJsonNull()) {
            parameters.add(null);
        } else if (value.isJsonPrimitive()) {
            if (value.getAsJsonPrimitive().isBoolean()) {
                long longVal = value.getAsJsonPrimitive().getAsBoolean() ? 1L : 0L;
                parameters.add(longVal);
            } else if (value.getAsJsonPrimitive().isNumber()) {
                parameters.add(value.getAsJsonPrimitive().getAsDouble());
            } else {
                parameters.add(value.getAsJsonPrimitive().getAsString());
            }
        } else {
            parameters.add(value.toString());
        }

        sql.append(paramName);
        colCount++;

    }

    sql.append(")");
}

From source file:com.moz.fiji.mapreduce.lib.bulkimport.JSONBulkImporter.java

License:Apache License

/**
 * Returns a string containing an element referenced by the specified path, or null if the
 * element isn't found.  This uses a period '.' delimited syntax similar to JSONPath
 * ({@linktourl http://goessner.net/articles/JsonPath/}).
 *
 * TODO(FIJIMRLIB-5) Use an enhanced JSONPath library for this functionality.
 *
 * @param head JsonObject that is the head of the current JSON tree.
 * @param path delimited by periods/* ww w  . ja v a 2s. c o m*/
 * @return string denoting the element at the specified path.
 */
private String getFromPath(JsonObject head, String path) {
    Preconditions.checkNotNull(head);
    Preconditions.checkNotNull(path);

    // Split the path into components using the delimiter for tree traversal.
    String[] pathComponents = path.split("\\.");

    // After getting the path components traverse the json tree.
    JsonElement jsonElement = head;
    for (String pathComponent : pathComponents) {
        if (jsonElement.isJsonObject()) {
            JsonObject jsonObject = jsonElement.getAsJsonObject();
            if (jsonObject.has(pathComponent)) {
                jsonElement = jsonObject.get(pathComponent);
            } else {
                LOG.warn("Missing path component {} at current path {}.  Returning null.", pathComponent,
                        jsonObject);
                return null;
            }
        }
    }
    if (jsonElement.isJsonPrimitive()) {
        return jsonElement.getAsString();
    }
    LOG.warn("Specified path {} is not complete for {}.  Returning null", path, head);
    return null;
}

From source file:com.nextdoor.bender.operation.json.key.FlattenOperation.java

License:Apache License

protected void perform(JsonObject obj) {

    Set<Entry<String, JsonElement>> entries = obj.entrySet();
    Set<Entry<String, JsonElement>> orgEntries = new HashSet<Entry<String, JsonElement>>(entries);

    for (Entry<String, JsonElement> entry : orgEntries) {
        JsonElement val = entry.getValue();

        if (val.isJsonPrimitive() || val.isJsonNull()) {
            continue;
        }//from  w  w w . j a va  2  s .co  m

        obj.remove(entry.getKey());
        if (val.isJsonObject()) {
            perform(obj, val.getAsJsonObject(), entry.getKey());
        } else if (val.isJsonArray()) {
            perform(obj, val.getAsJsonArray(), entry.getKey());
        }
    }
}

From source file:com.nextdoor.bender.operation.json.key.KeyNameOperation.java

License:Apache License

protected void perform(JsonObject obj) {
    Set<Entry<String, JsonElement>> entries = obj.entrySet();
    Set<Entry<String, JsonElement>> orgEntries = new HashSet<Entry<String, JsonElement>>(entries);

    for (Entry<String, JsonElement> entry : orgEntries) {

        JsonElement val = entry.getValue();
        obj.remove(entry.getKey());// w  w  w .j  a  va2s  . c o m
        String key = entry.getKey().toLowerCase().replaceAll("[ .]", "_");

        if (val.isJsonPrimitive()) {
            JsonPrimitive prim = val.getAsJsonPrimitive();

            if (prim.isBoolean()) {
                obj.add(key + "__bool", val);
            } else if (prim.isNumber()) {
                if (prim.toString().contains(".")) {
                    obj.add(key + "__float", val);
                } else {
                    obj.add(key + "__long", val);
                }
            } else if (prim.isString()) {
                obj.add(key + "__str", val);
            }
        } else if (val.isJsonObject()) {
            obj.add(key, val);
            perform(val.getAsJsonObject());
        } else if (val.isJsonArray()) {
            obj.add(key + "__arr", val);
        }
    }
}