diff --git a/loki-logback-appender/src/main/java/com/github/loki4j/logback/json/JsonEventWriter.java b/loki-logback-appender/src/main/java/com/github/loki4j/logback/json/JsonEventWriter.java index 2ff01094..e744eb17 100644 --- a/loki-logback-appender/src/main/java/com/github/loki4j/logback/json/JsonEventWriter.java +++ b/loki-logback-appender/src/main/java/com/github/loki4j/logback/json/JsonEventWriter.java @@ -24,13 +24,28 @@ public void writeEndObject() { raw.writeByte(OBJECT_END); } + public void writeBeginArray() { + raw.writeByte(ARRAY_START); + } + + public void writeEndArray() { + raw.writeByte(ARRAY_END); + } + public void writeFieldSeparator() { raw.writeByte(COMMA); } + public void writeArraySeparator() { + raw.writeByte(COMMA); + } + public void writeObjectField(String fieldName, Object value) { serializeFieldName(fieldName); + writeObjectValue(value); + } + public void writeObjectValue(Object value) { // Object is a reference type, first check the value for null if (value == null) { raw.writeNull(); @@ -40,11 +55,13 @@ public void writeObjectField(String fieldName, Object value) { if (value instanceof String) raw.writeString((String) value); else if (value instanceof Integer) - NumberConverter.serialize(((Integer) value).longValue(), raw); + writeNumericValue(((Integer) value).longValue()); else if (value instanceof Long) - NumberConverter.serialize((long) value, raw); + writeNumericValue((long) value); else if (value instanceof Boolean) raw.writeBoolean((boolean) value); + else if (value instanceof Iterable) + serializeIterable((Iterable) value); else if (value instanceof RawJsonString) raw.writeRawAscii(((RawJsonString) value).value); else @@ -53,15 +70,24 @@ else if (value instanceof RawJsonString) public void writeStringField(String fieldName, String value) { serializeFieldName(fieldName); + writeStringValue(value); + } + + public void writeStringValue(String value) { // String is a reference type, first check the value for null - if (value == null) + if (value == null) { raw.writeNull(); - else + } else { raw.writeString(value); + } } public void writeNumericField(String fieldName, long value) { serializeFieldName(fieldName); + writeNumericValue(value); + } + + public void writeNumericValue(long value) { NumberConverter.serialize(value, raw); } @@ -70,6 +96,17 @@ private void serializeFieldName(String fieldName) { raw.writeByte(SEMI); } + private void serializeIterable(Iterable iterable) { + writeBeginArray(); + var it = iterable.iterator(); + while (it.hasNext()) { + writeObjectValue(it.next()); + if (it.hasNext()) + writeArraySeparator(); + } + writeEndArray(); + } + public String toString() { return raw.toString(); } diff --git a/loki-logback-appender/src/test/java/com/github/loki4j/logback/json/JsonEventWriterTest.java b/loki-logback-appender/src/test/java/com/github/loki4j/logback/json/JsonEventWriterTest.java index 768feadf..d7b5a843 100644 --- a/loki-logback-appender/src/test/java/com/github/loki4j/logback/json/JsonEventWriterTest.java +++ b/loki-logback-appender/src/test/java/com/github/loki4j/logback/json/JsonEventWriterTest.java @@ -2,6 +2,9 @@ import static org.junit.Assert.assertEquals; +import java.util.List; +import java.util.Set; + import org.junit.Test; public class JsonEventWriterTest { @@ -71,4 +74,34 @@ public void testWriteRawJsonStringObject() { assertEquals("{\"raw\":{\"arr\":[1,2,3],\"val\":null}}", writer.toString()); } + + @Test + public void testWriteListOfStrings() { + var writer = new JsonEventWriter(10); + writer.writeBeginObject(); + writer.writeObjectField("it", List.of("one", "two", "three")); + writer.writeEndObject(); + + assertEquals("{\"it\":[\"one\",\"two\",\"three\"]}", writer.toString()); + } + + @Test + public void testWriteSetOfInts() { + var writer = new JsonEventWriter(10); + writer.writeBeginObject(); + writer.writeObjectField("it", Set.of(1, 2, 3)); + writer.writeEndObject(); + + assertEquals("{\"it\":[1,2,3]}", writer.toString()); + } + + @Test + public void testWriteEmptyList() { + var writer = new JsonEventWriter(10); + writer.writeBeginObject(); + writer.writeObjectField("it", List.of()); + writer.writeEndObject(); + + assertEquals("{\"it\":[]}", writer.toString()); + } }