暂存提交
This commit is contained in:
+33
-1
@@ -22,8 +22,20 @@ public final class EventPayloadCanonicalizer {
|
||||
if (payload == null || payload.isNull()) {
|
||||
throw new IllegalArgumentException("事件负载不能为空");
|
||||
}
|
||||
return write(sort(payload));
|
||||
}
|
||||
|
||||
/** 仅用于接收规范化数字规则上线前已经生成的历史摘要。 */
|
||||
public static byte[] canonicalizeLegacy(JsonNode payload) {
|
||||
if (payload == null || payload.isNull()) {
|
||||
throw new IllegalArgumentException("事件负载不能为空");
|
||||
}
|
||||
return write(sortLegacy(payload));
|
||||
}
|
||||
|
||||
private static byte[] write(JsonNode payload) {
|
||||
try {
|
||||
return OBJECT_MAPPER.writeValueAsBytes(sort(payload));
|
||||
return OBJECT_MAPPER.writeValueAsBytes(payload);
|
||||
} catch (JsonProcessingException exception) {
|
||||
throw new IllegalArgumentException("事件负载无法规范化", exception);
|
||||
}
|
||||
@@ -43,6 +55,26 @@ public final class EventPayloadCanonicalizer {
|
||||
node.forEach(child -> result.add(sort(child)));
|
||||
return result;
|
||||
}
|
||||
if (node.isNumber()) {
|
||||
return com.fasterxml.jackson.databind.node.DecimalNode.valueOf(node.decimalValue().stripTrailingZeros());
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
private static JsonNode sortLegacy(JsonNode node) {
|
||||
if (node.isObject()) {
|
||||
var names = new java.util.ArrayList<String>();
|
||||
node.fieldNames().forEachRemaining(names::add);
|
||||
names.sort(String::compareTo);
|
||||
var result = OBJECT_MAPPER.createObjectNode();
|
||||
names.forEach(name -> result.set(name, sortLegacy(node.get(name))));
|
||||
return result;
|
||||
}
|
||||
if (node.isArray()) {
|
||||
var result = OBJECT_MAPPER.createArrayNode();
|
||||
node.forEach(child -> result.add(sortLegacy(child)));
|
||||
return result;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user