Search in sources :

Example 41 with JsonArray

use of com.google.gson.JsonArray in project JsonPath by jayway.

the class GsonJsonProvider method toIterable.

@Override
public Iterable<?> toIterable(final Object obj) {
    JsonArray arr = toJsonArray(obj);
    List<Object> values = new ArrayList<Object>(arr.size());
    for (Object o : arr) {
        values.add(unwrap(o));
    }
    return values;
}
Also used : JsonArray(com.google.gson.JsonArray) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject)

Example 42 with JsonArray

use of com.google.gson.JsonArray in project immutables by immutables.

the class ExpectedSubtypesAdapterTest method failSubtype.

@Test(expected = JsonParseException.class)
public void failSubtype() {
    ExpectedSubtypesAdapter<Object> subtypesAdaper = ExpectedSubtypesAdapter.create(gson, objectType, longType, booleanType);
    subtypesAdaper.fromJsonTree(new JsonArray());
}
Also used : JsonArray(com.google.gson.JsonArray) Test(org.junit.Test)

Example 43 with JsonArray

use of com.google.gson.JsonArray in project PushSms by koush.

the class MiddlewareService method createRegistration.

// fetch/create the gcm and public key info for a phone number
// from the server
private RegistrationFuture createRegistration(final String address, final Registration existing) {
    final RegistrationFuture ret = new RegistrationFuture();
    numberToRegistration.put(address, ret);
    // the server will need to know all the email/number combos when we're attempting
    // to locate the gcm registration id for a given number.
    // this will return HASHED emails, not actual emails. this way the server is not privy
    // to your contact information.
    HashSet<String> emailHash = Helper.getEmailHashesForNumber(this, address);
    if (emailHash.size() == 0) {
        ret.setComplete(new Exception("no emails"));
        return ret;
    }
    JsonObject post = new JsonObject();
    JsonArray authorities = new JsonArray();
    post.add("authorities", authorities);
    post.addProperty("endpoint", address);
    for (String authority : emailHash) {
        authorities.add(new JsonPrimitive(authority));
    }
    logd("Fetching registration for " + address);
    Ion.with(this).load(FIND_URL).setJsonObjectBody(post).asJsonObject().setCallback(new FutureCallback<JsonObject>() {

        @Override
        public void onCompleted(Exception e, JsonObject result) {
            Registration registration;
            boolean wasUnregistered = false;
            String oldRegistrationId = null;
            // from the old registration
            if (existing != null) {
                oldRegistrationId = existing.registrationId;
                wasUnregistered = existing.isUnregistered();
                // reuse the existing registration to preserve sequence numbers, etc.
                registration = existing;
                registration.register();
            } else {
                registration = new Registration();
            }
            try {
                if (e != null) {
                    // or lack of network access on the phone, etc.
                    throw e;
                }
                if (result.has("error"))
                    throw new Exception(result.toString());
                String newRegistrationId = result.get("registration_id").getAsString();
                // the number is available for an encrypted connection, grab
                // the registration info.
                registration.endpoint = address;
                registration.registrationId = newRegistrationId;
                BigInteger publicExponent = new BigInteger(Base64.decode(result.get("public_exponent").getAsString(), Base64.DEFAULT));
                BigInteger publicModulus = new BigInteger(Base64.decode(result.get("public_modulus").getAsString(), Base64.DEFAULT));
                RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(publicModulus, publicExponent);
                KeyFactory keyFactory = KeyFactory.getInstance("RSA");
                registration.remotePublicKey = keyFactory.generatePublic(publicKeySpec);
                logd("Registration complete for " + registration.endpoint);
                // gets hit.
                if (wasUnregistered && TextUtils.equals(newRegistrationId, oldRegistrationId))
                    throw new Exception("unregistered registration was refreshed, still invalid");
            } catch (Exception ex) {
                // mark this number as invalid
                Log.e(LOGTAG, "registration fetch failure", ex);
                registration.invalidate();
            }
            registry.register(address, registration);
            ret.setComplete(registration);
            // that will leverage the new registration id and potentially public key
            if (gcmConnectionManager != null)
                gcmConnectionManager.remove(address);
        }
    });
    return ret;
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) JsonObject(com.google.gson.JsonObject) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) RemoteException(android.os.RemoteException) IOException(java.io.IOException) JsonArray(com.google.gson.JsonArray) BigInteger(java.math.BigInteger) KeyFactory(java.security.KeyFactory)

Example 44 with JsonArray

use of com.google.gson.JsonArray in project PneumaticCraft by MineMaarten.

the class AmadronOfferConfig method writeToJson.

@Override
protected final void writeToJson(JsonObject json) {
    JsonArray array = new JsonArray();
    for (AmadronOffer offer : getOffers()) {
        array.add(offer.toJson());
    }
    json.addProperty("description", getComment());
    writeToJsonCustom(json);
    json.add("offers", array);
}
Also used : JsonArray(com.google.gson.JsonArray) AmadronOffer(pneumaticCraft.common.recipes.AmadronOffer)

Example 45 with JsonArray

use of com.google.gson.JsonArray in project PneumaticCraft by MineMaarten.

the class ProgWidgetConfig method writeToJson.

@Override
protected void writeToJson(JsonObject json) {
    json.addProperty("description", "In the 'blacklist' tag you can put the programming puzzle names that need to blacklisted from this instance. When they were used in existing programs already they will be deleted. A list of all programming puzzle names can be seen in 'allWidgets'.");
    JsonArray array = new JsonArray();
    List<String> names = new ArrayList<String>(WidgetRegistrator.getAllWidgetNames());
    Collections.sort(names);
    for (String name : names) {
        array.add(new JsonPrimitive(name));
    }
    json.add("allWidgets", array);
    array = new JsonArray();
    for (String name : blacklistedPieces) {
        array.add(new JsonPrimitive(name));
    }
    json.add("blacklist", array);
    WidgetRegistrator.compileBlacklist();
}
Also used : JsonArray(com.google.gson.JsonArray) JsonPrimitive(com.google.gson.JsonPrimitive) ArrayList(java.util.ArrayList)

Aggregations

JsonArray (com.google.gson.JsonArray)424 JsonObject (com.google.gson.JsonObject)290 JsonElement (com.google.gson.JsonElement)167 JsonPrimitive (com.google.gson.JsonPrimitive)118 Test (org.testng.annotations.Test)59 JsonParser (com.google.gson.JsonParser)57 ArrayList (java.util.ArrayList)50 HashMap (java.util.HashMap)34 Map (java.util.Map)34 Gson (com.google.gson.Gson)22 IOException (java.io.IOException)17 Test (org.junit.Test)16 Type (java.lang.reflect.Type)12 List (java.util.List)11 Matchers.anyString (org.mockito.Matchers.anyString)10 TextView (android.widget.TextView)9 Point (android.graphics.Point)8 JsonParseException (com.google.gson.JsonParseException)7 GsonUtilities.jboolean (com.ibm.streamsx.topology.internal.gson.GsonUtilities.jboolean)7 KcaUtils.getStringFromException (com.antest1.kcanotify.KcaUtils.getStringFromException)6