Example usage for com.google.gson JsonElement getAsJsonPrimitive

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

Introduction

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

Prototype

public JsonPrimitive getAsJsonPrimitive() 

Source Link

Document

convenience method to get this element as a JsonPrimitive .

Usage

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. j  a v  a 2s. c  om*/
 * @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/* w  ww. jav a 2 s.  c o  m*/
 * @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/*from  ww  w .  java 2  s  . 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  va2  s  .  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.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());/*from  w w  w  . ja  v  a2s  .  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);
        }
    }
}

From source file:com.nimbits.server.gson.deserializer.DateDeserializer.java

License:Apache License

public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    try {/*w  ww.  j a  v  a 2  s  . co m*/
        return format.parse(json.getAsJsonPrimitive().getAsString());
    } catch (ParseException e) {
        throw new JsonParseException(e.getMessage());
    }
}

From source file:com.pc.dailymile.domain.converters.DateConverter.java

License:Apache License

public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    return deserializeFormatter.parseDateTime(json.getAsJsonPrimitive().getAsString()).toDate();
}

From source file:com.qmetry.qaf.automation.gson.GsonObjectDeserializer.java

License:Open Source License

public static Object read(JsonElement in) {

    if (in.isJsonArray()) {
        List<Object> list = new ArrayList<Object>();
        JsonArray arr = in.getAsJsonArray();
        for (JsonElement anArr : arr) {
            list.add(read(anArr));//w w w . ja  v  a2 s  . com
        }
        return list;
    } else if (in.isJsonObject()) {
        Map<String, Object> map = new LinkedTreeMap<String, Object>();
        JsonObject obj = in.getAsJsonObject();
        Set<Map.Entry<String, JsonElement>> entitySet = obj.entrySet();
        for (Map.Entry<String, JsonElement> entry : entitySet) {
            map.put(entry.getKey(), read(entry.getValue()));
        }
        return map;
    } else if (in.isJsonPrimitive()) {
        JsonPrimitive prim = in.getAsJsonPrimitive();
        if (prim.isBoolean()) {
            return prim.getAsBoolean();
        } else if (prim.isString()) {
            return prim.getAsString();
        } else if (prim.isNumber()) {
            if (prim.getAsString().contains("."))
                return prim.getAsDouble();
            else {
                return prim.getAsLong();
            }
        }
    }
    return null;
}

From source file:com.quchwe.cms.data.remote.RemoteSource.java

private RemoteSource(Context context) {
    this.mContext = context;
    mLocalSource = LocalSource.instance(context);
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return new Date(json.getAsJsonPrimitive().getAsLong());
        }/*from   w w  w  . j a  v  a2 s  .co  m*/
    });

    gson = builder.create();

}

From source file:com.ryanantkowiak.jOptionsHouseAPI.JsonUtilities.java

License:Open Source License

/**
 * Recursively print the contents of a GSON JSON element.
 * /*from   w  w w.  j a  va2s.co m*/
 * @param element   the GSON JSON element to be printed
 * @param prefix   output will be prefixed with this string
 */
public static void printJson(JsonElement element, String prefix) {
    if (null == prefix || prefix.isEmpty()) {
        prefix = "";
    }

    if (null == element || element.isJsonNull()) {
        System.out.println(prefix + " [null]");
        return;
    }

    else if (element.isJsonPrimitive()) {
        JsonPrimitive p = element.getAsJsonPrimitive();
        if (p.isBoolean()) {
            System.out.println(prefix + " [bool=" + p.getAsBoolean() + "]");
        } else if (p.isString()) {
            System.out.println(prefix + " [string='" + p.getAsString() + "']");
        } else if (p.isNumber()) {
            System.out.println(prefix + " [number=" + p.getAsDouble() + "]");
        }
    }

    else if (element.isJsonArray()) {
        System.out.println(prefix + " [array]");
        for (int i = 0; i < element.getAsJsonArray().size(); ++i) {
            String newPrefix = prefix + "[" + i + "]";
            printJson(element.getAsJsonArray().get(i), newPrefix);
        }
    }

    else if (element.isJsonObject()) {
        JsonObject obj = element.getAsJsonObject();

        for (Map.Entry<String, JsonElement> entry : obj.entrySet()) {
            String key = entry.getKey();
            JsonElement value = entry.getValue();
            String newPrefix = prefix + "." + key;
            printJson(value, newPrefix);
        }
    }

}