Search in sources :

Example 41 with JsonPrimitive

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

the class ExpectedSubtypesAdapterTest method readSubtype.

@Test
public void readSubtype() {
    Gson gson = new Gson();
    ExpectedSubtypesAdapter<Object> subtypesAdaper = ExpectedSubtypesAdapter.create(gson, objectType, longType, booleanType);
    check(subtypesAdaper.fromJsonTree(new JsonPrimitive(true))).is(true);
    check(subtypesAdaper.fromJsonTree(new JsonPrimitive(111))).is(111L);
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) Gson(com.google.gson.Gson) Test(org.junit.Test)

Example 42 with JsonPrimitive

use of com.google.gson.JsonPrimitive 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 43 with JsonPrimitive

use of com.google.gson.JsonPrimitive in project play-cookbook by spinscale.

the class SearchHelperPlugin method getJsonStatus.

@Override
public JsonObject getJsonStatus() {
    JsonObject obj = new JsonObject();
    List<ApplicationClass> classes = Play.classes.getAssignableClasses(IndexedModel.class);
    for (ApplicationClass applicationClass : classes) {
        if (isIndexed(applicationClass)) {
            List<String> fieldList = getIndexedFields(applicationClass);
            JsonArray fields = new JsonArray();
            for (String field : fieldList) {
                fields.add(new JsonPrimitive(field));
            }
            obj.add(applicationClass.name, fields);
        }
    }
    return obj;
}
Also used : JsonArray(com.google.gson.JsonArray) JsonPrimitive(com.google.gson.JsonPrimitive) JsonObject(com.google.gson.JsonObject) ApplicationClass(play.classloading.ApplicationClasses.ApplicationClass)

Example 44 with JsonPrimitive

use of com.google.gson.JsonPrimitive in project disunity by ata4.

the class JsonTablePrinter method tableToJson.

private JsonArray tableToJson(Table<Integer, Integer, Object> table, Gson gson) {
    JsonArray jsonTable = new JsonArray();
    table.rowMap().forEach((rk, r) -> {
        if (rk == 0) {
            return;
        }
        JsonObject jsonRow = new JsonObject();
        table.columnMap().forEach((ck, c) -> {
            String key = String.valueOf(table.get(0, ck)).toLowerCase();
            Object value = table.get(rk, ck);
            jsonRow.add(key, gson.toJsonTree(value));
        });
        jsonTable.add(jsonRow);
    });
    JsonObject jsonRoot = new JsonObject();
    if (file != null) {
        jsonRoot.add("file", new JsonPrimitive(file.toString()));
    }
    return jsonTable;
}
Also used : JsonArray(com.google.gson.JsonArray) JsonPrimitive(com.google.gson.JsonPrimitive) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject)

Example 45 with JsonPrimitive

use of com.google.gson.JsonPrimitive in project disunity by ata4.

the class JsonTablePrinter method print.

@Override
public void print(Collection<TableModel> models) {
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    JsonObject jsonRoot = new JsonObject();
    if (file != null) {
        jsonRoot.add("file", new JsonPrimitive(file.toString()));
    }
    models.forEach(model -> {
        jsonRoot.add(model.name().toLowerCase(), tableToJson(model.table(), gson));
    });
    gson.toJson(jsonRoot, out);
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) JsonPrimitive(com.google.gson.JsonPrimitive) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject)

Aggregations

JsonPrimitive (com.google.gson.JsonPrimitive)168 JsonArray (com.google.gson.JsonArray)103 JsonObject (com.google.gson.JsonObject)78 Test (org.testng.annotations.Test)56 JsonElement (com.google.gson.JsonElement)47 Test (org.junit.Test)12 Map (java.util.Map)9 Matchers.anyString (org.mockito.Matchers.anyString)8 JsonProcessorInjectionMap (com.builtbroken.mc.lib.json.loading.JsonProcessorInjectionMap)7 Gson (com.google.gson.Gson)5 JsonParser (com.google.gson.JsonParser)4 Date (java.util.Date)4 HashMap (java.util.HashMap)4 GsonBuilder (com.google.gson.GsonBuilder)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 Matcher (java.util.regex.Matcher)3 LobWrapper (angularBeans.io.LobWrapper)2 DatasetCreationSpec (co.cask.cdap.internal.dataset.DatasetCreationSpec)2 IRenderState (com.builtbroken.mc.client.json.imp.IRenderState)2