Java Code Examples for javax.json.JsonObject#size()

The following examples show how to use javax.json.JsonObject#size() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonpRuntime.java    From jmespath-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public List<JsonValue> toList(JsonValue value) {
  ValueType valueType = value.getValueType();
  if (valueType == ARRAY) {
    return new JsonArrayListWrapper((JsonArray) value);
  } else if (valueType == OBJECT) {
    JsonObject obj = (JsonObject) value;
    if (!obj.isEmpty()) {
      List<JsonValue> elements = new ArrayList<>(obj.size());
      for (JsonValue v : obj.values()) {
        elements.add(nodeOrNullNode(v));
      }
      return elements;
    }
  }
  return Collections.emptyList();
}
 
Example 2
Source File: PsGoogle.java    From takes with MIT License 6 votes vote down vote up
/**
 * Make identity from JSON object.
 * @param json JSON received from Google
 * @return Identity found
 */
private static Identity parse(final JsonObject json) {
    final Map<String, String> props = new HashMap<>(json.size());
    final Opt<JsonObject> image = new Opt.Single<>(
        json.getJsonObject("image")
    );
    if (image.has()) {
        props.put(PsGoogle.PICTURE, image.get().getString("url", "#"));
    } else {
        props.put(PsGoogle.PICTURE, "#");
    }
    props.put(
        PsGoogle.NAME, json.getString(PsGoogle.DISPLAY_NAME, "unknown")
    );
    return new Identity.Simple(
        String.format("urn:google:%s", json.getString("id")), props
    );
}
 
Example 3
Source File: PsLinkedin.java    From takes with MIT License 5 votes vote down vote up
/**
 * Make identity from JSON object.
 * @param json JSON received from Github
 * @return Identity found
 */
private static Identity parse(final JsonObject json) {
    final String fname = "firstName";
    final String lname = "lastName";
    final String unknown = "?";
    final Map<String, String> props = new HashMap<>(json.size());
    props.put(fname, json.getString(fname, unknown));
    props.put(lname, json.getString(lname, unknown));
    return new Identity.Simple(
        String.format("urn:linkedin:%s", json.getString("id")), props
    );
}
 
Example 4
Source File: PsTwitter.java    From takes with MIT License 5 votes vote down vote up
/**
 * Make identity from JSON object.
 * @param json JSON received from Twitter
 * @return Identity found
 */
private static Identity parse(final JsonObject json) {
    final Map<String, String> props = new HashMap<>(json.size());
    props.put(PsTwitter.NAME, json.getString(PsTwitter.NAME));
    props.put("picture", json.getString("profile_image_url"));
    return new Identity.Simple(
        String.format("urn:twitter:%d", json.getInt("id")),
        props
    );
}
 
Example 5
Source File: PsGithub.java    From takes with MIT License 5 votes vote down vote up
/**
 * Make identity from JSON object.
 * @param json JSON received from Github
 * @return Identity found
 */
private static Identity parse(final JsonObject json) {
    final Map<String, String> props = new HashMap<>(json.size());
    // @checkstyle MultipleStringLiteralsCheck (1 line)
    props.put(PsGithub.LOGIN, json.getString(PsGithub.LOGIN, "unknown"));
    props.put("avatar", json.getString("avatar_url", "#"));
    return new Identity.Simple(
        String.format("urn:github:%d", json.getInt("id")), props
    );
}
 
Example 6
Source File: Json2Xml.java    From iaf with Apache License 2.0 5 votes vote down vote up
@Override
public void startParse(JsonValue node) throws SAXException {
	if (node instanceof JsonObject) {
		JsonObject root = (JsonObject)node;
		if (StringUtils.isEmpty(getRootElement())) {
			if (root.isEmpty()) {
				throw new SAXException("no names found");
			}
			if (root.size()>1) {
				String namesList=null;
				int i=0;
				for (String name:root.keySet()) {
					if (namesList==null) {
						namesList=name;
					} else {
						namesList+=","+name;
					}
					if (i++>5) {
						namesList+=", ...";
						break;
					}
				}
				throw new SAXException("too many names ["+namesList+"]");
			}
			setRootElement((String)root.keySet().toArray()[0]);
		} 
		// determine somewhat heuristically whether the json contains a 'root' node:
		// if the outermost JsonObject contains only one key, that has the name of the root element, 
		// then we'll assume that that is the root element...
		if (root.size()==1 && getRootElement().equals(root.keySet().toArray()[0])) {
			node=root.get(getRootElement());
		}
	}
	if (node instanceof JsonArray && !insertElementContainerElements && strictSyntax) {
		throw new SAXException(MSG_EXPECTED_SINGLE_ELEMENT+" ["+getRootElement()+"] or array element container");
	}
	super.startParse(node);
}