Skip to content

Commit

Permalink
more serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
brig committed Dec 29, 2024
1 parent 2da58c0 commit dd30b94
Showing 1 changed file with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class SaveLastErrorCommand implements Command {
// for backward compatibility (java8 concord 1.92.0 version)
private static final long serialVersionUID = 5759484819869224819L;

private static final TypeReference<Map<String, Object>> MAP_TYPE = new TypeReference<Map<String, Object>>() {
private static final TypeReference<Map<String, Object>> MAP_TYPE = new TypeReference<>() {
};

private static final AtomicInteger idGenerator = new AtomicInteger(1);
Expand Down Expand Up @@ -84,6 +84,7 @@ private static ObjectMapper createMapper() {
var module = new SimpleModule();
module.addSerializer(ParallelExecutionException.class, new ParallelExceptionSerializer());
module.addSerializer(LoggedException.class, new LoggedExceptionSerializer());
module.addSerializer(UserDefinedException.class, new UserDefinedExceptionSerializer());
module.addSerializer(Exception.class, new ExceptionSerializer());

var om = new ObjectMapper();
Expand Down Expand Up @@ -118,20 +119,28 @@ public void serialize(LoggedException exception, JsonGenerator gen, SerializerPr
}
}

private static class UserDefinedExceptionSerializer extends JsonSerializer<UserDefinedException> {

@Override
public void serialize(UserDefinedException exception, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeStartObject();
gen.writeNumberField("@id", idGenerator.getAndIncrement());
gen.writeStringField("message", exception.getMessage());
if (exception.getPayload() != null && !exception.getPayload().isEmpty()) {
gen.writeObjectField("payload", exception.getPayload());
}
gen.writeEndObject();
}
}

private static class ExceptionSerializer extends JsonSerializer<Exception> {

@Override
public void serialize(Exception exception, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeStartObject();
gen.writeNumberField("@id", idGenerator.getAndIncrement());
gen.writeStringField("message", exception.getMessage());
if (exception instanceof UserDefinedException ue) {
if (ue.getPayload() != null && !ue.getPayload().isEmpty()) {
gen.writeObjectField("payload", ue.getPayload());
}
} else {
gen.writeStringField("type", exception.getClass().getName());
}
gen.writeStringField("type", exception.getClass().getName());
gen.writeEndObject();
}
}
Expand Down

0 comments on commit dd30b94

Please sign in to comment.