Search in sources :

Example 6 with JSONValue

use of com.google.gwt.json.client.JSONValue 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 JSONValue

use of com.google.gwt.json.client.JSONValue 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 JSONValue

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

the class StringListUnmarshaller method toList.

public List<String> toList(String jsonStr) {
    JSONValue parsed = JSONParser.parseStrict(jsonStr);
    JSONArray jsonArray = parsed.isArray();
    if (jsonArray == null) {
        return Collections.emptyList();
    }
    List<String> list = new ArrayList<>();
    for (int i = 0; i < jsonArray.size(); i++) {
        JSONValue jsonValue = jsonArray.get(i);
        JSONString jsonString = jsonValue.isString();
        String stringValue = (jsonString == null) ? jsonValue.toString() : jsonString.stringValue();
        list.add(stringValue);
    }
    return list;
}
Also used : JSONValue(com.google.gwt.json.client.JSONValue) JSONArray(com.google.gwt.json.client.JSONArray) ArrayList(java.util.ArrayList) JSONString(com.google.gwt.json.client.JSONString) JSONString(com.google.gwt.json.client.JSONString)

Example 9 with JSONValue

use of com.google.gwt.json.client.JSONValue 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 10 with JSONValue

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

the class QueryUi method refreshLogs.

private void refreshLogs() {
    asyncGetJson(LOGS_URL, new GotJsonCallback() {

        public void got(final JSONValue json) {
            final JSONArray logmsgs = json.isArray();
            final int nmsgs = logmsgs.size();
            final FlexTable.FlexCellFormatter fcf = logs.getFlexCellFormatter();
            final FlexTable.RowFormatter rf = logs.getRowFormatter();
            for (int i = 0; i < nmsgs; i++) {
                final String msg = logmsgs.get(i).isString().stringValue();
                String part = msg.substring(0, msg.indexOf('\t'));
                logs.setText(i * 2, 0, new Date(Integer.valueOf(part) * 1000L).toString());
                // So we can change the style ahead.
                logs.setText(i * 2 + 1, 0, "");
                int pos = part.length() + 1;
                part = msg.substring(pos, msg.indexOf('\t', pos));
                if ("WARN".equals(part)) {
                    rf.getElement(i * 2).getStyle().setBackgroundColor("#FCC");
                    rf.getElement(i * 2 + 1).getStyle().setBackgroundColor("#FCC");
                } else if ("ERROR".equals(part)) {
                    rf.getElement(i * 2).getStyle().setBackgroundColor("#F99");
                    rf.getElement(i * 2 + 1).getStyle().setBackgroundColor("#F99");
                } else {
                    rf.getElement(i * 2).getStyle().clearBackgroundColor();
                    rf.getElement(i * 2 + 1).getStyle().clearBackgroundColor();
                    if ((i % 2) == 0) {
                        rf.addStyleName(i * 2, "subg");
                        rf.addStyleName(i * 2 + 1, "subg");
                    }
                }
                pos += part.length() + 1;
                // level
                logs.setText(i * 2, 1, part);
                part = msg.substring(pos, msg.indexOf('\t', pos));
                pos += part.length() + 1;
                // thread
                logs.setText(i * 2, 2, part);
                part = msg.substring(pos, msg.indexOf('\t', pos));
                pos += part.length() + 1;
                if (part.startsWith("net.opentsdb.")) {
                    part = part.substring(13);
                } else if (part.startsWith("org.hbase.")) {
                    part = part.substring(10);
                }
                // logger
                logs.setText(i * 2, 3, part);
                // message
                logs.setText(i * 2 + 1, 0, msg.substring(pos));
                fcf.setColSpan(i * 2 + 1, 0, 4);
                rf.addStyleName(i * 2, "fwf");
                rf.addStyleName(i * 2 + 1, "fwf");
            }
        }
    });
}
Also used : JSONValue(com.google.gwt.json.client.JSONValue) JSONArray(com.google.gwt.json.client.JSONArray) JSONString(com.google.gwt.json.client.JSONString) Date(java.util.Date)

Aggregations

JSONValue (com.google.gwt.json.client.JSONValue)23 JSONObject (com.google.gwt.json.client.JSONObject)13 JSONArray (com.google.gwt.json.client.JSONArray)10 JSONString (com.google.gwt.json.client.JSONString)8 HashMap (java.util.HashMap)5 Date (java.util.Date)4 JSONNumber (com.google.gwt.json.client.JSONNumber)3 EntryPoint (com.google.gwt.core.client.EntryPoint)2 JavaScriptObject (com.google.gwt.core.client.JavaScriptObject)2 RequestBuilder (com.google.gwt.http.client.RequestBuilder)2 RequestCallback (com.google.gwt.http.client.RequestCallback)2 RequestException (com.google.gwt.http.client.RequestException)2 InlineLabel (com.google.gwt.user.client.ui.InlineLabel)2 StringCallback (com.gwtmobile.phonegap.client.plugins.Bluetooth.StringCallback)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 JsArrayString (com.google.gwt.core.client.JsArrayString)1 Style (com.google.gwt.dom.client.Style)1 ClickEvent (com.google.gwt.event.dom.client.ClickEvent)1 ClickHandler (com.google.gwt.event.dom.client.ClickHandler)1