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.google.gson.JsonElement.java

License:Apache License

static JsonElement wrap(JSONValue value) {
    if (value == null)
        return null;
    if (value.isNull() != null)
        return JsonNull.createJsonNull();
    if (value.isBoolean() != null || value.isNumber() != null || value.isString() != null)
        return new JsonPrimitive(value);
    if (value.isObject() != null)
        return new JsonObject(value.isObject());
    if (value.isArray() != null)
        return new JsonArray(value.isArray());

    throw new IllegalArgumentException();
}

From source file:com.google.gwt.sample.hello.client.HelloJsonp.java

private void decodeJson(JSONValue jv) {
    JSONArray jsonArray;//from  w ww . j a  v a2  s  . c om
    JSONObject jsonObject;
    JSONString jsonString;

    if ((jsonArray = jv.isArray()) != null) {
        str.append(" [[ \n");
        indent += 4;
        prefix();
        for (int i = 0; i < jsonArray.size(); ++i) {
            str.append("[" + Integer.toString(i) + "]=(");
            decodeJson(jsonArray.get(i));
            str.append(")\n");
            prefix();
        }
        str.append("\n");
        indent -= 4;
        prefix();
        str.append(" ]] \n");
        prefix();
    } else if ((jsonObject = jv.isObject()) != null) {
        Set<String> keys = jsonObject.keySet();
        str.append(" {{ \n");
        indent += 4;
        prefix();
        for (String key : keys) {
            str.append("{" + key + "}=(");
            decodeJson(jsonObject.get(key));
            str.append(")\n");
            prefix();
        }
        str.append("\n");
        indent -= 4;
        prefix();
        str.append(" }} \n");
        prefix();
    } else if ((jsonString = jv.isString()) != null) {
        // Use stringValue instead of toString() because we don't want escaping
        str.append("\"" + jsonString.stringValue() + "\"\n");
        prefix();
    } else {
        // JSONBoolean, JSONNumber, and JSONNull work well with toString().
        str.append(jv.toString() + "\n");
        prefix();
    }
}

From source file:com.google.gwt.sample.json.client.JSON.java

License:Apache License

private void addChildren(TreeItem treeItem, JSONValue jsonValue) {
    JSONArray jsonArray;/*from   w w  w .jav a  2 s  .com*/
    JSONObject jsonObject;
    JSONString jsonString;

    if ((jsonArray = jsonValue.isArray()) != null) {
        for (int i = 0; i < jsonArray.size(); ++i) {
            TreeItem child = treeItem.addItem(getChildText("[" + Integer.toString(i) + "]"));
            addChildren(child, jsonArray.get(i));
        }
    } else if ((jsonObject = jsonValue.isObject()) != null) {
        Set<String> keys = jsonObject.keySet();
        for (String key : keys) {
            TreeItem child = treeItem.addItem(getChildText(key));
            addChildren(child, jsonObject.get(key));
        }
    } else if ((jsonString = jsonValue.isString()) != null) {
        // Use stringValue instead of toString() because we don't want escaping
        treeItem.addItem(jsonString.stringValue());
    } else {
        // JSONBoolean, JSONNumber, and JSONNull work well with toString().
        treeItem.addItem(getChildText(jsonValue.toString()));
    }
}

From source file:com.googlecode.gwtphonegap.client.contacts.browser.ContactsBrowserImpl.java

License:Apache License

protected LightMap<Contact> loadContactsFromStorage() {
    LightMap<Contact> map = CollectionFactory.constructMap();

    String item = storage.getItem("gwtphonegap-contact-emulation");
    if (item == null) {
        return map;
    }//  ww  w.j a  v  a2  s.  com
    JSONValue parsed = JSONParser.parseStrict(item);

    JSONObject root = parsed.isObject();
    if (root == null) {
        return map;
    } else {
        Set<String> set = root.keySet();
        for (String key : set) {
            JSONObject jsonContact = root.get(key).isObject();
            Contact contact = parseContact(jsonContact);
            map.put(contact.getId(), contact);
        }
    }

    return map;
}

From source file:com.hiramchirino.restygwt.client.AbstractJsonEncoderDecoder.java

License:Apache License

static public JSONObject toObject(JSONValue value) {
    JSONObject object = value.isObject();
    if (object == null) {
        throw new DecodingException("Expected a json obejct, but was given: " + object);
    }/* w ww . jav a2 s. com*/
    return object;
}

From source file:com.hiramchirino.restygwt.client.AbstractJsonEncoderDecoder.java

License:Apache License

static public <Type> Map<String, Type> toMap(JSONValue value, AbstractJsonEncoderDecoder<Type> encoder,
        Style style) {/*from ww  w . j a  v a 2  s .  co m*/
    if (value == null || value.isNull() != null) {
        return null;
    }

    switch (style) {
    case DEFAULT:
    case SIMPLE: {
        JSONObject object = value.isObject();
        if (object == null) {
            throw new DecodingException("Expected a json object, but was given: " + value);
        }

        HashMap<String, Type> rc = new HashMap<String, Type>(object.size() * 2);
        for (String key : object.keySet()) {
            rc.put(key, encoder.decode(object.get(key)));
        }
        return rc;
    }
    case JETTISON_NATURAL: {
        JSONObject object = value.isObject();
        if (object == null) {
            throw new DecodingException("Expected a json object, but was given: " + value);
        }
        value = object.get("entry");
        if (value == null) {
            throw new DecodingException("Expected an entry array not found");
        }
        JSONArray entries = value.isArray();
        if (entries == null) {
            throw new DecodingException("Expected an entry array, but was given: " + value);
        }

        HashMap<String, Type> rc = new HashMap<String, Type>(object.size() * 2);
        for (int i = 0; i < entries.size(); i++) {
            JSONObject entry = entries.get(i).isObject();
            if (entry == null)
                throw new DecodingException("Expected an entry object, but was given: " + value);
            JSONValue key = entry.get("key");
            if (key == null)
                throw new DecodingException("Expected an entry key field not found");
            JSONString k = key.isString();
            if (k == null)
                throw new DecodingException("Expected an entry key to be a string, but was given: " + value);

            rc.put(k.stringValue(), encoder.decode(entry.get("value")));
        }
        return rc;
    }
    default:
        throw new UnsupportedOperationException("The encoding style is not yet suppored: " + style.name());
    }
}

From source file:com.hunchee.haystack.client.utils.JsonConverter.java

License:Open Source License

/**
 * Decodes a JSONObject to a map.//  w  ww  . j  a v a 2  s  . c  om
 *
 * @param jso the JSONObject
 * @return the map
 */
public static Map<String, Object> decode(JSONObject jso) {
    Map<String, Object> map = new LinkedHashMap<String, Object>();
    for (String key : jso.keySet()) {
        JSONValue j = jso.get(key);
        if (j.isObject() != null) {
            map.put(key, decode(j.isObject()));
        } else if (j.isArray() != null) {
            map.put(key, decodeToList(j.isArray()));
        } else if (j.isBoolean() != null) {
            map.put(key, j.isBoolean().booleanValue());
        } else if (j.isNumber() != null) {
            map.put(key, j.isNumber().doubleValue());
        } else if (j.isString() != null) {
            //map.put(key, decodeValue(j.isString().stringValue()));
            map.put(key, j.isString().stringValue());
        }
    }
    return map;
}

From source file:com.hunchee.haystack.client.utils.JsonConverter.java

License:Open Source 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. ja  va 2 s.c  o 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()));
            list.add(v.isString().stringValue());
        }
    }

    return list;
}

From source file:com.hunchee.haystack.client.utils.JsonHelper.java

License:Open Source License

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()) {
            map.put(key, jsonObj.get(key).toString());
        }/*ww w. j  a  va 2s .c  om*/
    }

    return map;
}

From source file:com.ikon.frontend.client.bean.GWTFileUploadResponse.java

License:Open Source License

/**
 * GWTFileUploadResponse//from www.j av  a 2 s  . c  o m
 * 
 * @param text json encoded parameters.
 */
public GWTFileUploadResponse(String text) {
    text = text.substring(text.indexOf("{"));
    text = text.substring(0, text.lastIndexOf("}") + 1);
    JSONValue responseValue = JSONParser.parseStrict(text);
    JSONObject response = responseValue.isObject();

    // Deserialize information
    hasAutomation = response.get("hasAutomation").isBoolean().booleanValue();
    path = URL.decodeQueryString(response.get("path").isString().stringValue());
    error = response.get("error").isString().stringValue();
    showWizardCategories = response.get("showWizardCategories").isBoolean().booleanValue();
    showWizardKeywords = response.get("showWizardKeywords").isBoolean().booleanValue();
    digitalSignature = response.get("digitalSignature").isBoolean().booleanValue();

    // Getting property groups
    JSONArray groupsArray = response.get("groupsList").isArray();

    if (groupsArray != null) {
        for (int i = 0; i <= groupsArray.size() - 1; i++) {
            groupsList.add(groupsArray.get(i).isString().stringValue());
        }
    }

    // Getting workflows
    JSONArray workflowArray = response.get("workflowList").isArray();

    if (workflowArray != null) {
        for (int i = 0; i <= workflowArray.size() - 1; i++) {
            workflowList.add(workflowArray.get(i).isString().stringValue());
        }
    }
}