Remove the constraint that map keys mustn't be the empty string.

See bug 227.
This commit is contained in:
Jesse Wilson 2010-08-28 05:02:33 +00:00
parent 69ecb9465a
commit 7e1e4eab07
3 changed files with 32 additions and 1 deletions

View File

@ -52,7 +52,7 @@ public final class JsonObject extends JsonElement {
* @param value the member object. * @param value the member object.
*/ */
public void add(String property, JsonElement value) { public void add(String property, JsonElement value) {
Preconditions.checkArgument(property != null && !"".equals(property.trim())); Preconditions.checkArgument(property != null);
if (value == null) { if (value == null) {
value = JsonNull.createJsonNull(); value = JsonNull.createJsonNull();
} }

View File

@ -120,4 +120,20 @@ public class JsonObjectTest extends TestCase {
String json = new Gson().toJson(jsonObj); String json = new Gson().toJson(jsonObj);
assertEquals("{\"a\\\"b\":\"c\\\"d\"}", json); assertEquals("{\"a\\\"b\":\"c\\\"d\"}", json);
} }
/**
* From issue 227.
*/
public void testWritePropertyWithEmptyStringName() {
JsonObject jsonObj = new JsonObject();
jsonObj.add("", new JsonPrimitive(true));
assertEquals("{\"\":true}", new Gson().toJson(jsonObj));
}
public void testReadPropertyWithEmptyStringName() {
JsonObject jsonObj = new JsonParser().parse("{\"\":true}").getAsJsonObject();
assertEquals(true, jsonObj.get("").getAsBoolean());
}
} }

View File

@ -297,4 +297,19 @@ public class MapTest extends TestCase {
String json = gson.toJson(map); String json = gson.toJson(map);
assertEquals("{\"a\\\"b\":\"c\\\"d\"}", json); assertEquals("{\"a\\\"b\":\"c\\\"d\"}", json);
} }
/**
* From issue 227.
*/
public void testWriteMapsWithEmptyStringKey() {
Map<String, Boolean> map = new HashMap<String, Boolean>();
map.put("", true);
assertEquals("{\"\":true}", gson.toJson(map));
}
public void testReadMapsWithEmptyStringKey() {
Map<String, Boolean> map = gson.fromJson("{\"\":true}", new TypeToken<Map<String, Boolean>>() {}.getType());
assertEquals(Boolean.TRUE, map.get(""));
}
} }