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.ikanow.infinit.e.data_model.custom.InfiniteFileInputJsonParser.java

License:Apache License

private String getKey(JsonElement meta, String key, boolean bPrimitiveOnly) {
    try {/* w ww  . j  a v  a  2s  .  co m*/
        String[] components = key.split("\\.");
        JsonObject metaObj = meta.getAsJsonObject();
        for (String comp : components) {
            meta = metaObj.get(comp);

            if (null == meta) {
                return null;
            } //TESTED
            else if (meta.isJsonObject()) {
                metaObj = meta.getAsJsonObject();
            } //TESTED
            else if (meta.isJsonPrimitive()) {
                return meta.getAsString();
            } //TESTED
            else if (bPrimitiveOnly) { // (meta isn't allowed to be an array, then you'd have too many primary keys!)
                return null;
            } //TOTEST (? - see JsonToMetadataParser)
            else { // Check with first instance
                JsonArray array = meta.getAsJsonArray();
                meta = array.get(0);
                if (meta.isJsonObject()) {
                    metaObj = meta.getAsJsonObject();
                }
            } //TESTED            
        }
        if (!bPrimitiveOnly) { // allow objects, we just care if the field exists...
            if (null != metaObj) {
                return "[Object]";
            }
        } //TESTED
    } catch (Exception e) {
    } // no primary key

    return null;
}

From source file:com.ikanow.infinit.e.data_model.custom.InfiniteFileInputJsonParser.java

License:Apache License

static public BasicDBObject convertJsonObjectToBson(JsonObject json, boolean bHtmlUnescape) {
    int length = json.entrySet().size();
    BasicDBObject list = new BasicDBObject(capacity(length));
    for (Map.Entry<String, JsonElement> jsonKeyEl : json.entrySet()) {
        JsonElement jsonEl = jsonKeyEl.getValue();
        if (jsonEl.isJsonArray()) {
            list.put(jsonKeyEl.getKey(), handleJsonArray(jsonEl.getAsJsonArray(), bHtmlUnescape));
        } else if (jsonEl.isJsonObject()) {
            list.put(jsonKeyEl.getKey(), convertJsonObjectToBson(jsonEl.getAsJsonObject(), bHtmlUnescape));
        } else if (jsonEl.isJsonPrimitive()) {
            if (bHtmlUnescape) {
                list.put(jsonKeyEl.getKey(), StringEscapeUtils.unescapeHtml(jsonEl.getAsString()));
            } else {
                list.put(jsonKeyEl.getKey(), jsonEl.getAsString());
            }/*from  w w w. j  a v a  2s  . co  m*/
        }
    }
    if (list.size() > 0) {
        return list;
    }
    return null;
}

From source file:com.ikanow.infinit.e.data_model.custom.InfiniteFileInputJsonParser.java

License:Apache License

static private Object[] handleJsonArray(JsonArray jarray, boolean bHtmlUnescape) {
    Object o[] = new Object[jarray.size()];
    for (int i = 0; i < jarray.size(); i++) {
        JsonElement jsonEl = jarray.get(i);
        if (jsonEl.isJsonObject()) {
            o[i] = convertJsonObjectToBson(jsonEl.getAsJsonObject(), bHtmlUnescape);
        }//from   ww w . java  2 s.c o m
        if (jsonEl.isJsonArray()) {
            o[i] = handleJsonArray(jsonEl.getAsJsonArray(), bHtmlUnescape);
        } else if (jsonEl.isJsonPrimitive()) {
            if (bHtmlUnescape) {
                o[i] = StringEscapeUtils.unescapeHtml(jsonEl.getAsString());
            } else {
                o[i] = jsonEl.getAsString();
            }
        }
    }
    return o;
}

From source file:com.ikanow.infinit.e.data_model.index.document.DocumentPojoIndexMap.java

License:Apache License

private static boolean enforceTypeNamingPolicy(JsonElement je, int nDepth) {

    if (je.isJsonPrimitive()) {
        return false; // Done
    } else if (je.isJsonArray()) {
        JsonArray ja = je.getAsJsonArray();
        if (0 == ja.size()) {
            return false; // No idea, carry on
        }/* ww w  .  j a  va  2 s.c  o m*/
        JsonElement jaje = ja.get(0);
        return enforceTypeNamingPolicy(jaje, nDepth + 1); // keep going until you find primitive/object
    } else if (je.isJsonObject()) {
        JsonObject jo = je.getAsJsonObject();
        // Nested variables:
        Iterator<Entry<String, JsonElement>> it = jo.entrySet().iterator();
        StringBuffer newName = null;
        Map<String, JsonElement> toFixList = null;
        while (it.hasNext()) {
            boolean bFix = false;
            Entry<String, JsonElement> el = it.next();
            String currKey = el.getKey();

            if ((currKey.indexOf('.') >= 0) || (currKey.indexOf('%') >= 0)) {
                it.remove();
                currKey = currKey.replace("%", "%25").replace(".", "%2e");
                bFix = true;
            }
            if (null == el.getValue()) {
                if (!bFix)
                    it.remove(); // nice easy case, just get rid of it (if bFix, it's already removed)
                bFix = false;
            } else if (enforceTypeNamingPolicy(el.getValue(), nDepth + 1)) { // rename!
                if (currKey.indexOf("__") < 0) { // unless it's an es type
                    if (!bFix)
                        it.remove(); // (if bFix, it's already removed)
                    if (null == newName) {
                        newName = new StringBuffer();
                    } else {
                        newName.setLength(0);
                    }
                    currKey = newName.append(currKey).append("__obj").toString();
                    bFix = true;
                }
            } // (end check if need to rename)
            if (bFix) {
                if (null == toFixList) {
                    toFixList = new HashMap<String, JsonElement>();
                }
                toFixList.put(currKey, el.getValue());
            }
        } // (end loop over params)   
        if (null != toFixList) {
            for (Entry<String, JsonElement> el : toFixList.entrySet()) {
                jo.add(el.getKey(), el.getValue());
            }
        }
        return true; // (in any case, I get renamed by calling parent)
    }
    return false;
}

From source file:com.ikanow.infinit.e.data_model.store.MongoDbUtil.java

License:Apache License

public static Object encodeUnknown(JsonElement from) {
    if (from.isJsonArray()) { // Array
        return encodeArray(from.getAsJsonArray());
    } //TESTED/* w  ww  .  ja v a  2  s  .  c  o  m*/
    else if (from.isJsonObject()) { // Object
        JsonObject obj = from.getAsJsonObject();
        // Check for OID/Date:
        if (1 == obj.entrySet().size()) {
            if (obj.has("$date")) {
                try {
                    return _format.parse(obj.get("$date").getAsString());
                } catch (ParseException e) {
                    try {
                        return _format2.parse(obj.get("$date").getAsString());
                    } catch (ParseException e2) {
                        return null;
                    }
                }
            } //TESTED
            else if (obj.has("$oid")) {
                return new ObjectId(obj.get("$oid").getAsString());
            } //TESTED                
        }
        return encode(obj);
    } //TESTED
    else if (from.isJsonPrimitive()) { // Primitive
        JsonPrimitive val = from.getAsJsonPrimitive();
        if (val.isNumber()) {
            return val.getAsNumber();
        } //TESTED
        else if (val.isBoolean()) {
            return val.getAsBoolean();
        } //TESTED
        else if (val.isString()) {
            return val.getAsString();
        } //TESTED
    } //TESTED
    return null;
}

From source file:com.indragie.cmput301as1.DateJSONDeserializer.java

License:Open Source License

@Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (json.isJsonPrimitive()) {
        JsonPrimitive primitive = json.getAsJsonPrimitive();
        if (primitive.isNumber()) {
            return new Date(primitive.getAsLong());
        }//from   w  w  w.ja va  2 s. c  o  m
    }
    return null;
}

From source file:com.indragie.cmput301as1.JSONHelpers.java

License:Open Source License

/**
 * Gets the value of the JSON element as a string if possible.
 * @param element The JSON element.//from w ww. jav a 2  s  .  co  m
 * @return A string if the JSON element was a string, or
 * null otherwise.
 */
public static String getStringIfPossible(JsonElement element) {
    if (element == null)
        return null;

    if (element.isJsonPrimitive()) {
        JsonPrimitive primitive = element.getAsJsonPrimitive();
        if (primitive.isString()) {
            return primitive.getAsString();
        }
    }
    return null;
}

From source file:com.indragie.cmput301as1.JSONHelpers.java

License:Open Source License

/**
 * Gets the value of the JSON element as a boolean if possible.
 * @param element The JSON element./*from w ww  .ja  va2s. com*/
 * @return A string if the JSON element was a boolean, or
 * false otherwise.
 */
public static boolean getBooleanIfPossible(JsonElement element) {
    if (element == null)
        return false;

    if (element.isJsonPrimitive()) {
        JsonPrimitive primitive = element.getAsJsonPrimitive();
        if (primitive.isBoolean()) {
            return primitive.getAsBoolean();
        }
    }
    return false;
}

From source file:com.indragie.cmput301as1.JSONHelpers.java

License:Open Source License

/**
 * Gets the value of the JSON element as a long if possible.
 * @param element The JSON element./*www.  j  a  v a2 s .c o  m*/
 * @return A string if the JSON element was a long, or
 * 0 otherwise.
 */
public static long getLongIfPossible(JsonElement element) {
    if (element == null)
        return 0;

    if (element.isJsonPrimitive()) {
        JsonPrimitive primitive = element.getAsJsonPrimitive();
        if (primitive.isNumber()) {
            return primitive.getAsLong();
        }
    }
    return 0;
}

From source file:com.javacreed.examples.gson.part3.AuthorDeserializer.java

License:Apache License

@Override
public Author deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context)
        throws JsonParseException {

    // Only the ID is available
    if (json.isJsonPrimitive()) {
        final JsonPrimitive primitive = json.getAsJsonPrimitive();
        return getOrCreate(primitive.getAsInt());
    }//from w ww. ja  va  2  s  .co m

    // The whole object is available
    if (json.isJsonObject()) {
        final JsonObject jsonObject = json.getAsJsonObject();

        final Author author = getOrCreate(jsonObject.get("id").getAsInt());
        author.setName(jsonObject.get("name").getAsString());
        return author;
    }

    throw new JsonParseException("Unexpected JSON type: " + json.getClass().getSimpleName());
}