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.microsoft.windowsazure.mobileservices.table.MobileServiceTableBase.java

License:Open Source License

/**
 * Patches the original entity with the one returned in the response after
 * executing the operation//from w  w w . j a va  2s. co  m
 *
 * @param originalEntity The original entity
 * @param newEntity      The entity obtained after executing the operation
 * @return
 */
protected JsonObject patchOriginalEntityWithResponseEntity(JsonObject originalEntity, JsonObject newEntity) {
    // Patch the object to return with the new values
    JsonObject patchedEntityJson = (JsonObject) new JsonParser().parse(originalEntity.toString());

    for (Entry<String, JsonElement> entry : newEntity.entrySet()) {
        patchedEntityJson.add(entry.getKey(), entry.getValue());
    }

    return patchedEntityJson;
}

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

License:Open Source License

/**
 * Updates the JsonObject to have an id property
 *
 * @param json the element to evaluate//from   w  w  w .ja v a 2  s. co m
 */
protected void updateIdProperty(final JsonObject json) throws IllegalArgumentException {
    for (Entry<String, JsonElement> entry : json.entrySet()) {
        String key = entry.getKey();

        if (key.equalsIgnoreCase("id")) {
            JsonElement element = entry.getValue();

            if (isValidTypeId(element)) {
                if (!key.equals("id")) {
                    // force the id name to 'id', no matter the casing
                    json.remove(key);
                    // Create a new id property using the given property
                    // name

                    JsonPrimitive value = entry.getValue().getAsJsonPrimitive();
                    if (value.isNumber()) {
                        json.addProperty("id", value.getAsLong());
                    } else {
                        json.addProperty("id", value.getAsString());
                    }
                }

                return;
            } else {
                throw new IllegalArgumentException("The id must be numeric or string");
            }
        }
    }
}

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

License:Open Source License

private Statement generateUpsertStatement(String tableName, JsonObject[] items, boolean fromServer) {
    Statement result = new Statement();

    String invTableName = normalizeTableName(tableName);

    StringBuilder sql = new StringBuilder();

    sql.append("INSERT OR REPLACE INTO \"");
    sql.append(invTableName);/*from ww  w. java 2  s. c o m*/
    sql.append("\" (");

    String delimiter = "";

    JsonObject firstItem = items[0];

    Map<String, ColumnDataInfo> tableDefinition = mTables.get(invTableName);

    List<Object> parameters = new ArrayList<Object>(firstItem.entrySet().size());

    int columnsOnStatement = 0;

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

        //if (isSystemProperty(property.getKey()) && !tableDefinition.containsKey(property.getKey())) {
        //    continue;
        //}

        if (fromServer && !tableDefinition.containsKey(property.getKey().toLowerCase())) {
            continue;
        }

        String invColumnName = normalizeColumnName(property.getKey());
        sql.append(delimiter);
        sql.append("\"");
        sql.append(invColumnName);
        sql.append("\"");
        delimiter = ",";

        columnsOnStatement++;
    }

    if (columnsOnStatement == 0) {
        result.sql = "";
        result.parameters = parameters;

        return result;
    }

    sql.append(") VALUES ");

    String prefix = "";

    for (JsonObject item : items) {
        sql.append(prefix);
        appendInsertValuesSql(sql, parameters, tableDefinition, item, fromServer);
        prefix = ",";
    }

    result.sql = sql.toString();
    result.parameters = parameters;

    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;
        }/*  w ww .jav a2  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.microsoft.windowsazure.mobileservices.table.sync.operations.RemoteTableOperationProcessor.java

License:Open Source License

private static EnumSet<MobileServiceSystemProperty> getSystemProperties(JsonObject instance) {
    EnumSet<MobileServiceSystemProperty> systemProperties = EnumSet.noneOf(MobileServiceSystemProperty.class);

    for (Entry<String, JsonElement> property : instance.entrySet()) {
        String propertyName = property.getKey().trim().toLowerCase(Locale.getDefault());

        switch (propertyName) {
        case "__createdat":
            systemProperties.add(MobileServiceSystemProperty.CreatedAt);
            break;
        case "__updatedat":
            systemProperties.add(MobileServiceSystemProperty.UpdatedAt);
            break;
        case "__version":
            systemProperties.add(MobileServiceSystemProperty.Version);
            break;
        default://  w  ww  .j  av a2 s . com
            break;
        }
    }

    return systemProperties;
}

From source file:com.microsoft.windowsazure.mobileservices.table.sync.operations.RemoteTableOperationProcessor.java

License:Open Source License

private static JsonObject removeSystemProperties(JsonObject instance) {
    boolean haveCloned = false;

    for (Entry<String, JsonElement> property : instance.entrySet()) {
        if (property.getKey().startsWith("__")) {
            if (!haveCloned) {
                instance = (JsonObject) new JsonParser().parse(instance.toString());
                haveCloned = true;//w w w .j  a v a2 s .  c  o  m
            }

            instance.remove(property.getKey());
        }
    }

    return instance;
}

From source file:com.microsoft.windowsazure.mobileservices.zumoe2etestapp.framework.Util.java

License:Open Source License

public static boolean compareJson(JsonElement e1, JsonElement e2) {
    // NOTE: if every property defined in e1 is in e2, the objects are
    // considered equal.

    if (e1 == null && e2 == null) {
        return true;
    }/*from   ww  w. java 2s.  co m*/

    if (e1 == null || e2 == null) {
        return false;
    }

    if (e1.getClass() != e2.getClass()) {
        return false;
    }

    if (e1 instanceof JsonPrimitive) {
        if (!e1.equals(e2)) {
            return false;
        }
    } else if (e1 instanceof JsonArray) {
        JsonArray a1 = (JsonArray) e1;
        JsonArray a2 = (JsonArray) e2;

        if (a1.size() != a2.size()) {
            return false;
        }

        for (int i = 0; i < a1.size(); i++) {
            if (!compareJson(a1.get(i), a2.get(i))) {
                return false;
            }
        }

    } else if (e1 instanceof JsonObject) {

        JsonObject o1 = (JsonObject) e1;
        JsonObject o2 = (JsonObject) e2;

        Set<Entry<String, JsonElement>> entrySet1 = o1.entrySet();

        for (Entry<String, JsonElement> entry : entrySet1) {
            if (entry.getKey().toLowerCase(Locale.getDefault()).equals("id")) {
                continue;
            }

            String propertyName1 = entry.getKey();
            String propertyName2 = null;
            for (Entry<String, JsonElement> entry2 : o2.entrySet()) {
                if (propertyName1.toLowerCase(Locale.getDefault())
                        .equals(entry2.getKey().toLowerCase(Locale.getDefault()))) {
                    propertyName2 = entry2.getKey();
                }
            }

            if (propertyName2 == null) {
                return false;
            }

            if (!compareJson(entry.getValue(), o2.get(propertyName2))) {
                return false;
            }
        }
    }

    return true;
}

From source file:com.microsoft.windowsazure.mobileservices.zumoe2etestapp.push.GCMMessageHelper.java

License:Open Source License

public static GCMMessageCallback getPushCallback(final TestCase test, final JsonObject expectedPayload,
        final TestExecutionCallback callback) {
    return new GCMMessageCallback() {

        @Override/*  w w w.j  a  v  a2  s.  c o m*/
        public void timeoutElapsed() {

            test.log("Did not receive push message on time, test failed");
            TestResult testResult = new TestResult();
            testResult.setTestCase(test);
            testResult.setStatus(TestStatus.Failed);
            callback.onTestComplete(test, testResult);
        }

        @Override
        public void pushMessageReceived(Intent intent) {
            test.log("Received push message: " + intent.toString());
            TestResult testResult = new TestResult();
            testResult.setTestCase(test);
            testResult.setStatus(TestStatus.Passed);

            Set<Entry<String, JsonElement>> payloadEntries = expectedPayload.entrySet();

            for (Entry<String, JsonElement> entry : payloadEntries) {
                String key = entry.getKey();
                String value = entry.getValue().getAsString();
                String intentExtra = intent.getStringExtra(key);

                if (value.equals(intentExtra)) {
                    test.log("Retrieved correct value for key " + key);
                } else {
                    test.log("Error retrieving value for key " + key + ". Expected: " + value + "; actual: "
                            + intentExtra);
                    testResult.setStatus(TestStatus.Failed);
                }
            }

            callback.onTestComplete(test, testResult);
        }
    };
}

From source file:com.microsoftopentechnologies.auth.jwt.JWTClaimsSet.java

License:Apache License

public static JWTClaimsSet parse(JsonObject claims) {
    JWTClaimsSet claimsSet = new JWTClaimsSet();

    // initialize standard claims
    claimsSet.setIssuer(JsonUtils.getJsonStringProp(claims, "iss"));
    claimsSet.setSubject(JsonUtils.getJsonStringProp(claims, "sub"));
    claimsSet.setAudience(JsonUtils.getJsonStringProp(claims, "aud"));
    claimsSet.setExpirationTime(new Date(JsonUtils.getJsonLongProp(claims, "exp") * 1000));
    claimsSet.setNotBeforeTime(new Date(JsonUtils.getJsonLongProp(claims, "nbf") * 1000));
    claimsSet.setIssueTime(new Date(JsonUtils.getJsonLongProp(claims, "iat") * 1000));
    claimsSet.setJwtID(JsonUtils.getJsonStringProp(claims, "jti"));

    // initialize custom claims
    Map<String, JsonElement> customClaims = claimsSet.getCustomClaims();
    for (Map.Entry<String, JsonElement> entry : claims.entrySet()) {
        if (!Iterables.contains(standardClaims, entry.getKey())) {
            customClaims.put(entry.getKey(), entry.getValue());
        }//from   w  w  w . ja v  a 2s.co  m
    }

    return claimsSet;
}

From source file:com.mikenhill.java.json.JavaGSON.java

private void processJSONObject(JsonObject obj) throws Exception {

    System.out.println("JsonObject -> " + obj);

    Set<Map.Entry<String, JsonElement>> entrySet = obj.entrySet();
    for (Map.Entry<String, JsonElement> entry : entrySet) {
        //System.out.println ("entry -> " + entry);
        JsonElement element = entry.getValue();
        if (element instanceof JsonPrimitive) {

            processJSONPrimitive((JsonPrimitive) element);

        } else if (element instanceof JsonArray) {

            processJSONArray((JsonArray) element);

        } else if (element instanceof JsonObject) {

            processJSONObject((JsonObject) element);

        } else {/*  w ww.j  a  v a2  s. co  m*/
            System.out.println("Unknown -> " + element);
        }
        int gg = 0;
    }

    //Get the first address city value

    //Iterate over all objects
    //        for (Object o : parentObject.) {
    //            //Look at each type
    //            if (o instanceof JsonElement) {
    //                System.out.println ("0 -> " + o);
    //            }
    //        }

    //Now select by name
    //JsonElement addressObj = parentObject.get("address");
    //System.out.println (addressObj.  "address -> " + addressObj);

    //System.out.println ("1 -> " + parentObject);
}