From 7bb91ee243f37b6fee7227f81ba3e156da97bafe Mon Sep 17 00:00:00 2001 From: jim Date: Wed, 20 Sep 2023 17:57:01 -0500 Subject: [PATCH] fix/re-add proper Boolean json serialization. --- README.md | 47 ++++++++++++------- .../data/PagerDutyEventPayloadBuilder.java | 17 ++++--- 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 74982f6..c4767dd 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,19 @@ # PagerDuty Event Client -This client aims to be compliant with the latest version of the [PagerDuty Event API](https://v2.developer.pagerduty.com/docs/events-api-v2), currently V2. +This client aims to be compliant with the latest version of +the [PagerDuty Event API](https://v2.developer.pagerduty.com/docs/events-api-v2), currently V2. It also only aims to be +supported against the latest GA OpenJDK release. ## Hello Event Trigger ```java -var client = PagerDutyEventClient.build() +var client=PagerDutyEventClient.build() .defaultClientName("CLIENT_NAME") .defaultRoutingKey("INTEGRATION_KEY") .authToken("AUTH_TOKEN") .create(); -var payload = PagerDutyEventPayload.build() + var payload=PagerDutyEventPayload.build() .summary("summary") .source("source") .severity(PagerDutySeverity.critical) @@ -19,29 +21,38 @@ var payload = PagerDutyEventPayload.build() .component("component") .group("group") .type("class") - .customDetails("num-metric", 1) - .customDetails("string-metric", "val") + .customDetails("num-metric",1) + .customDetails("string-metric","val") .link(PagerDutyLinkRef.build() - .href("https://github.com/comodal/pagerduty-client") - .text("Github pagerduty-client") - .create()) + .href("https://github.com/comodal/pagerduty-client") + .text("Github pagerduty-client") + .create()) .image(PagerDutyImageRef.build() - .src("https://www.pagerduty.com/wp-content/uploads/2016/05/pagerduty-logo-green.png") - .href("https://www.pagerduty.com/") - .alt("pagerduty") - .create()) + .src("https://www.pagerduty.com/wp-content/uploads/2016/05/pagerduty-logo-green.png") + .href("https://www.pagerduty.com/") + .alt("pagerduty") + .create()) .create(); -var triggerResponse = client.triggerDefaultRouteEvent(payload).join(); -System.out.println(triggerResponse); + var triggerResponse=client.triggerDefaultRouteEvent(payload).join(); + System.out.println(triggerResponse); -var resolveResponse = client.resolveEvent(triggerResponse.getDedupeKey()).join(); -System.out.println(resolveResponse); + var resolveResponse=client.resolveEvent(triggerResponse.getDedupeKey()).join(); + System.out.println(resolveResponse); ``` -## Library Layout +## Library Layout -The core module [systems.comodal.pagerduty_event_client](systems.comodal.pagerduty_event_client/src/main/java/module-info.java) has direct dependencies on `java.base`, `java.net.http` and a [PagerDutyEventAdapterFactory](systems.comodal.pagerduty_event_client/src/main/java/systems/comodal/pagerduty/event/data/adapters/PagerDutyEventAdapterFactory.java). The module [systems.comodal.pagerduty_event_json_iterator_adapter](systems.comodal.pagerduty_event_json_iterator_adapter/src/main/java/module-info.java) provides this factory and has a dependency on [systems.comodal.json_iterator](https://github.com/comodal/json-iterator). This separation is intended to make it easy to provide your own json parser if desired. +The core +module [systems.comodal.pagerduty_event_client](systems.comodal.pagerduty_event_client/src/main/java/module-info.java) +has direct dependencies on `java.base`, `java.net.http` and +a [PagerDutyEventAdapterFactory](systems.comodal.pagerduty_event_client/src/main/java/systems/comodal/pagerduty/event/data/adapters/PagerDutyEventAdapterFactory.java). + +The [systems.comodal.pagerduty_event_json_iterator_adapter](systems.comodal.pagerduty_event_json_iterator_adapter/src/main/java/module-info.java) +module provides this factory and has a dependency +on [systems.comodal.json_iterator](https://github.com/comodal/json-iterator). +This separation is intended to make it easy for you to provide your own json parser (dependency) if desired. If not, +just use it, it is minimal and has no further transitive dependencies. ```bash > ./gradlew pagerduty-event-json-iterator-adapter:dependencies diff --git a/systems.comodal.pagerduty_event_client/src/main/java/systems/comodal/pagerduty/event/data/PagerDutyEventPayloadBuilder.java b/systems.comodal.pagerduty_event_client/src/main/java/systems/comodal/pagerduty/event/data/PagerDutyEventPayloadBuilder.java index f3fed99..56c5180 100644 --- a/systems.comodal.pagerduty_event_client/src/main/java/systems/comodal/pagerduty/event/data/PagerDutyEventPayloadBuilder.java +++ b/systems.comodal.pagerduty_event_client/src/main/java/systems/comodal/pagerduty/event/data/PagerDutyEventPayloadBuilder.java @@ -295,12 +295,17 @@ private static String toJson(final Map object) { final var key = entry.getKey(); final var val = entry.getValue(); return switch (val) { - case BigDecimal num -> String.format(""" - "%s":"%s\"""", key, num.toPlainString()); - case BigInteger num -> String.format(""" - "%s":"%s\"""", key, num); - case Number num -> String.format(""" - "%s":%s""", key, num); + 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 case Object obj -> { final var str = obj.toString(); yield String.format("""