Skip to content

Commit

Permalink
FELIX-5358 Cannot deserialize empty Map
Browse files Browse the repository at this point in the history
This commit resolves the issue.
Testcase contributed by David Leangen with many thanks.


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1762341 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
bosschaert committed Sep 26, 2016
1 parent 6e78e08 commit 5696cb5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Expand Up @@ -130,8 +130,12 @@ private static Map<String, Object> parseObject(String jsonObject) {
if (!(jsonObject.startsWith("{") && jsonObject.endsWith("}")))
throw new IllegalArgumentException("Malformatted JSON object: " + jsonObject);

jsonObject = jsonObject.substring(1, jsonObject.length() - 1);
Map<String, Object> values = new HashMap<>();

jsonObject = jsonObject.substring(1, jsonObject.length() - 1).trim();
if (jsonObject.length() == 0)
return values;

for (String element : parseKeyValueListRaw(jsonObject)) {
Pair<String, Object> pair = parseKeyValue(element);
values.put(pair.key, pair.value);
Expand Down
Expand Up @@ -21,7 +21,6 @@
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.felix.serializer.impl.json.JsonSerializerImpl;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -78,4 +77,13 @@ public void testComplexMapSerialization2() {
+ "{\"yes\":true,\"no\":{\"maybe\":false}}}";
assertEquals(expected, new JsonSerializerImpl().serialize(cm).toString());
}

@Test
public void testEmptyMapSerialization() {
Map<?,?> m = new LinkedHashMap<>();
String expected = "{}";
assertEquals(expected, new JsonSerializerImpl().serialize(m).toString());
Map<?,?> m2 = new JsonSerializerImpl().deserialize(Map.class).from(expected);
assertEquals(m, m2);
}
}

0 comments on commit 5696cb5

Please sign in to comment.