Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added JsonObject.getObject(String) method to complete helper API #82

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,23 @@ public JsonValue get(String name) {
return index != -1 ? values.get(index) : null;
}

/**
* Returns the <code>JsonObject</code> value of the member with the specified name in this object. If
* this object does not contain a member with this name, null is returned. If
* this object contains multiple members with the given name, the last one will be picked. If this
* member's value does not represent a JSON number or if it cannot be interpreted as Java
* <code>int</code>, an exception is thrown.
*
* @param name
* the name of the member whose value is to be returned
* @return the value of the last member with the specified name, or the given default value if
* this object does not contain a member with that name
*/
public JsonObject getObject(String name) {
JsonValue value = get(name);
return value != null ? value.asObject() : null;
}

/**
* Returns the <code>int</code> value of the member with the specified name in this object. If
* this object does not contain a member with this name, the given default value is returned. If
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,22 @@ public void get_returnsLastValueForName() {
assertEquals(Json.TRUE, object.get("foo"));
}

@Test
public void get_object_returnsValueFromMember() {
JsonObject member = new JsonObject();
member.add("foo", "bar");

object.add("foo", member);

assertEquals("bar", object.getObject("foo").getString("foo",null));
}

@Test
public void get_object_returnsNullForMissingMember() {
assertNull(object.getObject("foo"));
}


@Test
public void get_int_returnsValueFromMember() {
object.add("foo", 23);
Expand Down