Skip to content

Commit

Permalink
split custom object serialization into two steps due to lack of commo…
Browse files Browse the repository at this point in the history
…n parent casting syntax in new pattern matching syntax.
  • Loading branch information
jpe42 committed Sep 22, 2023
1 parent fc57b2d commit 88eea8d
Showing 1 changed file with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,27 +231,27 @@ private static String escapeQuotes(final String str) {

static String toJson(final Map<String, Object> 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(",", "{", "}"));
}

Expand Down

0 comments on commit 88eea8d

Please sign in to comment.