Skip to content

Fix for issue #92 : when trying to serialize null, serialize it as th… #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 29, 2020
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 @@ -58,6 +58,10 @@ public final class JsonUtils {
}

public static void quoteAsString(CharSequence content, StringBuilder sb) {
if (content == null) {
sb.append("null");
return;
}
final int[] escCodes = sOutputEscapes128;
final int escLen = escCodes.length;
for (int i = 0, len = content.length(); i < len; ++i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
package co.elastic.logging;

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

@Test
void serializeNullDoesNotThrowAnException() throws JsonProcessingException {
StringBuilder stringBuilder = new StringBuilder();
EcsJsonSerializer.serializeFormattedMessage(stringBuilder, null);
assertThat(stringBuilder.toString()).isEqualTo("\"message\":\"null\", ");
}

@Test
void serializeExceptionAsArray() throws IOException {
Exception exception = new Exception("foo");
Expand Down