Example usage for com.google.gson JsonElement isJsonArray

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

Introduction

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

Prototype

public boolean isJsonArray() 

Source Link

Document

provides check for verifying if this element is an array or not.

Usage

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 ww w.j av  a  2s .  c o 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 .ja  va2  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
        }//from   w ww .j a v a2s  .  com
        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.index.ElasticSearchManager.java

License:Apache License

public BulkResponse bulkAddDocuments(JsonElement docsJson, String idFieldName, String sParentId,
        boolean bAllowOverwrite) {
    if (null != _multiIndex) {
        throw new RuntimeException("bulkAddDocuments not supported on multi-index manager");
    }//from ww w . j  a v a 2s. co m
    if (!docsJson.isJsonArray()) {
        throw new RuntimeException("bulkAddDocuments - not a list");
    }
    BulkRequestBuilder brb = _elasticClient.prepareBulk();

    JsonArray docJsonArray = docsJson.getAsJsonArray();
    for (JsonElement docJson : docJsonArray) {
        IndexRequest ir = new IndexRequest(_sIndexName);
        ir.type(_sIndexType);
        if (null != sParentId) {
            ir.parent(sParentId);
        }
        if (!bAllowOverwrite) {
            ir.opType(OpType.CREATE);
        } //TESTED

        // Some _id unpleasantness
        if (null != idFieldName) {

            String id = docJson.getAsJsonObject().get(idFieldName).getAsString();
            ir.id(id);
            ir.source(docJson.toString());
        } //TESTED

        brb.add(ir);
    }
    brb.setConsistencyLevel(WriteConsistencyLevel.ONE);
    return brb.execute().actionGet();
}

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  w w.j a  va  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.jayway.jsonpath.internal.spi.mapper.GsonMapper.java

License:Apache License

@Override
public Object convert(Object src, Class<?> srcType, Class<?> targetType, Configuration conf) {

    assertValidConversion(src, srcType, targetType);

    if (src == null || src.getClass().equals(JsonNull.class)) {
        return null;
    }/* ww w . j ava2  s .c  o m*/

    if (JsonPrimitive.class.isAssignableFrom(srcType)) {

        JsonPrimitive primitive = (JsonPrimitive) src;
        if (targetType.equals(Long.class)) {
            return primitive.getAsLong();
        } else if (targetType.equals(Integer.class)) {
            return primitive.getAsInt();
        } else if (targetType.equals(BigInteger.class)) {
            return primitive.getAsBigInteger();
        } else if (targetType.equals(Byte.class)) {
            return primitive.getAsByte();
        } else if (targetType.equals(BigDecimal.class)) {
            return primitive.getAsBigDecimal();
        } else if (targetType.equals(Double.class)) {
            return primitive.getAsDouble();
        } else if (targetType.equals(Float.class)) {
            return primitive.getAsFloat();
        } else if (targetType.equals(String.class)) {
            return primitive.getAsString();
        } else if (targetType.equals(Boolean.class)) {
            return primitive.getAsBoolean();
        } else if (targetType.equals(Date.class)) {

            if (primitive.isNumber()) {
                return new Date(primitive.getAsLong());
            } else if (primitive.isString()) {
                try {
                    return DateFormat.getInstance().parse(primitive.getAsString());
                } catch (ParseException e) {
                    throw new MappingException(e);
                }
            }
        }

    } else if (JsonObject.class.isAssignableFrom(srcType)) {
        JsonObject srcObject = (JsonObject) src;
        if (targetType.equals(Map.class)) {
            Map<String, Object> targetMap = new LinkedHashMap<String, Object>();
            for (Map.Entry<String, JsonElement> entry : srcObject.entrySet()) {
                Object val = null;
                JsonElement element = entry.getValue();
                if (element.isJsonPrimitive()) {
                    val = GsonJsonProvider.unwrap(element);
                } else if (element.isJsonArray()) {
                    val = convert(element, element.getClass(), List.class, conf);
                } else if (element.isJsonObject()) {
                    val = convert(element, element.getClass(), Map.class, conf);
                } else if (element.isJsonNull()) {
                    val = null;
                }
                targetMap.put(entry.getKey(), val);
            }
            return targetMap;
        }

    } else if (JsonArray.class.isAssignableFrom(srcType)) {
        JsonArray srcArray = (JsonArray) src;
        if (targetType.equals(List.class)) {
            List<Object> targetList = new ArrayList<Object>();
            for (JsonElement element : srcArray) {
                if (element.isJsonPrimitive()) {
                    targetList.add(GsonJsonProvider.unwrap(element));
                } else if (element.isJsonArray()) {
                    targetList.add(convert(element, element.getClass(), List.class, conf));
                } else if (element.isJsonObject()) {
                    targetList.add(convert(element, element.getClass(), Map.class, conf));
                } else if (element.isJsonNull()) {
                    targetList.add(null);
                }
            }
            return targetList;
        }
    }

    throw new MappingException("Can not map: " + srcType.getName() + " to: " + targetType.getName());
}

From source file:com.jcwhatever.nucleus.storage.JsonDataNode.java

License:MIT License

@Nullable
@Override/*from   ww w.  jav a 2s  .co m*/
public Object get(String keyPath) {
    PreCon.notNull(keyPath);

    keyPath = getFullPath(keyPath);

    if (keyPath.isEmpty())
        return _object;

    String[] path = TextUtils.PATTERN_DOT.split(keyPath);

    JsonElement element = getJsonElement(path);
    if (element == null || element.isJsonObject())
        return null;

    if (element.isJsonArray()) {
        return getStringList(keyPath, null);
    }

    return element.getAsString();
}

From source file:com.jcwhatever.nucleus.storage.JsonDataNode.java

License:MIT License

@Override
@Nullable//  ww w. j  a  v a 2s  .c  o m
protected Object getCollectionObject(String keyPath) {
    JsonElement element = getJsonElement(keyPath);
    if (element == null || element.isJsonObject() || !element.isJsonArray())
        return null;

    JsonArray array = element.getAsJsonArray();
    List<String> result = new ArrayList<>(array.size());

    for (int i = 0; i < array.size(); i++) {
        result.add(array.get(i).getAsString());
    }

    return result;
}

From source file:com.jomofisher.tests.cmake.JsonUtils.java

License:Apache License

private static void checkJsonElementAgainstJavaType(JsonElement element, Class clazz) {
    if (element.isJsonArray()) {
        checkJsonArrayAgainstJavaType(element.getAsJsonArray(), clazz);
    } else if (element.isJsonObject()) {
        checkJsonObjectAgainstJavaType(element.getAsJsonObject(), clazz);
    }//from  w  w  w.  j  a  va 2 s  .  c o  m
}

From source file:com.jomofisher.tests.cmake.JsonUtils.java

License:Apache License

private static void checkJsonElementAgainstJava(JsonElement element, Field field) {
    if (element.isJsonArray()) {
        checkJsonArrayAgainstJava(element.getAsJsonArray(), field);
    } else if (element.isJsonObject()) {
        checkJsonObjectAgainstJava(element.getAsJsonObject(), field);
    }/*from  w  w  w  . ja  va  2  s. c o  m*/
}