Search in sources :

Example 6 with JSONObject

use of com.google.gwt.json.client.JSONObject in project che by eclipse.

the class JsonHelper method toMap.

public static Map<String, String> toMap(String jsonStr) {
    Map<String, String> map = new HashMap<String, String>();
    JSONValue parsed = JSONParser.parseStrict(jsonStr);
    JSONObject jsonObj = parsed.isObject();
    if (jsonObj != null) {
        for (String key : jsonObj.keySet()) {
            JSONValue jsonValue = jsonObj.get(key);
            JSONString jsonString = jsonValue.isString();
            // if the json value is a string, set the unescaped value, else set the json representation of the value
            String stringValue = (jsonString == null) ? jsonValue.toString() : jsonString.stringValue();
            map.put(key, stringValue);
        }
    }
    return map;
}
Also used : JSONValue(com.google.gwt.json.client.JSONValue) JSONObject(com.google.gwt.json.client.JSONObject) HashMap(java.util.HashMap) JSONString(com.google.gwt.json.client.JSONString) JSONString(com.google.gwt.json.client.JSONString)

Example 7 with JSONObject

use of com.google.gwt.json.client.JSONObject in project che by eclipse.

the class JsonHelper method toMapOfLists.

public static Map<String, List<String>> toMapOfLists(String jsonStr) {
    Map<String, List<String>> map = new HashMap<>();
    JSONValue parsed = JSONParser.parseStrict(jsonStr);
    JSONObject jsonObj = parsed.isObject();
    if (jsonObj != null) {
        for (String key : jsonObj.keySet()) {
            JSONValue jsonValue = jsonObj.get(key);
            JSONArray jsonArray = jsonValue.isArray();
            List<String> values = new ArrayList<>();
            for (int i = 0; i < jsonArray.size(); i++) {
                values.add(jsonArray.get(i).isString().stringValue());
            }
            map.put(key, values);
        }
    }
    return map;
}
Also used : JSONValue(com.google.gwt.json.client.JSONValue) JSONObject(com.google.gwt.json.client.JSONObject) HashMap(java.util.HashMap) JSONArray(com.google.gwt.json.client.JSONArray) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) JSONString(com.google.gwt.json.client.JSONString)

Example 8 with JSONObject

use of com.google.gwt.json.client.JSONObject in project opentsdb by OpenTSDB.

the class QueryUi method asyncGetJson.

private void asyncGetJson(final String url, final GotJsonCallback callback) {
    final RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
    try {
        builder.sendRequest(null, new RequestCallback() {

            public void onError(final Request request, final Throwable e) {
                displayError("Failed to get " + url + ": " + e.getMessage());
                // Since we don't call the callback we've been given, reset this
                // bit of state as we're not going to retry anything right now.
                pending_requests = 0;
            }

            public void onResponseReceived(final Request request, final Response response) {
                final int code = response.getStatusCode();
                if (code == Response.SC_OK) {
                    clearError();
                    callback.got(JSONParser.parse(response.getText()));
                    return;
                } else if (code >= Response.SC_BAD_REQUEST) {
                    // 400+ => Oops.
                    // Since we don't call the callback we've been given, reset this
                    // bit of state as we're not going to retry anything right now.
                    pending_requests = 0;
                    String err = response.getText();
                    // an error message.
                    if (!err.isEmpty() && err.charAt(0) == '{') {
                        final JSONValue json = JSONParser.parse(err);
                        final JSONObject result = json == null ? null : json.isObject();
                        final JSONValue jerr = result == null ? null : result.get("err");
                        final JSONString serr = jerr == null ? null : jerr.isString();
                        err = serr.stringValue();
                        // If the error message has multiple lines (which is common if
                        // it contains a stack trace), show only the first line and
                        // hide the rest in a panel users can expand.
                        final int newline = err.indexOf('\n', 1);
                        final String msg = "Request failed: " + response.getStatusText();
                        if (newline < 0) {
                            displayError(msg + ": " + err);
                        } else {
                            displayError(msg);
                            final DisclosurePanel dp = new DisclosurePanel(err.substring(0, newline));
                            // Attach the widget.
                            RootPanel.get("queryuimain").add(dp);
                            final InlineLabel content = new InlineLabel(err.substring(newline, err.length()));
                            // For readable stack traces.
                            content.addStyleName("fwf");
                            dp.setContent(content);
                            current_error.getElement().appendChild(dp.getElement());
                        }
                    } else {
                        displayError("Request failed while getting " + url + ": " + response.getStatusText());
                        // Since we don't call the callback we've been given, reset this
                        // bit of state as we're not going to retry anything right now.
                        pending_requests = 0;
                    }
                    graphstatus.setText("");
                }
            }
        });
    } catch (RequestException e) {
        displayError("Failed to get " + url + ": " + e.getMessage());
    }
}
Also used : RequestBuilder(com.google.gwt.http.client.RequestBuilder) Request(com.google.gwt.http.client.Request) DisclosurePanel(com.google.gwt.user.client.ui.DisclosurePanel) JSONString(com.google.gwt.json.client.JSONString) RequestException(com.google.gwt.http.client.RequestException) EntryPoint(com.google.gwt.core.client.EntryPoint) Response(com.google.gwt.http.client.Response) JSONValue(com.google.gwt.json.client.JSONValue) RequestCallback(com.google.gwt.http.client.RequestCallback) JSONObject(com.google.gwt.json.client.JSONObject) InlineLabel(com.google.gwt.user.client.ui.InlineLabel) JSONString(com.google.gwt.json.client.JSONString)

Example 9 with JSONObject

use of com.google.gwt.json.client.JSONObject in project rstudio by rstudio.

the class RemoteServer method previewDataImport.

@Override
public void previewDataImport(DataImportOptions dataImportOptions, int maxCols, int maxFactors, ServerRequestCallback<DataImportPreviewResponse> requestCallback) {
    JSONArray params = new JSONArray();
    params.set(0, new JSONObject(dataImportOptions));
    params.set(1, new JSONNumber(maxCols));
    params.set(2, new JSONNumber(maxFactors));
    sendRequest(RPC_SCOPE, PREVIEW_DATA_IMPORT, params, requestCallback);
}
Also used : JSONObject(com.google.gwt.json.client.JSONObject) JSONArray(com.google.gwt.json.client.JSONArray) JSONNumber(com.google.gwt.json.client.JSONNumber)

Example 10 with JSONObject

use of com.google.gwt.json.client.JSONObject in project rstudio by rstudio.

the class RemoteServer method quitSession.

public void quitSession(boolean saveWorkspace, String switchToProject, RVersionSpec switchToRVersion, String hostPageUrl, ServerRequestCallback<Boolean> requestCallback) {
    JSONArray params = new JSONArray();
    params.set(0, JSONBoolean.getInstance(saveWorkspace));
    params.set(1, new JSONString(StringUtil.notNull(switchToProject)));
    if (switchToRVersion != null)
        params.set(2, new JSONObject(switchToRVersion));
    else
        params.set(2, JSONNull.getInstance());
    params.set(3, new JSONString(StringUtil.notNull(hostPageUrl)));
    sendRequest(RPC_SCOPE, QUIT_SESSION, params, requestCallback);
}
Also used : JSONObject(com.google.gwt.json.client.JSONObject) JSONArray(com.google.gwt.json.client.JSONArray) JSONString(com.google.gwt.json.client.JSONString)

Aggregations

JSONObject (com.google.gwt.json.client.JSONObject)227 JSONNumber (com.google.gwt.json.client.JSONNumber)109 JSONString (com.google.gwt.json.client.JSONString)74 JSONArray (com.google.gwt.json.client.JSONArray)62 PerunError (cz.metacentrum.perun.webgui.model.PerunError)55 JavaScriptObject (com.google.gwt.core.client.JavaScriptObject)54 JsonCallbackEvents (cz.metacentrum.perun.webgui.json.JsonCallbackEvents)54 JsonPostClient (cz.metacentrum.perun.webgui.json.JsonPostClient)50 JSONValue (com.google.gwt.json.client.JSONValue)13 HashMap (java.util.HashMap)12 Map (java.util.Map)8 ArrayList (java.util.ArrayList)6 JsArrayString (com.google.gwt.core.client.JsArrayString)3 User (cz.metacentrum.perun.webgui.model.User)3 ClickEvent (com.google.gwt.event.dom.client.ClickEvent)2 ClickHandler (com.google.gwt.event.dom.client.ClickHandler)2 Command (com.google.gwt.user.client.Command)2 Contact (com.googlecode.gwtphonegap.client.contacts.Contact)2 ContactField (com.googlecode.gwtphonegap.client.contacts.ContactField)2 StringCallback (com.gwtmobile.phonegap.client.plugins.Bluetooth.StringCallback)2