Search in sources :

Example 96 with JsonArray

use of com.google.gson.JsonArray in project YourAppIdea by Michenux.

the class LocationDeserializer method deserialize.

@Override
public Location deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
    JsonObject jsonObject = jsonElement.getAsJsonObject();
    Location location = new Location("mongodb");
    JsonArray coord = jsonObject.getAsJsonArray("coordinates");
    location.setLongitude(coord.get(0).getAsDouble());
    location.setLatitude(coord.get(1).getAsDouble());
    return location;
}
Also used : JsonArray(com.google.gson.JsonArray) JsonObject(com.google.gson.JsonObject) Location(android.location.Location)

Example 97 with JsonArray

use of com.google.gson.JsonArray in project jianshi by wingjay.

the class SyncManager method syncLog.

public synchronized void syncLog() {
    final List<EventLog> logItems = SQLite.select().from(EventLog.class).queryList();
    JsonObject syncLog = new JsonObject();
    JsonArray array = new JsonArray();
    for (EventLog eventLog : logItems) {
        array.add(new Gson().toJsonTree(eventLog));
    }
    syncLog.add("log_items", array);
    userService.syncLog(syncLog).subscribe(new Action1<JsonDataResponse<JsonObject>>() {

        @Override
        public void call(JsonDataResponse<JsonObject> jsonObjectJsonDataResponse) {
            if (jsonObjectJsonDataResponse.getRc() == Constants.ServerResultCode.RESULT_OK) {
                JsonObject object = jsonObjectJsonDataResponse.getData();
                int syncedCount = object.get("synced_count").getAsInt();
                Timber.i("synced log count %d", syncedCount);
                for (EventLog eventLog : logItems) {
                    SQLite.delete(EventLog.class).where(EventLog_Table.id.eq(eventLog.getId())).execute();
                    syncedCount--;
                    if (syncedCount <= 0) {
                        break;
                    }
                }
            }
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            Timber.e(throwable, "SyncLog failed");
        }
    });
}
Also used : JsonArray(com.google.gson.JsonArray) JsonDataResponse(com.wingjay.jianshi.network.JsonDataResponse) EventLog(com.wingjay.jianshi.db.model.EventLog) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson)

Example 98 with JsonArray

use of com.google.gson.JsonArray in project jianshi by wingjay.

the class SyncManager method sync.

public synchronized void sync(@Nullable final SyncResultListener syncResultListener) {
    final List<PushData> pushDataList = SQLite.select().from(PushData.class).queryList();
    JsonParser jsonParser = new JsonParser();
    JsonObject syncData = new JsonObject();
    JsonArray array = new JsonArray();
    for (PushData pushData : pushDataList) {
        array.add(jsonParser.parse(pushData.getData()));
    }
    syncData.add("sync_items", array);
    syncData.add("need_pull", gson.toJsonTree(1));
    syncData.add("sync_token", gson.toJsonTree(userPrefs.getSyncToken()));
    Timber.d("Sync Data : %s", syncData.toString());
    userService.sync(syncData).subscribe(new Action1<JsonDataResponse<SyncModel>>() {

        @Override
        public void call(JsonDataResponse<SyncModel> response) {
            if (response.getRc() == Constants.ServerResultCode.RESULT_OK) {
                SyncModel syncModel = response.getData();
                userPrefs.setSyncToken(syncModel.getSyncToken());
                if (syncModel.getUpsert() != null) {
                    for (Diary diary : syncModel.getUpsert()) {
                        diary.save();
                    }
                }
                if (syncModel.getDelete() != null) {
                    for (Diary diary : syncModel.getDelete()) {
                        diary.save();
                    }
                }
                int syncCount = syncModel.getSyncedCount();
                for (PushData pushData : pushDataList) {
                    SQLite.delete().from(PushData.class).where(PushData_Table.id.eq(pushData.getId())).execute();
                    syncCount--;
                    if (syncCount <= 0) {
                        break;
                    }
                }
            }
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            Timber.e(throwable, "Sync Failed");
        }
    });
    Observable.just(null).subscribeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Object>() {

        @Override
        public void call(Object o) {
            if (syncResultListener != null) {
                if (SQLite.select().from(PushData.class).queryList().size() > 0) {
                    syncResultListener.onFailure();
                } else {
                    syncResultListener.onSuccess();
                }
            }
        }
    });
}
Also used : SyncModel(com.wingjay.jianshi.bean.SyncModel) JsonDataResponse(com.wingjay.jianshi.network.JsonDataResponse) JsonObject(com.google.gson.JsonObject) PushData(com.wingjay.jianshi.db.model.PushData) Diary(com.wingjay.jianshi.db.model.Diary) JsonArray(com.google.gson.JsonArray) JsonObject(com.google.gson.JsonObject) JsonParser(com.google.gson.JsonParser)

Example 99 with JsonArray

use of com.google.gson.JsonArray in project intellij-plugins by JetBrains.

the class TypeHierarchyItem method toJson.

public JsonObject toJson() {
    JsonObject jsonObject = new JsonObject();
    jsonObject.add("classElement", classElement.toJson());
    if (displayName != null) {
        jsonObject.addProperty("displayName", displayName);
    }
    if (memberElement != null) {
        jsonObject.add("memberElement", memberElement.toJson());
    }
    if (superclass != null) {
        jsonObject.addProperty("superclass", superclass);
    }
    JsonArray jsonArrayInterfaces = new JsonArray();
    for (int elt : interfaces) {
        jsonArrayInterfaces.add(new JsonPrimitive(elt));
    }
    jsonObject.add("interfaces", jsonArrayInterfaces);
    JsonArray jsonArrayMixins = new JsonArray();
    for (int elt : mixins) {
        jsonArrayMixins.add(new JsonPrimitive(elt));
    }
    jsonObject.add("mixins", jsonArrayMixins);
    JsonArray jsonArraySubclasses = new JsonArray();
    for (int elt : subclasses) {
        jsonArraySubclasses.add(new JsonPrimitive(elt));
    }
    jsonObject.add("subclasses", jsonArraySubclasses);
    return jsonObject;
}
Also used : JsonArray(com.google.gson.JsonArray) JsonPrimitive(com.google.gson.JsonPrimitive) JsonObject(com.google.gson.JsonObject)

Example 100 with JsonArray

use of com.google.gson.JsonArray in project intellij-plugins by JetBrains.

the class OverrideMember method toJson.

public JsonObject toJson() {
    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("offset", offset);
    jsonObject.addProperty("length", length);
    if (superclassMember != null) {
        jsonObject.add("superclassMember", superclassMember.toJson());
    }
    if (interfaceMembers != null) {
        JsonArray jsonArrayInterfaceMembers = new JsonArray();
        for (OverriddenMember elt : interfaceMembers) {
            jsonArrayInterfaceMembers.add(elt.toJson());
        }
        jsonObject.add("interfaceMembers", jsonArrayInterfaceMembers);
    }
    return jsonObject;
}
Also used : JsonArray(com.google.gson.JsonArray) JsonObject(com.google.gson.JsonObject)

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