-
-
Notifications
You must be signed in to change notification settings - Fork 3
Add Notice JSON serialize/deserialize API with round-trip test #127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| package com.eternalcode.multification.notice; | ||
|
|
||
| import com.eternalcode.multification.notice.resolver.NoticeDeserializeResult; | ||
| import com.eternalcode.multification.notice.resolver.NoticeContent; | ||
| import com.eternalcode.multification.notice.resolver.NoticeResolverRegistry; | ||
| import com.eternalcode.multification.notice.resolver.NoticeSerdesResult; | ||
| import com.google.gson.JsonArray; | ||
| import com.google.gson.JsonElement; | ||
| import com.google.gson.JsonNull; | ||
| import com.google.gson.JsonObject; | ||
| import com.google.gson.JsonParseException; | ||
| import com.google.gson.JsonParser; | ||
| import com.google.gson.JsonPrimitive; | ||
| import java.util.ArrayList; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
||
| final class NoticeJsonCodec { | ||
|
|
||
| private NoticeJsonCodec() { | ||
| } | ||
|
|
||
| static String serialize(Notice notice, NoticeResolverRegistry noticeRegistry) { | ||
| JsonObject root = new JsonObject(); | ||
|
|
||
| for (NoticePart<?> part : notice.parts()) { | ||
| NoticeSerdesResult serializedPart = noticeRegistry.serialize(part); | ||
| root.add(part.noticeKey().key(), toJsonElement(serializedPart)); | ||
| } | ||
|
|
||
| return root.toString(); | ||
| } | ||
|
|
||
| static Notice deserialize(String raw, NoticeResolverRegistry noticeRegistry) { | ||
| JsonElement parsed = JsonParser.parseString(raw); | ||
| if (!parsed.isJsonObject()) { | ||
| throw new IllegalArgumentException("Notice JSON must be an object"); | ||
| } | ||
|
|
||
| Notice.Builder builder = Notice.builder(); | ||
| JsonObject root = parsed.getAsJsonObject(); | ||
|
|
||
| for (Map.Entry<String, JsonElement> entry : root.entrySet()) { | ||
| String key = entry.getKey(); | ||
| NoticeSerdesResult result = toSerdesResult(key, entry.getValue()); | ||
| Optional<NoticeDeserializeResult<?>> deserialized = noticeRegistry.deserialize(key, result); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
|
||
| if (deserialized.isEmpty()) { | ||
| throw new IllegalArgumentException("Unsupported notice key: " + key); | ||
| } | ||
|
|
||
| withPart(builder, deserialized.get()); | ||
| } | ||
|
|
||
| return builder.build(); | ||
| } | ||
|
|
||
| private static JsonElement toJsonElement(NoticeSerdesResult result) { | ||
| if (result instanceof NoticeSerdesResult.Single single) { | ||
| return new JsonPrimitive(single.element()); | ||
| } | ||
|
|
||
| if (result instanceof NoticeSerdesResult.Multiple multiple) { | ||
| JsonArray array = new JsonArray(); | ||
| for (String element : multiple.elements()) { | ||
| array.add(element); | ||
| } | ||
| return array; | ||
| } | ||
|
|
||
| if (result instanceof NoticeSerdesResult.Section section) { | ||
| JsonObject object = new JsonObject(); | ||
| for (Map.Entry<String, String> sectionEntry : section.elements().entrySet()) { | ||
| object.addProperty(sectionEntry.getKey(), sectionEntry.getValue()); | ||
| } | ||
| return object; | ||
| } | ||
|
|
||
| if (result instanceof NoticeSerdesResult.Empty) { | ||
| return JsonNull.INSTANCE; | ||
| } | ||
|
Comment on lines
+81
to
+83
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Serializing Useful? React with 👍 / 👎. |
||
|
|
||
| throw new IllegalArgumentException("Unsupported result type: " + result.getClass().getName()); | ||
| } | ||
|
Comment on lines
+60
to
+86
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The private static JsonElement toJsonElement(NoticeSerdesResult result) {
return switch (result) {
case NoticeSerdesResult.Single single -> new JsonPrimitive(single.element());
case NoticeSerdesResult.Multiple multiple -> {
JsonArray array = new JsonArray();
multiple.elements().forEach(array::add);
yield array;
}
case NoticeSerdesResult.Section section -> {
JsonObject object = new JsonObject();
section.elements().forEach(object::addProperty);
yield object;
}
case NoticeSerdesResult.Empty empty -> JsonNull.INSTANCE;
};
} |
||
|
|
||
| private static NoticeSerdesResult toSerdesResult(String key, JsonElement element) { | ||
| if (element.isJsonNull()) { | ||
| return new NoticeSerdesResult.Empty(); | ||
| } | ||
|
|
||
| if (element.isJsonPrimitive() && element.getAsJsonPrimitive().isString()) { | ||
| return new NoticeSerdesResult.Single(element.getAsString()); | ||
| } | ||
|
|
||
| if (element.isJsonArray()) { | ||
| return new NoticeSerdesResult.Multiple(toStringList(key, element.getAsJsonArray())); | ||
| } | ||
|
|
||
| if (element.isJsonObject()) { | ||
| return new NoticeSerdesResult.Section(toStringMap(key, element.getAsJsonObject())); | ||
| } | ||
|
|
||
| throw new JsonParseException("Unsupported JSON value for key '" + key + "'"); | ||
| } | ||
|
|
||
| private static List<String> toStringList(String key, JsonArray jsonArray) { | ||
| List<String> values = new ArrayList<>(); | ||
| for (JsonElement jsonElement : jsonArray) { | ||
| if (!jsonElement.isJsonPrimitive() || !jsonElement.getAsJsonPrimitive().isString()) { | ||
| throw new JsonParseException("All array elements for key '" + key + "' must be strings"); | ||
| } | ||
| values.add(jsonElement.getAsString()); | ||
| } | ||
| return values; | ||
| } | ||
|
Comment on lines
+108
to
+117
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method can be simplified by using Java Streams. This would make the code more declarative and potentially more readable. private static List<String> toStringList(String key, JsonArray jsonArray) {
return jsonArray.asList().stream()
.map(jsonElement -> {
if (!jsonElement.isJsonPrimitive() || !jsonElement.getAsJsonPrimitive().isString()) {
throw new JsonParseException("All array elements for key '" + key + "' must be strings");
}
return jsonElement.getAsString();
})
.toList();
} |
||
|
|
||
| private static Map<String, String> toStringMap(String key, JsonObject jsonObject) { | ||
| Map<String, String> values = new LinkedHashMap<>(); | ||
| for (Map.Entry<String, JsonElement> jsonEntry : jsonObject.entrySet()) { | ||
| JsonElement jsonElement = jsonEntry.getValue(); | ||
| if (!jsonElement.isJsonPrimitive() || !jsonElement.getAsJsonPrimitive().isString()) { | ||
| throw new JsonParseException("All object values for key '" + key + "' must be strings"); | ||
| } | ||
| values.put(jsonEntry.getKey(), jsonElement.getAsString()); | ||
| } | ||
| return values; | ||
| } | ||
|
Comment on lines
+119
to
+129
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method can be refactored to use Java Streams and private static Map<String, String> toStringMap(String key, JsonObject jsonObject) {
return jsonObject.entrySet().stream()
.collect(java.util.stream.Collectors.toMap(
Map.Entry::getKey,
entry -> {
JsonElement jsonElement = entry.getValue();
if (!jsonElement.isJsonPrimitive() || !jsonElement.getAsJsonPrimitive().isString()) {
throw new JsonParseException("All object values for key '" + key + "' must be strings");
}
return jsonElement.getAsString();
},
(v1, v2) -> v2,
LinkedHashMap::new
));
} |
||
|
|
||
| private static <T extends NoticeContent> void withPart( | ||
| Notice.Builder builder, | ||
| NoticeDeserializeResult<T> noticeResult | ||
| ) { | ||
| builder.withPart(noticeResult.noticeKey(), noticeResult.content()); | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When deserializing arbitrary JSON, this line calls
noticeRegistry.deserialize(key, result)before validating thatkeyis registered; for unknown keys,NoticeResolverRegistry.deserializedereferences a null resolver set and throws aNullPointerException, so the intendedIllegalArgumentException("Unsupported notice key")path is never reached. Any typo or extra field in input JSON will crash deserialization with an opaque NPE instead of a controlled validation error.Useful? React with 👍 / 👎.