Example usage for com.google.gwt.json.client JSONValue isObject

List of usage examples for com.google.gwt.json.client JSONValue isObject

Introduction

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

Prototype

public JSONObject isObject() 

Source Link

Document

Returns non-null if this JSONValue is really a JSONObject.

Usage

From source file:com.extjs.gxt.ui.client.js.JsonConverter.java

License:sencha.com license

protected static List<Object> decodeToList(JSONArray array) {
    List<Object> list = new ArrayList<Object>();
    for (int i = 0; i < array.size(); i++) {
        JSONValue v = array.get(i);
        if (v.isObject() != null) {
            list.add(decode(v.isObject()));
        } else if (v.isArray() != null) {
            list.add(decodeToList(v.isArray()));
        } else if (v.isNull() != null) {
            list.add(null);/*from  w  w w. j av  a  2s  . co m*/
        } else if (v.isNumber() != null) {
            list.add(v.isNumber().doubleValue());
        } else if (v.isBoolean() != null) {
            list.add(v.isBoolean().booleanValue());
        } else if (v.isString() != null) {
            list.add(decodeValue(v.isString().stringValue()));
        }
    }

    return list;
}

From source file:com.github.gilbertotorrezan.gwtcloudinary.client.CloudinaryUploadWidget.java

License:Open Source License

/**
 * Fires the {@link CloudinaryUploadFinishedEvent} by using the native objects, converting them to {@link CloudinaryUploadInfo} objects.
 */// w w  w.  j a va2 s  . co m
protected void fireUploadFinished(JavaScriptObject error, JavaScriptObject result) {
    String message = null;
    List<CloudinaryUploadInfo> infos = new ArrayList<>();

    if (error != null) {
        JSONObject obj = new JSONObject(error);
        message = getSafeString(obj.get("message"));
    }

    if (result != null) {
        JSONArray resultArray = new JSONArray(result);
        int size = resultArray.size();
        for (int i = 0; i < size; i++) {
            JSONObject object = resultArray.get(i).isObject();

            CloudinaryUploadInfo info = new CloudinaryUploadInfo();
            info.setPublicId(getSafeString(object.get("public_id")));
            info.setSecureUrl(getSafeString(object.get("secure_url")));
            info.setThumbnailUrl(getSafeString(object.get("thumbnail_url")));
            info.setUrl(getSafeString(object.get("url")));
            info.setType(getSafeString(object.get("type")));
            info.setVersion(getSafeString(object.get("version")));
            info.setWidth(getSafeInteger(object.get("width")));
            info.setHeight(getSafeInteger(object.get("height")));
            info.setFormat(getSafeString(object.get("format")));
            info.setResourceType(getSafeString(object.get("resource_type")));
            info.setSignature(getSafeString(object.get("signature")));
            info.setBytes(getSafeInteger(object.get("bytes")));
            info.setOriginalFilename(getSafeString(object.get("original_filename")));
            info.setEtag(getSafeString(object.get("etag")));
            info.setPath(getSafeString(object.get("path")));
            info.setCreatedAt(getSafeString(object.get("created_at")));

            JSONValue tagsValue = object.get("tags");
            if (tagsValue != null && tagsValue.isArray() != null) {
                JSONArray array = tagsValue.isArray();
                String[] tags = new String[array.size()];
                for (int j = 0; j < tags.length; j++) {
                    JSONValue v = array.get(j);
                    tags[j] = getSafeString(v);
                }
                info.setTags(tags);
            }

            JSONValue coordinatesValue = object.get("coordinates");
            if (coordinatesValue != null && coordinatesValue.isObject() != null) {
                JSONObject obj = coordinatesValue.isObject();

                JSONValue customValue = obj.get("custom");
                if (customValue != null && customValue.isArray() != null) {
                    JSONArray array = customValue.isArray();
                    CloudinaryCoordinates[] coordinatesArray = new CloudinaryCoordinates[array.size()];
                    for (int j = 0; j < coordinatesArray.length; j++) {
                        JSONValue value = array.get(j);
                        if (value != null && value.isArray() != null && value.isArray().size() >= 4) {
                            JSONArray valueArray = value.isArray();
                            coordinatesArray[j] = new CloudinaryCoordinates();
                            coordinatesArray[j].setX(getSafeInteger(valueArray.get(0)));
                            coordinatesArray[j].setY(getSafeInteger(valueArray.get(1)));
                            coordinatesArray[j].setWidth(getSafeInteger(valueArray.get(2)));
                            coordinatesArray[j].setHeight(getSafeInteger(valueArray.get(3)));
                        }
                    }
                    info.setCustomCoordinates(coordinatesArray);
                }

                JSONValue facesValue = obj.get("faces");
                if (facesValue == null || facesValue.isArray() == null) {
                    facesValue = obj.get("face");
                }
                if (facesValue != null && facesValue.isArray() != null) {
                    JSONArray array = facesValue.isArray();
                    CloudinaryCoordinates[] coordinatesArray = new CloudinaryCoordinates[array.size()];
                    for (int j = 0; j < coordinatesArray.length; j++) {
                        JSONValue value = array.get(j);
                        if (value != null && value.isArray() != null && value.isArray().size() >= 4) {
                            JSONArray valueArray = value.isArray();
                            coordinatesArray[j] = new CloudinaryCoordinates();
                            coordinatesArray[j].setX(getSafeInteger(valueArray.get(0)));
                            coordinatesArray[j].setY(getSafeInteger(valueArray.get(1)));
                            coordinatesArray[j].setWidth(getSafeInteger(valueArray.get(2)));
                            coordinatesArray[j].setHeight(getSafeInteger(valueArray.get(3)));
                        }
                    }
                    info.setFaceCoordinates(coordinatesArray);
                }
            }

            infos.add(info);
        }
    }

    CloudinaryUploadFinishedEvent.fireUploadFinished(this, infos, message, result, error);
}

From source file:com.github.ligangty.common.highconvert.BaseChart.java

License:Apache License

private JavaScriptObject createNativeOptions() {
    JSONObject options = getOptions();/*from ww  w .j  a va 2s  .  com*/
    if (options == null) {
        options = new JSONObject();
    }

    // #1: We need to merge the options they provided with the additional detail we know internally,
    // and first we need to set which DOM element the chart should be rendered within
    JSONValue chartValue = options.get("chart");
    if (chartValue == null || chartValue.isObject() == null) {
        chartValue = new JSONObject();
        options.put("chart", chartValue);
    }
    final JSONObject chartObject = (JSONObject) options.get("chart");
    chartObject.put("renderTo", new JSONString(this.getElement().getId()));

    // #2: We need to setup whatever data series needs to be rendered initially in the chart
    if (seriesList.size() > 0) {
        final JSONValue seriesValue = options.get("series");
        if (seriesValue == null || seriesValue.isArray() == null) {
            options.put("series", new JSONArray());
        }
        final JSONArray seriesArray = (JSONArray) options.get("series");
        for (int i = 0, seriesListSize = seriesList.size(); i < seriesListSize; i++) {
            Series series = seriesList.get(i);
            JSONObject seriesOptions = convertSeriesToJSON(series);
            seriesArray.set(i, seriesOptions);
        }
    }

    // In the case that they're going to wait to access the X/Y axis after the chart is rendered,
    // we need to call the 'get' methods once (at least for the primary X/Y axis) so that the ids
    // are set on the Highcharts options correctly
    this.getXAxis();
    this.getYAxis();

    // #3: We need to add references to our axis so that we can later lookup the
    // axis by id (as well as pass along any configuration options that were applied to the axis)
    final JSONValue xAxisJSONValue = convertToJSONValue(xAxes.toArray(new Configurable[xAxes.size()]));
    if (xAxisJSONValue != null && xAxisJSONValue.isNull() == null) {
        options.put("xAxis", xAxisJSONValue);
    }
    final JSONValue yAxisJSONValue = convertToJSONValue(yAxes.toArray(new Configurable[yAxes.size()]));
    if (yAxisJSONValue != null && yAxisJSONValue.isNull() == null) {
        options.put("yAxis", yAxisJSONValue);
    }

    // For debugging the raw options that we're passing to the chart on startup, uncomment the following line
    // com.google.gwt.user.client.Window.alert(options.toString());

    return options.getJavaScriptObject();
}

From source file:com.gloopics.g3viewer.client.G3Viewer.java

License:Apache License

private void checkAdmin() {

    doJSONRequest(IS_ADMIN_URL, new HttpSuccessHandler() {

        @Override// w w w .  j a va  2 s.  co m
        public void success(JSONValue aValue) {
            JSONObject jso = aValue.isObject();
            if (jso != null) {
                JSONString jss = jso.get("result").isString();
                if (jss != null) {
                    if (jss.stringValue().equals("success")) {

                        m_CSRF = (jso.get("csrf").isString()).stringValue();
                        m_Tree.fetchTree();
                        return;
                    }
                }
            }

            doDialog("index.php/login/ajax", new HttpDialogHandler() {

                @Override
                public void success(String aResult) {
                    // recheck admin
                    checkAdmin();

                }
            });
        }
    }, false, true);
}

From source file:com.google.api.explorer.client.parameter.schema.ObjectSchemaEditor.java

License:Apache License

@Override
public void setJSONValue(JSONValue value) {
    JSONObject obj = value.isObject();
    if (obj == null) {
        // If this object came as a json blob, we might have to deserialize it
        JSONString str = value.isString();
        JSONValue parsed = null;//w  ww .  j  a  v  a2 s .  c  om
        try {
            parsed = JSONParser.parseStrict(str.stringValue());
        } catch (Exception e) {
            // There was an error parsing, just leave parsed as null
        }
        JSONObject parsedObject = parsed != null ? parsed.isObject() : null;
        if (parsedObject != null) {
            obj = parsed.isObject();
        }
    }

    if (obj != null) {
        // Clear the editor before we start adding the keys back in
        clear();

        // For each key that we are going to map we have to instantiate an
        // appropriate editor type. The {@link #onSelect(String)} function
        // instantiates the proper editor type for the key and binds the new
        // editor to our editor.
        for (String key : obj.keySet()) {
            if (properties.containsKey(key)) {
                SchemaEditor editor = onSelect(key, OPTIONAL_PROPERTY);
                editor.setJSONValue(obj.get(key));
            } else if (additionalPropertiesType != null) {
                SchemaEditor editor = addAdditionalPropertyEditor(key);
                editor.setJSONValue(obj.get(key));
            } else {
                throw new JSONException("JSON object contains unknown key: " + key);
            }
        }
    } else {
        throw new JSONException("Invalid JSON object: " + value.toString());
    }
}

From source file:com.google.appinventor.client.explorer.commands.WaitForBuildResultCommand.java

License:Open Source License

private static String extractFormName(RpcResult result) {
    String extraString = result.getExtra();
    if (extraString != null) {
        JSONValue extraJSONValue = JSONParser.parseStrict(extraString);
        JSONObject extraJSONObject = extraJSONValue.isObject();
        if (extraJSONObject != null) {
            JSONValue formNameJSONValue = extraJSONObject.get("formName");
            if (formNameJSONValue != null) {
                JSONString formNameJSONString = formNameJSONValue.isString();
                if (formNameJSONString != null) {
                    return formNameJSONString.stringValue();
                }/*w  w  w  .jav a  2  s  . com*/
            }
        }
    }
    return "Screen1";
}

From source file:com.google.appinventor.client.wizards.TemplateUploadWizard.java

License:Open Source License

/**
 * Returns a list of Template objects containing data needed
 *  to load a template from a zip file. Each non-null template object
 *  is created from its Json string/*from ww  w.  jav  a  2  s.  c o  m*/
 *
 * @return ArrayList of TemplateInfo objects
 */
protected static ArrayList<TemplateInfo> getTemplates() {
    JSONValue jsonVal = JSONParser.parseLenient(templateDataString);
    JSONArray jsonArr = jsonVal.isArray();
    ArrayList<TemplateInfo> templates = new ArrayList<TemplateInfo>();
    for (int i = 0; i < jsonArr.size(); i++) {
        JSONValue value = jsonArr.get(i);
        JSONObject obj = value.isObject();
        if (obj != null)
            templates.add(new TemplateInfo(obj)); // Create TemplateInfo from Json
    }
    return templates;
}

From source file:com.google.appinventor.client.wizards.TemplateUploadWizard.java

License:Open Source License

/**
 * Called from ProjectToolbar when user selects a set of external templates. It uses
 *  JsonP to retrieve a json file from an external server.
 *
 * @param hostUrl, Url of the host -- e.g., http://localhost:85/
 *///www . j a v a 2s  .c om
public static void retrieveExternalTemplateData(final String hostUrl) {
    String url = hostUrl + TEMPLATES_ROOT_DIRECTORY + EXTERNAL_JSON_FILE;

    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
    try {
        Request response = builder.sendRequest(null, new RequestCallback() {
            @Override
            public void onError(Request request, Throwable exception) {
                Window.alert("Unable to load Project Template Data.");
                if (instance != null) {
                    instance.populateTemplateDialog(null);
                }
            }

            @Override
            public void onResponseReceived(Request request, Response response) {
                if (response.getStatusCode() != Response.SC_OK) {
                    Window.alert("Unable to load Project Template Data.");
                    return;
                }

                ArrayList<TemplateInfo> externalTemplates = new ArrayList<TemplateInfo>();

                JSONValue jsonVal = JSONParser.parseLenient(response.getText());
                JSONArray jsonArr = jsonVal.isArray();

                for (int i = 0; i < jsonArr.size(); i++) {
                    JSONValue entry1 = jsonArr.get(i);
                    JSONObject entry = entry1.isObject();
                    externalTemplates.add(new TemplateInfo(entry.get("name").isString().stringValue(),
                            entry.get("subtitle").isString().stringValue(),
                            entry.get("description").isString().stringValue(),
                            entry.get("screenshot").isString().stringValue(),
                            entry.get("thumbnail").isString().stringValue()));
                }
                if (externalTemplates.size() == 0) {
                    Window.alert("Unable to retrieve templates for host = " + hostUrl + ".");
                    return;
                }
                addNewTemplateHost(hostUrl, externalTemplates);
            }
        });
    } catch (RequestException e) {
        Window.alert("Error fetching external template.");
    }
}

From source file:com.google.debugging.sourcemap.SourceMapObject.java

License:Apache License

/**
 * Construct a new {@link SourceMapObject} from the source JSON.
 *///from  www  .  jav  a 2  s  .  c o m
public SourceMapObject(String contents) throws SourceMapParseException {
    JSONValue value = JSONParser.parseStrict(contents);
    JSONObject sourceMapRoot = value != null ? value.isObject() : null;
    if (sourceMapRoot == null) {
        throw new SourceMapParseException("couldn't parseStrict contents of source map");
    }

    version = getNumber(sourceMapRoot.get("version"), 0);
    file = getStringOrNull(sourceMapRoot.get("file"));
    lineCount = getNumber(sourceMapRoot.get("lineCount"), -1);
    mappings = getStringOrNull(sourceMapRoot.get("mappings"));
    sourceRoot = getStringOrNull(sourceMapRoot.get("sourceRoot"));

    if (sourceMapRoot.containsKey("sections")) {
        ImmutableList.Builder<SourceMapSection> builder = ImmutableList.builder();
        JSONArray array = sourceMapRoot.get("sections").isArray();
        if (array != null) {
            for (int i = 0, count = array.size(); i < count; ++i) {
                builder.add(buildSection(array.get(i).isObject()));
            }
        }
        sections = builder.build();
    } else {
        sections = null;
    }

    sources = getJavaStringArray(sourceMapRoot.get("sources"));
    names = getJavaStringArray(sourceMapRoot.get("names"));

    Map<String, Object> extensions = new LinkedHashMap<>();
    for (String key : sourceMapRoot.keySet()) {
        if (key.startsWith("x_")) {
            extensions.put(key, sourceMapRoot.get(key));
        }
    }
    this.extensions = Collections.unmodifiableMap(extensions);
}

From source file:com.google.gerrit.client.rpc.RestApi.java

License:Apache License

@SuppressWarnings("unchecked")
private static <T extends JavaScriptObject> T cast(JSONValue val) {
    if (val.isObject() != null) {
        return (T) val.isObject().getJavaScriptObject();
    } else if (val.isArray() != null) {
        return (T) val.isArray().getJavaScriptObject();
    } else if (val.isString() != null) {
        return (T) NativeString.wrap(val.isString().stringValue());
    } else if (val.isNull() != null) {
        return null;
    } else {/*from w  w w .  ja v  a2  s  .  c  om*/
        throw new JSONException("unsupported JSON type");
    }
}