Add keySet method and test (#942)

This commit is contained in:
Mark Hess 2016-12-14 00:15:49 -06:00 committed by inder123
parent 5f73666404
commit 4976e420fc
2 changed files with 24 additions and 0 deletions

View File

@ -132,6 +132,15 @@ public final class JsonObject extends JsonElement {
return members.entrySet();
}
/**
* Returns a set of members key values.
*
* @return a set of member keys as Strings
*/
public Set<String> keySet() {
return members.keySet();
}
/**
* Returns the number of key/value pairs in the object.
*

View File

@ -183,4 +183,19 @@ public class JsonObjectTest extends TestCase {
assertEquals(1, original.get("key").getAsJsonArray().size());
assertEquals(0, copy.get("key").getAsJsonArray().size());
}
/**
* From issue 941
*/
public void testKeySet() {
JsonObject a = new JsonObject();
a.add("foo", new JsonArray());
a.add("bar", new JsonObject());
assertEquals(2, a.size());
assertEquals(2, a.keySet().size());
assertTrue(a.keySet().contains("foo"));
assertTrue(a.keySet().contains("bar"));
}
}