Example usage for com.google.gwt.json.client JSONParser parse

List of usage examples for com.google.gwt.json.client JSONParser parse

Introduction

In this page you can find the example usage for com.google.gwt.json.client JSONParser parse.

Prototype

@Deprecated
public static JSONValue parse(String jsonString) 

Source Link

Document

Evaluates a trusted JSON string and returns its JSONValue representation.

Usage

From source file:anzsoft.xmpp4gwt.client.User.java

License:Open Source License

public boolean resume() {
    String cookie = Cookies.getCookie(COOKIENAME);
    if (cookie == null || cookie.length() == 0)
        return false;
    JSONObject jUser = JSONParser.parse(cookie).isObject();
    if (jUser == null)
        return false;

    this.username = jUser.get(STORAGE_USERNAME).isString().stringValue();
    this.domainname = jUser.get(STORAGE_DOMAIN).isString().stringValue();
    //this.password = jUser.get("password").isString().stringValue();  
    //jUser.put("priority", new JSONString(String.valueOf(priority)));
    this.resource = jUser.get(STORAGE_RESOURCE).isString().stringValue();
    this.priority = Integer.parseInt(jUser.get(STORAGE_PRIORITY).isString().stringValue());
    Cookies.removeCookie(COOKIENAME);//from   w  ww  .  j  a v  a 2  s . c om
    if (username.length() == 0 || domainname.length() == 0)
        return false;
    return true;
}

From source file:anzsoft.xmpp4gwt.client.xmpp.presence.PresencePlugin.java

License:Open Source License

public void doResume() {
    try {/*w  ww  . j a v a 2s. c  o m*/
        final String prefix = Session.instance().getUser().getStorageID();
        Storage storage = Storage.createStorage(STORAGEKEY, prefix);
        if (storage == null)
            return;
        //get self presence
        String showStr = storage.get(STORAGE_CURRENTSHOW);
        if (showStr != null && !(showStr.length() == 0))
            currentShow = Show.valueOf(showStr);
        String priorityStr = storage.get(STORAGE_PRIORITY);
        if (priorityStr != null && !(priorityStr.length() == 0))
            priority = Integer.valueOf(priorityStr);
        String initStr = storage.get(STORAGE_INIT_PRESENCE);
        if (initStr != null)
            initialPresenceSended = initStr.equals("true");
        //get presence data
        String data = storage.get("data");
        if (data == null)
            return;
        JSONArray jPresences = JSONParser.parse(data).isArray();
        if (jPresences == null)
            return;
        for (int index = 0; index < jPresences.size(); index++) {
            String packetStr = jPresences.get(index).isString().stringValue();
            if (packetStr == null || packetStr.length() == 0)
                continue;
            final Packet pPacket = parse(packetStr);
            process(pPacket);
        }
    } catch (Exception e) {
        System.out.println(e.toString());
    }
}

From source file:anzsoft.xmpp4gwt.client.xmpp.roster.RosterPlugin.java

License:Open Source License

public boolean resume() {
    try {//from   ww  w  . j  a  v a2  s . c om
        final String prefix = Session.instance().getUser().getStorageID();
        Storage storage = Storage.createStorage(STORAGEKEY, prefix);
        String indexString = storage.get("index");
        if (indexString == null)
            return false;
        JSONArray jRoster = JSONParser.parse(indexString).isArray();
        if (jRoster == null)
            return false;
        rosterItemsByBareJid.clear();
        for (int index = 0; index < jRoster.size(); index++) {
            String jid = jRoster.get(index).isString().stringValue();
            if (jid == null || jid.length() == 0)
                continue;
            String objectString = storage.get(jid);
            if (objectString == null || objectString.length() == 0)
                continue;
            JSONObject object = JSONParser.parse(objectString).isObject();
            if (object == null)
                continue;
            RosterItem ri = new RosterItem();
            ri.fromJsonObject(object);
            rosterItemsByBareJid.put(ri.getJid(), ri);
        }
        this.rosterReceived = true;
        Iterator<RosterItem> iterator = this.rosterItemsByBareJid.values().iterator();
        while (iterator.hasNext()) {
            RosterItem ri = iterator.next();
            this.fireAddRosterItem(ri);
        }
    } catch (Exception e) {
        System.out.println(e.toString());
    }
    return true;
}

From source file:ca.upei.ic.timetable.client.CourseDetailController.java

License:Apache License

/**
 * Show the details of a course in a dialog box
 * //from w  w  w  .j  av  a  2  s . c o  m
 * @param courseId
 */
public void showCourseDetail(final int courseId) {

    Service.defaultInstance().cancelCourseDetailRequest();

    Service.defaultInstance().askCourseDetail(courseId, new RequestCallback() {

        public void onError(Request request, Throwable exception) {
            app_.error("Cannot get course details for course " + courseId, exception);
        }

        public void onResponseReceived(Request request, Response response) {
            try {
                String result = response.getText();
                JSONValue value = JSONParser.parse(result);
                try {
                    getView().loadJSON(value);
                    getView().showAt(265, 28);
                } catch (Exception ex) {
                    // do nothing
                }
            } catch (Exception ex) {
                app_.error("Cannot get course details for course " + courseId, ex);
            }
        }

    });
}

From source file:ca.upei.ic.timetable.client.CourseViewController.java

License:Apache License

public void search() {
    JSONObject department = new JSONObject();
    for (String d : departmentCriteria_.keySet()) {
        if (departmentCriteria_.get(d)) {
            department.put(d, JSONBoolean.getInstance(true));
        }/*from w  w w.ja  v a 2  s  .  c o m*/
    }
    JSONObject level = new JSONObject();
    for (String l : levelCriteria_.keySet()) {
        if (levelCriteria_.get(l)) {
            level.put(l, JSONBoolean.getInstance(true));
        }
    }
    JSONObject semester = new JSONObject();
    for (String s : semesterCriteria_.keySet()) {
        if (semesterCriteria_.get(s))
            semester.put(s, JSONBoolean.getInstance(true));
    }

    JSONObject value = new JSONObject();
    value.put("department", department);
    value.put("level", level);
    value.put("semester", semester);
    value.put("day", new JSONString(courseDay_));
    value.put("startTime", new JSONString(courseStartTime_));

    Service.defaultInstance().askCourses(value.toString(), new RequestCallback() {

        public void onError(Request request, Throwable exception) {
            app_.error(ApplicationController.OOPS, exception);
        }

        public void onResponseReceived(Request request, Response response) {

            try {
                JSONArray parsedArray = (JSONArray) JSONParser.parse(response.getText());
                // remove the selected courses
                // filtered array
                JSONArray filteredArray = new JSONArray();
                int filtered_index = 0;
                for (int i = 0; i < parsedArray.size(); i++) {
                    JSONObject o = (JSONObject) parsedArray.get(i);
                    int id = (int) ((JSONNumber) o.get("id")).doubleValue();
                    if (!isSelected(id)) {
                        filteredArray.set(filtered_index++, o);
                    }
                }
                // clear out the view so we can add courses
                view_.clear();
                // add the selected values first
                for (int index : selectionIndexes_) {
                    view_.addCourse(index, selectionValues_.get(index), true);
                }
                // add the remaining courses
                view_.loadJSON(filteredArray);
            } catch (Throwable e) {
                app_.error("Error: " + response.getText(), e);
            }
        }

    });
}

From source file:ca.upei.ic.timetable.client.FindCourseViewController.java

License:Apache License

/**
 * Get the department model//from   www  . j a v a  2 s  .  c  om
 * 
 * @return the department model
 */
public DepartmentModelView getDepartmentModel() {
    if (!departmentLoaded_) {
        Service.defaultInstance().askDepartments(new RequestCallback() {

            public void onError(Request request, Throwable exception) {
                app_.error(ApplicationController.OOPS, exception);
            }

            public void onResponseReceived(Request request, Response response) {
                JSONValue value = JSONParser.parse(response.getText());
                department_.loadJSON(value);
                departmentLoaded_ = true;
            }

        });
    }

    return department_;
}

From source file:ca.upei.ic.timetable.client.FindCourseViewController.java

License:Apache License

/**
 * Get the level model/*from   w  w  w  .  j  a va  2  s  . co m*/
 * 
 * @return the level model
 */
public LevelModelView getLevelModel() {
    if (!levelLoaded_) {
        Service.defaultInstance().askCourseLevels(new RequestCallback() {

            public void onError(Request request, Throwable exception) {
                app_.error(ApplicationController.OOPS, exception);
            }

            public void onResponseReceived(Request request, Response response) {
                JSONValue value = JSONParser.parse(response.getText());
                level_.loadJSON(value);
                levelLoaded_ = true;
            }

        });
    }

    return level_;
}

From source file:ca.upei.ic.timetable.client.FindCourseViewController.java

License:Apache License

public SemesterModelView getSemesterModel() {
    if (!semesterLoaded_) {
        Service.defaultInstance().askSemesters(new RequestCallback() {

            public void onError(Request request, Throwable exception) {
                app_.error(ApplicationController.OOPS, exception);
            }/*ww  w  .ja v a 2 s .  c o m*/

            public void onResponseReceived(Request request, Response response) {
                try {
                    String text = response.getText();
                    JSONValue value = JSONParser.parse(text);
                    semester_.loadJSON(value);
                    semesterLoaded_ = true;
                } catch (Exception e) {
                    app_.error("Error: " + response.getText(), e);
                }
            }

        });
    }

    return semester_;
}

From source file:cc.kune.core.client.sitebar.search.MultivalueSuggestBox.java

License:GNU Affero Public License

/**
 * Retrieve Options (name-value pairs) that are suggested from the REST
 * endpoint// w w  w .j  av  a 2  s  .c  o  m
 * 
 * @param query
 *          - the String search term
 * @param from
 *          - the 0-based begin index int
 * @param to
 *          - the end index inclusive int
 * @param callback
 *          - the OptionQueryCallback to handle the response
 */
private void queryOptions(final String query, final int from, final int to,
        final OptionQueryCallback callback) {
    final RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,
            URL.encode(mrestEndpointUrl + "?" + SearcherConstants.QUERY_PARAM + "=" + query + "&"
                    + SearcherConstants.START_PARAM + "=" + from + "&" + SearcherConstants.LIMIT_PARAM + "="
                    + PAGE_SIZE));

    // Set our headers
    builder.setHeader("Accept", "application/json; charset=utf-8");

    // Fails on chrome
    // builder.setHeader("Accept-Charset", "UTF-8");

    builder.setCallback(new RequestCallback() {

        @Override
        public void onError(final com.google.gwt.http.client.Request request, final Throwable exception) {
            callback.error(exception);
        }

        @Override
        public void onResponseReceived(final com.google.gwt.http.client.Request request,
                final Response response) {
            final JSONValue val = JSONParser.parse(response.getText());
            final JSONObject obj = val.isObject();
            final int totSize = (int) obj.get(OptionResultSet.TOTAL_SIZE).isNumber().doubleValue();
            final OptionResultSet options = new OptionResultSet(totSize);
            final JSONArray optionsArray = obj.get(OptionResultSet.OPTIONS).isArray();

            if (options.getTotalSize() > 0 && optionsArray != null) {

                for (int i = 0; i < optionsArray.size(); i++) {
                    if (optionsArray.get(i) == null) {
                        /*
                         * This happens when a JSON array has an invalid trailing comma
                         */
                        continue;
                    }

                    final JSONObject jsonOpt = optionsArray.get(i).isObject();
                    final Option option = new Option();

                    final String longName = jsonOpt.get(OptionResultSet.DISPLAY_NAME).isString().stringValue();
                    final String shortName = jsonOpt.get(OptionResultSet.VALUE).isString().stringValue();
                    final JSONValue groupTypeJsonValue = jsonOpt.get("groupType");
                    final String prefix = groupTypeJsonValue.isString() == null ? ""
                            : GroupType.PERSONAL.name().equals(groupTypeJsonValue.isString().stringValue())
                                    ? I18n.t("User") + ": "
                                    : I18n.t("Group") + ": ";
                    option.setName(prefix
                            + (!longName.equals(shortName) ? longName + " (" + shortName + ")" : shortName));
                    option.setValue(jsonOpt.get(OptionResultSet.VALUE).isString().stringValue());
                    options.addOption(option);
                }
            }
            callback.success(options);
        }
    });

    try {
        if (lastQuery != null && lastQuery.isPending()) {
            lastQuery.cancel();
        }
        lastQuery = builder.send();
    } catch (final RequestException e) {
        updateFormFeedback(FormFeedback.ERROR, "Error: " + e.getMessage());
    }

}

From source file:ccc.client.gwt.core.GWTTextParser.java

License:Open Source License

/** {@inheritDoc} */
@Override/*from   www . java  2s .c o  m*/
public Json parseJson(final String text) {
    try {
        final JSONObject result = JSONParser.parse(text).isObject();
        final Json json = new GWTJson(result);
        return json;
    } catch (final JSONException e) {
        throw new S11nException(e);
    }
}