Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ else if (attributeValue instanceof Map)
//noinspection unchecked
sb.append(jsonAttribute).append("\": ").append(getJsonFromMapRecursive((Map<String, Object>) attributeValue));
}
else if (isNumber(attributeValue))
else if (isNumber(attributeValue) || attributeValue instanceof Boolean)
{
sb.append(jsonAttribute).append("\": ").append(attributeValue);
}
Expand Down Expand Up @@ -190,7 +190,7 @@ else if (innerAttributeValue instanceof List) // recursive case
{
json.append("\"").append(innerAttribute).append("\": \"").append(getSafeSizeString((String) innerAttributeValue)).append("\", ");
}
else if (isNumber(innerAttributeValue))
else if (isNumber(innerAttributeValue) || innerAttributeValue instanceof Boolean)
{
json.append("\"").append(innerAttribute).append("\": ").append(innerAttributeValue).append(", ");
}
Expand Down Expand Up @@ -234,7 +234,7 @@ else if (element instanceof List) // recursive case
{
json.append("\"").append(getSafeSizeString((String) element)).append("\", ");
}
else if (isNumber(element))
else if (isNumber(element) || element instanceof Boolean)
{
json.append(element).append(", ");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,26 @@ public void getJsonBody_customFields()

}

@Test
public void getJsonBody_WithBooleans()
{
double timestamp = (System.currentTimeMillis() - 5000) / 1000d;

AnalyticsEvent event = new AnalyticsEvent("test_event")
.putAttribute("level", 5)
.putAttribute("full_message", "This is a test of the JSON conversion")
.putAttribute("timestamp", timestamp)
.putAttribute("test_attribute_1", 100)
.putAttribute("test_attribute_2", "200")
.putAttribute("can_pass_booleans", true);

String json = jsonizer.getJsonBody(event);
assertThat(json).isEqualTo("{\"version\": \"" + VERSION + "\", \"host\": \"" + HOST + "\", \"short_message\": \"" +
event.name() + "\", \"full_message\": \"This is a test of the JSON conversion\", " +
"\"timestamp\": " + timestamp + ", \"level\": 5, \"_test_attribute_1\": 100, " +
"\"_test_attribute_2\": \"200\", \"_can_pass_booleans\": true}");
}

@Test
public void getJsonFromMapRecursive_emptyMap()
{
Expand All @@ -113,13 +133,14 @@ public void getJsonFromMapRecursive_emptyMap()
@Test
public void getJsonFromMapRecursive_flatMapStructure()
{
Map<String, Object> map = new HashMap<>();
Map<String, Object> map = new LinkedHashMap<>();
map.put("one", "abc");
map.put("two", 123);
map.put("three", 321.123d);
map.put("four", true);

String json = jsonizer.getJsonFromMapRecursive(map);
assertThat(json).isEqualTo("{\"one\": \"abc\", \"two\": 123, \"three\": 321.123}");
assertThat(json).isEqualTo("{\"one\": \"abc\", \"two\": 123, \"three\": 321.123, \"four\": true}");
}

@Test
Expand Down Expand Up @@ -156,18 +177,21 @@ public void getJsonFromListRecursive()
List<Object> list = new LinkedList<>();
List<Object> innerListOne = new LinkedList<>();
List<Object> innerListTwo = new LinkedList<>();
List<Object> innerListThree = new LinkedList<>();
List<Object> levelThreeListOne = new LinkedList<>();
List<Object> levelThreeListTwo = new LinkedList<>();

levelThreeListOne.add(31);
levelThreeListTwo.add(32);
innerListOne.add(levelThreeListOne);
innerListTwo.add(levelThreeListTwo);
innerListThree.add(true);
list.add(innerListOne);
list.add(innerListTwo);
list.add(innerListThree);

String json = jsonizer.getJsonFromListRecursive(list);
assertThat(json).isEqualTo("[[[31]], [[32]]]");
assertThat(json).isEqualTo("[[[31]], [[32]], [true]]");

}

Expand Down