Skip to content

Commit

Permalink
Allow the BeanToJsonConverter cleanly handle common return types
Browse files Browse the repository at this point in the history
The way that `toJson` is currently handled encourages people
to take a hard dependency on gson, or attempt to handle
serialising JSON themselves. Modify how `toJson` is handled
to allow us to return other data types, such as `Map` and
anything extending `Collection`.

This opens up the possibility of an infinite loop. Best not
do that in the Real World, eh?
  • Loading branch information
shs96c committed Jan 23, 2017
1 parent b2aa9fd commit ea350ae
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,17 @@ private JsonElement convertObject(Object toConvert, int maxDepth) throws Excepti
if (res instanceof JsonElement) {
return (JsonElement) res;
}
try {
return new JsonParser().parse((String) res);
} catch (JsonParseException e) {
return new JsonPrimitive((String) res);

if (res instanceof Map) {
return convertObject(res);
} else if (res instanceof Collection) {
return convertObject(res);
} else if (res instanceof String) {
try {
return new JsonParser().parse((String) res);
} catch (JsonParseException e) {
return new JsonPrimitive((String) res);
}
}
} catch (ReflectiveOperationException e) {
throw new WebDriverException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
import static org.junit.Assert.fail;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSyntaxException;

import org.junit.Test;
Expand Down Expand Up @@ -233,6 +235,39 @@ public void testShouldCallToJsonMethodIfPresent() {
assertEquals("\"converted\"", json);
}

@Test
public void toJsonMethodCanConvertibleReturnedMap() {
class ToJsonReturnsMap {
public Map<String, Object> toJson() {
return ImmutableMap.of("cheese", "peas");
}
}

String json = new BeanToJsonConverter().convert(new ToJsonReturnsMap());
JsonObject converted = new JsonParser().parse(json).getAsJsonObject();

assertEquals(1, converted.entrySet().size());
assertEquals("peas", converted.get("cheese").getAsString());
}

@Test
public void toJsonMethodCanConvertReturnedCollection() {
class ToJsonReturnsCollection {
public Set<String> toJson() {
return ImmutableSortedSet.of("cheese", "peas");
}
}

String json = new BeanToJsonConverter().convert(new ToJsonReturnsCollection());
JsonArray converted = new JsonParser().parse(json).getAsJsonArray();

assertEquals(2, converted.size());
JsonArray expected = new JsonArray();
expected.add(new JsonPrimitive("cheese"));
expected.add(new JsonPrimitive("peas"));
assertEquals(expected, converted);
}

@Test
public void testShouldCallAsMapMethodIfPresent() {
String json = new BeanToJsonConverter().convert(new Mappable1("a key", "a value"));
Expand Down

0 comments on commit ea350ae

Please sign in to comment.