Remove the constraint that map keys mustn't be the empty string.
See bug 227.
This commit is contained in:
parent
69ecb9465a
commit
7e1e4eab07
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
|
@ -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());
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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(""));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user