Example usage for com.google.gson JsonObject get

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

Introduction

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

Prototype

public JsonElement get(String memberName) 

Source Link

Document

Returns the member with the specified name.

Usage

From source file:angularBeans.remote.RealTimeInvoker.java

License:Open Source License

public void process(@Observes @DataReceivedEvent RealTimeDataReceivedEvent event) {

    JsonObject jObj = event.getData();
    String UID = event.getSessionId();
    String beanName = jObj.get("service").getAsString();
    String method = jObj.get("method").getAsString();
    long reqId = jObj.get("reqId").getAsLong();
    JsonObject paramsObj = jObj.get("params").getAsJsonObject();

    NGSessionScopeContext.setCurrentContext(UID);

    if (reqId == 0) {
        return;/*from ww w.ja v  a2 s  . com*/
    }

    Object bean = locator.lookup(beanName, UID);

    remoteInvoker.realTimeInvoke(bean, method, paramsObj, event, reqId, UID);

}

From source file:aopdomotics.storage.food.FoodJsonDecoder.java

/**
 * Decode bill, using storage array(with prices) and bill json and return total price.
 * @param billJson/*from  w  w w. j av  a2 s  .c om*/
 * @param magazinList
 * @return 
 */
public static float decodeBill(String billJson, ArrayList<Food> magazinList) {

    JsonElement jelement = new JsonParser().parse(billJson);
    JsonObject json = jelement.getAsJsonObject();

    JsonElement billElement = json.get("Bill");
    System.out.println("Bill JSON " + billElement.toString());

    Gson gson = new GsonBuilder().create();
    FoodJsonDecoder billdecoder = gson.fromJson(billElement, FoodJsonDecoder.class);

    return billdecoder.billPrice(magazinList);
}

From source file:aopdomotics.storage.food.FoodJsonDecoder.java

/**
 * Decode recipe using the three food items from json recipe.
 * @param recipeJson/*  w w  w .ja v  a  2 s . c o  m*/
 * @return 
 */
public static Recipe decodeRecipe(String recipeJson) {
    Recipe recipe;
    Food[] component = new Food[3];
    JsonElement jelement = new JsonParser().parse(recipeJson);
    JsonObject json = jelement.getAsJsonObject();

    JsonElement recipeElement = json.get("Recipe");
    System.out.println("Recipe JSON " + recipeElement.toString());

    Gson gson = new GsonBuilder().create();
    FoodJsonDecoder recipedecoder = gson.fromJson(recipeElement, FoodJsonDecoder.class);
    int i = 0;
    for (Food item : recipedecoder.foodList()) {
        if (item.quantity > 0) {
            component[i++] = item;
        }
    }
    recipe = new Recipe("eaten", component[0], component[1], component[2]);
    return recipe;
}

From source file:aopdomotics.storage.food.FoodJsonDecoder.java

/**
 * Decode storage update json, return entire catalogue list
 * @param storageJson// w  w w.  ja va  2s  . com
 * @return 
 */
public static ArrayList<Food> decodeStorage(String storageJson) {
    System.out.println("Storage Json :  " + storageJson);
    JsonElement jelement = new JsonParser().parse(storageJson);
    JsonObject json = jelement.getAsJsonObject();

    JsonElement storageElement = json.get("Storage");
    System.out.println("Storage JSON " + storageElement.toString());

    Gson gson = new GsonBuilder().create();
    FoodJsonDecoder recipedecoder = gson.fromJson(storageElement, FoodJsonDecoder.class);
    return recipedecoder.foodList();
}

From source file:app.abhijit.iter.data.source.StudentDataSource.java

License:Open Source License

public void fetch() {
    String selectedRegistrationNumber = mLocalStore.getString(SELECTED_REGISTRATION_NUMBER_KEY, null);
    if (!StringUtils.equals(selectedRegistrationNumber, mSelectedRegistrationNumber)) {
        if (mFetchStudentAsyncTask != null) {
            mFetchStudentAsyncTask.cancel(true);
            mFetchStudentAsyncTask = null;
        }// w w  w  .j  a va  2  s .c o  m
        if (mFetchStudentSubjectsAsyncTask != null) {
            mFetchStudentSubjectsAsyncTask.cancel(true);
            mFetchStudentSubjectsAsyncTask = null;
        }
        mSelectedRegistrationNumber = selectedRegistrationNumber;
    }

    if (mSelectedRegistrationNumber != null && mErrorMessage == null && mFetchStudentAsyncTask == null
            && mFetchStudentSubjectsAsyncTask == null) {
        if (!mLocalStore.contains(mSelectedRegistrationNumber)) {
            JsonObject student = new JsonObject();
            student.addProperty(STUDENT_REGISTRATION_NUMBER_KEY, mSelectedRegistrationNumber);
            mLocalStore.edit().putString(mSelectedRegistrationNumber, student.toString()).apply();
            mFetchStudentAsyncTask = new FetchStudentAsyncTask();
            mFetchStudentAsyncTask.execute();
        } else {
            JsonObject student = mJsonParser.parse(mLocalStore.getString(mSelectedRegistrationNumber, null))
                    .getAsJsonObject();
            if (!student.has(STUDENT_NAME_KEY)) {
                mFetchStudentAsyncTask = new FetchStudentAsyncTask();
                mFetchStudentAsyncTask.execute();
            } else {
                boolean isSubjectDataOld = (new Date().getTime()
                        - student.get(STUDENT_TIMESTAMP_KEY).getAsLong()) > CACHE_TIMEOUT;
                if (isSubjectDataOld || !student.has(STUDENT_SUBJECTS_KEY)) {
                    String studentId = student.get(STUDENT_ID_KEY).getAsString();
                    mFetchStudentSubjectsAsyncTask = new FetchStudentSubjectsAsyncTask();
                    mFetchStudentSubjectsAsyncTask.execute(studentId);
                }
            }
        }
    }

    if (mErrorMessage != null) {
        EventBus.getDefault().post(new Error(mErrorMessage));
        mErrorMessage = null;
    }

    ArrayList<Student> students = new ArrayList<>();

    for (Map.Entry<String, ?> entry : mLocalStore.getAll().entrySet()) {
        if (entry.getKey().equals(SELECTED_REGISTRATION_NUMBER_KEY))
            continue;
        JsonObject student = mJsonParser.parse(mLocalStore.getString(entry.getKey(), null)).getAsJsonObject();
        students.add(generateStudent(student));
    }

    EventBus.getDefault().post(ImmutableList.copyOf(students));
}

From source file:app.abhijit.iter.data.source.StudentDataSource.java

License:Open Source License

private void processStudent(String[] response) {
    mFetchStudentAsyncTask = null;//from   w w  w  .  j  a v a  2s . com

    if (response[0] == null || response[1] == null) {
        mErrorMessage = "Could not load registration number " + mSelectedRegistrationNumber;
        delete(mSelectedRegistrationNumber);
        fetch();
        return;
    }

    String studentId = response[0];

    if (studentId.equals("0")) {
        mErrorMessage = "Registration number " + mSelectedRegistrationNumber + " is not valid";
        delete(mSelectedRegistrationNumber);
        fetch();
        return;
    }

    JsonObject student = new JsonObject();

    try {
        JsonObject studentDetails = mJsonParser.parse(response[1]).getAsJsonArray().get(0).getAsJsonObject();
        if (!studentDetails.has("name")) {
            throw new Exception();
        }
        student.addProperty(STUDENT_ID_KEY, studentId);
        student.addProperty(STUDENT_NAME_KEY, studentDetails.get("name").getAsString());
        student.addProperty(STUDENT_REGISTRATION_NUMBER_KEY, mSelectedRegistrationNumber);
        student.addProperty(STUDENT_TIMESTAMP_KEY, new Date().getTime());
    } catch (Exception e) {
        mErrorMessage = "Could not load registration number " + mSelectedRegistrationNumber;
        delete(mSelectedRegistrationNumber);
        fetch();
        return;
    }

    mLocalStore.edit().putString(mSelectedRegistrationNumber, student.toString()).apply();

    fetch();
}

From source file:app.abhijit.iter.data.source.StudentDataSource.java

License:Open Source License

private void processStudentSubjects(String response) {
    mFetchStudentSubjectsAsyncTask = null;

    JsonObject student = mJsonParser.parse(mLocalStore.getString(mSelectedRegistrationNumber, null))
            .getAsJsonObject();//from   ww w.j a va 2  s. c o m
    JsonObject subjects = new JsonObject();

    try {
        JsonArray studentSubjects = mJsonParser.parse(response).getAsJsonArray();
        for (int i = 0; i < studentSubjects.size(); i++) {
            if (!studentSubjects.get(i).getAsJsonObject().has("subjectcode")
                    || !studentSubjects.get(i).getAsJsonObject().has("subject")
                    || !studentSubjects.get(i).getAsJsonObject().has("totalpresentclass")
                    || !studentSubjects.get(i).getAsJsonObject().has("totalclasses")) {
                continue;
            }
            String code = studentSubjects.get(i).getAsJsonObject().get("subjectcode").getAsString();
            String name = studentSubjects.get(i).getAsJsonObject().get("subject").getAsString();
            int presentClasses = studentSubjects.get(i).getAsJsonObject().get("totalpresentclass").getAsInt();
            int totalClasses = studentSubjects.get(i).getAsJsonObject().get("totalclasses").getAsInt();
            if (totalClasses <= 0 || presentClasses < 0 || presentClasses > totalClasses)
                continue;
            long lastUpdated = new Date().getTime();
            int oldPresentClasses = presentClasses;
            int oldTotalClasses = totalClasses;
            if (student.has(STUDENT_SUBJECTS_KEY)
                    && student.get(STUDENT_SUBJECTS_KEY).getAsJsonObject().has(code)) {
                oldPresentClasses = student.get(STUDENT_SUBJECTS_KEY).getAsJsonObject().get(code)
                        .getAsJsonObject().get(STUDENT_SUBJECT_PRESENT_CLASSES_KEY).getAsInt();
                oldTotalClasses = student.get(STUDENT_SUBJECTS_KEY).getAsJsonObject().get(code)
                        .getAsJsonObject().get(STUDENT_SUBJECT_TOTAL_CLASSES_KEY).getAsInt();
                if (presentClasses == oldPresentClasses && totalClasses == oldTotalClasses) {
                    lastUpdated = student.get(STUDENT_SUBJECTS_KEY).getAsJsonObject().get(code)
                            .getAsJsonObject().get(STUDENT_SUBJECT_LAST_UPDATED_KEY).getAsLong();
                }
            }
            JsonObject subject = new JsonObject();
            subject.addProperty(STUDENT_SUBJECT_CODE_KEY, code);
            subject.addProperty(STUDENT_SUBJECT_NAME_KEY, name);
            subject.addProperty(STUDENT_SUBJECT_PRESENT_CLASSES_KEY, presentClasses);
            subject.addProperty(STUDENT_SUBJECT_TOTAL_CLASSES_KEY, totalClasses);
            subject.addProperty(STUDENT_SUBJECT_OLD_PRESENT_CLASSES_KEY, oldPresentClasses);
            subject.addProperty(STUDENT_SUBJECT_OLD_TOTAL_CLASSES_KEY, oldTotalClasses);
            subject.addProperty(STUDENT_SUBJECT_LAST_UPDATED_KEY, lastUpdated);
            subjects.add(code, subject);
        }
    } catch (Exception e) {
        if (student.has(STUDENT_SUBJECTS_KEY)) {
            subjects = student.get(STUDENT_SUBJECTS_KEY).getAsJsonObject();
        } else {
            subjects = new JsonObject();
        }
    }

    student.add(STUDENT_SUBJECTS_KEY, subjects);
    student.addProperty(STUDENT_TIMESTAMP_KEY, new Date().getTime());
    mLocalStore.edit().putString(mSelectedRegistrationNumber, student.toString()).apply();

    fetch();
}

From source file:app.abhijit.iter.data.source.StudentDataSource.java

License:Open Source License

private Student generateStudent(JsonObject student) {
    String registrationNumber = student.get(STUDENT_REGISTRATION_NUMBER_KEY).getAsString();
    String name = null;//w ww  .  j  a v a 2 s  .  c  o  m
    if (student.has(STUDENT_NAME_KEY)) {
        name = WordUtils.capitalizeFully(student.get(STUDENT_NAME_KEY).getAsString());
    }
    boolean selected = StringUtils.equals(registrationNumber, mSelectedRegistrationNumber);

    ArrayList<Subject> subjects = new ArrayList<>();

    if (StringUtils.equals(mSelectedRegistrationNumber, registrationNumber)
            && mFetchStudentSubjectsAsyncTask == null && student.has(STUDENT_SUBJECTS_KEY)) {
        for (Map.Entry<String, ?> entry : student.get(STUDENT_SUBJECTS_KEY).getAsJsonObject().entrySet()) {
            JsonObject subject = student.get(STUDENT_SUBJECTS_KEY).getAsJsonObject().get(entry.getKey())
                    .getAsJsonObject();
            subjects.add(generateStudentSubject(subject));
            removeOldSubjectData(registrationNumber, student, subject);
        }
    } else {
        subjects = null;
    }

    return new Student(name, registrationNumber, selected,
            subjects == null ? null : ImmutableList.copyOf(subjects));
}

From source file:app.abhijit.iter.data.source.StudentDataSource.java

License:Open Source License

private Subject generateStudentSubject(JsonObject subject) {
    String name = subject.get(STUDENT_SUBJECT_NAME_KEY).getAsString();
    String code = subject.get(STUDENT_SUBJECT_CODE_KEY).getAsString();
    int presentClasses = subject.get(STUDENT_SUBJECT_PRESENT_CLASSES_KEY).getAsInt();
    int oldPresentClasses = subject.get(STUDENT_SUBJECT_OLD_PRESENT_CLASSES_KEY).getAsInt();
    int totalClasses = subject.get(STUDENT_SUBJECT_TOTAL_CLASSES_KEY).getAsInt();
    int oldTotalClasses = subject.get(STUDENT_SUBJECT_OLD_TOTAL_CLASSES_KEY).getAsInt();
    long lastUpdated = subject.get(STUDENT_SUBJECT_LAST_UPDATED_KEY).getAsLong();

    float attendance = (presentClasses / (float) totalClasses) * 100;
    float oldAttendance = (oldPresentClasses / (float) oldTotalClasses) * 100;

    int absentClasses = totalClasses - presentClasses;
    int oldAbsentClasses = oldTotalClasses - oldPresentClasses;

    int status;/*from w w  w  .j a va 2 s .c  om*/
    if (attendance == oldAttendance) {
        if (attendance > 85) {
            status = R.drawable.ic_status_ok;
        } else if (attendance > 75) {
            status = R.drawable.ic_status_warning;
        } else {
            status = R.drawable.ic_status_critical;
        }
    } else {
        if (attendance > oldAttendance) {
            status = R.drawable.ic_status_up;
        } else {
            status = R.drawable.ic_status_down;
        }
    }

    int subjectAvatar = mSubjectAvatars.get("generic");
    if (mSubjectAvatars.containsKey(code)) {
        subjectAvatar = mSubjectAvatars.get(code);
    } else if (code.length() >= 3 && mSubjectAvatars.containsKey(code.substring(0, 3))) {
        subjectAvatar = mSubjectAvatars.get(code.substring(0, 3));
    }

    return new Subject(subjectAvatar, name,
            attendance == oldAttendance ? null : String.format(Locale.US, "%.2f", oldAttendance) + "%",
            String.format(Locale.US, "%.2f", attendance) + "%", status,
            DateUtils.getRelativeTimeSpanString(lastUpdated, new Date().getTime(), 0).toString(),
            presentClasses == oldPresentClasses && totalClasses == oldTotalClasses ? null
                    : oldPresentClasses + "/" + oldTotalClasses
                            + (oldTotalClasses == 1 ? " class" : " classes"),
            presentClasses + "/" + totalClasses + (totalClasses == 1 ? " class" : " classes"),
            absentClasses == oldAbsentClasses ? null
                    : oldAbsentClasses + (oldAbsentClasses == 1 ? " class" : " classes"),
            absentClasses + (absentClasses == 1 ? " class" : " classes"),
            generateBunkStats((int) attendance, totalClasses, presentClasses, absentClasses));
}

From source file:app.abhijit.iter.data.source.StudentDataSource.java

License:Open Source License

private void removeOldSubjectData(String registrationNumber, JsonObject student, JsonObject subject) {
    String code = subject.get(STUDENT_SUBJECT_CODE_KEY).getAsString();
    int presentClasses = subject.get(STUDENT_SUBJECT_PRESENT_CLASSES_KEY).getAsInt();
    int totalClasses = subject.get(STUDENT_SUBJECT_TOTAL_CLASSES_KEY).getAsInt();
    subject.addProperty(STUDENT_SUBJECT_OLD_PRESENT_CLASSES_KEY, presentClasses);
    subject.addProperty(STUDENT_SUBJECT_OLD_TOTAL_CLASSES_KEY, totalClasses);
    student.get(STUDENT_SUBJECTS_KEY).getAsJsonObject().add(code, subject);
    mLocalStore.edit().putString(registrationNumber, student.toString()).apply();
}