Skip to content

Commit 47c1067

Browse files
authored
Fix serializing null values (#93)
1 parent 6594a1f commit 47c1067

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

ecs-logging-core/src/main/java/co/elastic/logging/JsonUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ public final class JsonUtils {
5858
}
5959

6060
public static void quoteAsString(CharSequence content, StringBuilder sb) {
61+
if (content == null) {
62+
sb.append("null");
63+
return;
64+
}
6165
final int[] escCodes = sOutputEscapes128;
6266
final int escLen = escCodes.length;
6367
for (int i = 0, len = content.length(); i < len; ++i) {

ecs-logging-core/src/test/java/co/elastic/logging/EcsJsonSerializerTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525
package co.elastic.logging;
2626

27+
import com.fasterxml.jackson.core.JsonProcessingException;
2728
import com.fasterxml.jackson.databind.JsonNode;
2829
import com.fasterxml.jackson.databind.ObjectMapper;
2930
import org.junit.jupiter.api.Test;
@@ -54,6 +55,13 @@ void serializeExceptionAsString() throws IOException {
5455
assertThat(jsonNode.get("error.stack_trace").textValue()).isEqualTo(stringWriter.toString());
5556
}
5657

58+
@Test
59+
void serializeNullDoesNotThrowAnException() throws JsonProcessingException {
60+
StringBuilder stringBuilder = new StringBuilder();
61+
EcsJsonSerializer.serializeFormattedMessage(stringBuilder, null);
62+
assertThat(stringBuilder.toString()).isEqualTo("\"message\":\"null\", ");
63+
}
64+
5765
@Test
5866
void serializeExceptionAsArray() throws IOException {
5967
Exception exception = new Exception("foo");

0 commit comments

Comments
 (0)