Skip to content

Commit

Permalink
Customizable field serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
nehaev committed Dec 15, 2024
1 parent c6d5500 commit 8fbf930
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void writeArraySeparator() {
}

public void writeObjectField(String fieldName, Object value) {
serializeFieldName(fieldName);
writeFieldName(fieldName);
writeObjectValue(value);
}

Expand Down Expand Up @@ -69,7 +69,7 @@ else if (value instanceof RawJsonString)
}

public void writeStringField(String fieldName, String value) {
serializeFieldName(fieldName);
writeFieldName(fieldName);
writeStringValue(value);
}

Expand All @@ -83,15 +83,15 @@ public void writeStringValue(String value) {
}

public void writeNumericField(String fieldName, long value) {
serializeFieldName(fieldName);
writeFieldName(fieldName);
writeNumericValue(value);
}

public void writeNumericValue(long value) {
NumberConverter.serialize(value, raw);
}

private void serializeFieldName(String fieldName) {
public void writeFieldName(String fieldName) {
raw.writeString(fieldName);
raw.writeByte(SEMI);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ public class KeyValuePairsJsonProvider extends AbstractFieldCollectionJsonProvid

public static final String FIELD_KVP_PREFIX = "kvp_";

/**
* Json serializer to use for each field specified for this KVP provider.
*/
private JsonFieldSerializer fieldSerializer = (writer, name, value) -> writer.writeObjectField(name, value);

public KeyValuePairsJsonProvider() {
setPrefix(FIELD_KVP_PREFIX);
}
Expand All @@ -31,6 +36,14 @@ protected Object extractValue(KeyValuePair entry) {

@Override
protected void writeField(JsonEventWriter writer, String fieldName, Object fieldValue) {
writer.writeObjectField(fieldName, fieldValue);
fieldSerializer.writeField(writer, fieldName, fieldValue);
}

public JsonFieldSerializer getFieldSerializer() {
return fieldSerializer;
}

public void setFieldSerializer(JsonFieldSerializer fieldSerializer) {
this.fieldSerializer = fieldSerializer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,38 @@ public void testOmitPrefix() {

provider.stop();
}

@Test
public void testCustomFieldSerializer() {
var event = loggingEvent(101L, Level.DEBUG, "io.test.TestApp", "thread-1", "m2-line1", null);
event.setKeyValuePairs(Arrays.asList(
new KeyValuePair("property1", "value1"),
new KeyValuePair("property2", "value2")
));

var provider = new KeyValuePairsJsonProvider();
provider.setFieldSerializer(
(writer, name, value) -> {
writer.writeFieldName(name + "_arr");
writer.writeBeginArray();
writer.writeStringValue(name);
writer.writeArraySeparator();
writer.writeStringValue(value.toString());
writer.writeEndArray();
}
);
provider.start();

assertTrue("canWrite", provider.canWrite(event));

var writer = new JsonEventWriter(0);
provider.writeTo(writer, event, false);

assertEquals("writeTo",
"\"kvp_property1_arr\":[\"kvp_property1\",\"value1\"],\"kvp_property2_arr\":[\"kvp_property2\",\"value2\"]",
writer.toString()
);

provider.stop();
}
}

0 comments on commit 8fbf930

Please sign in to comment.