Example usage for com.google.gson JsonObject entrySet

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

Introduction

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

Prototype

public Set<Map.Entry<String, JsonElement>> entrySet() 

Source Link

Document

Returns a set of members of this object.

Usage

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 w w  . jav a2  s. co  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 .  j  ava 2s  .  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.ikanow.infinit.e.data_model.store.MongoDbUtil.java

License:Apache License

public static BasicDBObject encode(JsonObject o) {
    BasicDBObject dbo = new BasicDBObject();
    for (Map.Entry<String, JsonElement> elKV : o.entrySet()) {
        dbo.append(elKV.getKey(), encodeUnknown(elKV.getValue()));
    }//from  w  w  w. j av  a  2 s .  c om
    return dbo;
}

From source file:com.intellij.plugins.haxe.haxelib.HaxelibMetadata.java

License:Apache License

@Nullable
public List<Dependency> getDependencies() {
    if (null != root) {
        JsonElement elem = root.get(DEPENDENCIES);
        if (null != elem) {
            JsonObject dependencies = elem.getAsJsonObject(); // Can't fail.
            List<Dependency> list = new ArrayList<Dependency>();
            for (Map.Entry<String, JsonElement> entry : dependencies.entrySet()) {
                String name = entry.getKey();
                String version = entry.getValue().getAsString();
                list.add(new Dependency(name, version));
            }//from   w ww  .  j  av a2s  . c  om
            return list;
        }
    }
    return null;
}

From source file:com.intuit.wasabi.tests.service.statistic.StatisticsUtils.java

License:Apache License

static void COMPUTE_COUNT(JsonObject jsonObject) {
    jsonObject.remove("jointProgress");
    jsonObject.remove("actionProgress");
    jsonObject.remove("experimentProgress");
    jsonObject.remove("jointActionRate");
    jsonObject.remove("actionRates");
    JsonObject cur = jsonObject.getAsJsonObject("buckets");
    for (Map.Entry<String, JsonElement> entry : cur.entrySet()) {
        entry.getValue().getAsJsonObject().remove("actionRates");
        entry.getValue().getAsJsonObject().remove("jointActionRate");
        entry.getValue().getAsJsonObject().remove("bucketComparisons");
    }/*from  w ww  .  j a  v a 2 s.co  m*/
    JsonObject experimentStatistics = jsonObject.getAsJsonObject("buckets");
    for (String label : new String[] { "red", "blue" }) {
        JsonObject bucket = experimentStatistics.getAsJsonObject(label);
        if (bucket.getAsJsonObject("actionCounts").entrySet().size() == 0) {
            experimentStatistics.remove(label);
        }
    }
}

From source file:com.itametis.jsonconverter.pathstrategy.flattener.JsonObjectFlattener.java

License:Open Source License

@Override
protected void flatten(JsonObject element, JsonElementProxy currentElement) throws JsonException {
    for (Map.Entry<String, JsonElement> field : element.entrySet()) {
        LOGGER.debug("Found Json Leaf '{}' : {}, applied to parent {}", field.getKey(),
                field.getValue().toString(), currentElement.getJsonName());
        JsonElementProxy elementProxy = new JsonElementProxy(field.getValue(), field.getKey(), currentElement);
        this.flattenizer.flatten(elementProxy);
    }/*from  w w  w . j ava 2  s  .  c  om*/
}

From source file:com.itametis.jsonconverter.pathstrategy.flattener.JsonObjectFlattener.java

License:Open Source License

/**
 * Delete all links with other json Elements.
 *
 * @param element//from   w ww.j a  v  a  2s.  c  om
 */
protected void empty(JsonObject element) {
    LOGGER.debug("emptying json object {}", element.toString());
    List<String> toRemove = new ArrayList<>();
    for (Map.Entry<String, JsonElement> field : element.entrySet()) {
        toRemove.add(field.getKey());
    }
    for (String elementToRemove : toRemove) {
        element.remove(elementToRemove);
    }
}

From source file:com.jayway.jsonpath.internal.spi.json.GsonJsonProvider.java

License:Apache License

@Override
public Iterable<?> toIterable(Object obj) {
    if (isArray(obj)) {
        JsonArray arr = toJsonArray(obj);
        List<Object> values = new ArrayList<Object>(arr.size());
        for (Object o : arr) {
            values.add(unwrap(o));/*from w w w  .j a  v a  2  s .c  o m*/
        }
        return values;
    } else {
        JsonObject jsonObject = toJsonObject(obj);
        List<Object> values = new ArrayList<Object>();
        for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
            values.add(unwrap(entry.getValue()));
        }
        return values;
    }
}

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;
    }/*from   www.  jav a  2  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.jbirdvegas.mgerrit.objects.FileInfoList.java

License:Apache License

public static FileInfoList deserialize(JsonObject object) {
    List<FileInfo> newList = new ArrayList<>();
    Set<Map.Entry<String, JsonElement>> entries = object.entrySet();
    for (Map.Entry<String, JsonElement> entry : entries) {
        newList.add(FileInfo.deserialise(entry.getValue().getAsJsonObject(), entry.getKey()));

    }//from ww w .j  av  a  2  s.  c om
    return new FileInfoList(newList);
}