From 88eea8d74d9e55d59cd4793766aa5e2ade45de82 Mon Sep 17 00:00:00 2001 From: jim Date: Fri, 22 Sep 2023 16:04:27 -0500 Subject: [PATCH] split custom object serialization into two steps due to lack of common parent casting syntax in new pattern matching syntax. --- .../PagerDutyChangeEventPayloadRecord.java | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/systems.comodal.pagerduty_event_client/src/main/java/systems/comodal/pagerduty/event/data/PagerDutyChangeEventPayloadRecord.java b/systems.comodal.pagerduty_event_client/src/main/java/systems/comodal/pagerduty/event/data/PagerDutyChangeEventPayloadRecord.java index 9be252a..c52a6f0 100644 --- a/systems.comodal.pagerduty_event_client/src/main/java/systems/comodal/pagerduty/event/data/PagerDutyChangeEventPayloadRecord.java +++ b/systems.comodal.pagerduty_event_client/src/main/java/systems/comodal/pagerduty/event/data/PagerDutyChangeEventPayloadRecord.java @@ -231,27 +231,27 @@ private static String escapeQuotes(final String str) { static String toJson(final Map object) { return object.entrySet().stream().map(entry -> { - final var key = entry.getKey(); final var val = entry.getValue(); - return switch (val) { - case null -> String.format(""" - "%s":null""", key); - case BigDecimal bigDecimal -> String.format(""" - "%s":"%s\"""", key, bigDecimal.toPlainString()); - case BigInteger bigInteger -> String.format(""" - "%s":"%s\"""", key, bigInteger); - case Number number -> String.format(""" - "%s":%s""", key, number); - case Boolean bool -> String.format(""" - "%s":%s""", key, bool); - //noinspection DataFlowIssue + final var stringOrRawVal = switch (val) { + case null -> null; + case BigDecimal bigDecimal -> bigDecimal.toPlainString(); + case BigInteger bigInteger -> bigInteger.toString(); + case Number number -> number; + case Boolean bool -> bool; case Object obj -> { final var str = obj.toString(); - yield String.format(""" - "%s":"%s\"""", - key, str.indexOf('"') < 0 ? str : escapeQuotes(str)); + yield str.indexOf('"') < 0 ? str : escapeQuotes(str); } }; + final var key = entry.getKey(); + return switch (stringOrRawVal) { + case null -> String.format(""" + "%s":null""", key); + case String string -> String.format(""" + "%s":"%s\"""", key, string); + case Object obj -> String.format(""" + "%s":%s""", key, obj); + }; }).collect(Collectors.joining(",", "{", "}")); }