|
| 1 | +package com.eternalcode.multification.notice; |
| 2 | + |
| 3 | +import com.eternalcode.multification.notice.resolver.NoticeDeserializeResult; |
| 4 | +import com.eternalcode.multification.notice.resolver.NoticeContent; |
| 5 | +import com.eternalcode.multification.notice.resolver.NoticeResolverRegistry; |
| 6 | +import com.eternalcode.multification.notice.resolver.NoticeSerdesResult; |
| 7 | +import com.google.gson.JsonArray; |
| 8 | +import com.google.gson.JsonElement; |
| 9 | +import com.google.gson.JsonNull; |
| 10 | +import com.google.gson.JsonObject; |
| 11 | +import com.google.gson.JsonParseException; |
| 12 | +import com.google.gson.JsonParser; |
| 13 | +import com.google.gson.JsonPrimitive; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.LinkedHashMap; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.Optional; |
| 19 | + |
| 20 | +final class NoticeJsonCodec { |
| 21 | + |
| 22 | + private NoticeJsonCodec() { |
| 23 | + } |
| 24 | + |
| 25 | + static String serialize(Notice notice, NoticeResolverRegistry noticeRegistry) { |
| 26 | + JsonObject root = new JsonObject(); |
| 27 | + |
| 28 | + for (NoticePart<?> part : notice.parts()) { |
| 29 | + NoticeSerdesResult serializedPart = noticeRegistry.serialize(part); |
| 30 | + root.add(part.noticeKey().key(), toJsonElement(serializedPart)); |
| 31 | + } |
| 32 | + |
| 33 | + return root.toString(); |
| 34 | + } |
| 35 | + |
| 36 | + static Notice deserialize(String raw, NoticeResolverRegistry noticeRegistry) { |
| 37 | + JsonElement parsed = JsonParser.parseString(raw); |
| 38 | + if (!parsed.isJsonObject()) { |
| 39 | + throw new IllegalArgumentException("Notice JSON must be an object"); |
| 40 | + } |
| 41 | + |
| 42 | + Notice.Builder builder = Notice.builder(); |
| 43 | + JsonObject root = parsed.getAsJsonObject(); |
| 44 | + |
| 45 | + for (Map.Entry<String, JsonElement> entry : root.entrySet()) { |
| 46 | + String key = entry.getKey(); |
| 47 | + NoticeSerdesResult result = toSerdesResult(key, entry.getValue()); |
| 48 | + Optional<NoticeDeserializeResult<?>> deserialized = noticeRegistry.deserialize(key, result); |
| 49 | + |
| 50 | + if (deserialized.isEmpty()) { |
| 51 | + throw new IllegalArgumentException("Unsupported notice key: " + key); |
| 52 | + } |
| 53 | + |
| 54 | + withPart(builder, deserialized.get()); |
| 55 | + } |
| 56 | + |
| 57 | + return builder.build(); |
| 58 | + } |
| 59 | + |
| 60 | + private static JsonElement toJsonElement(NoticeSerdesResult result) { |
| 61 | + if (result instanceof NoticeSerdesResult.Single single) { |
| 62 | + return new JsonPrimitive(single.element()); |
| 63 | + } |
| 64 | + |
| 65 | + if (result instanceof NoticeSerdesResult.Multiple multiple) { |
| 66 | + JsonArray array = new JsonArray(); |
| 67 | + for (String element : multiple.elements()) { |
| 68 | + array.add(element); |
| 69 | + } |
| 70 | + return array; |
| 71 | + } |
| 72 | + |
| 73 | + if (result instanceof NoticeSerdesResult.Section section) { |
| 74 | + JsonObject object = new JsonObject(); |
| 75 | + for (Map.Entry<String, String> sectionEntry : section.elements().entrySet()) { |
| 76 | + object.addProperty(sectionEntry.getKey(), sectionEntry.getValue()); |
| 77 | + } |
| 78 | + return object; |
| 79 | + } |
| 80 | + |
| 81 | + if (result instanceof NoticeSerdesResult.Empty) { |
| 82 | + return JsonNull.INSTANCE; |
| 83 | + } |
| 84 | + |
| 85 | + throw new IllegalArgumentException("Unsupported result type: " + result.getClass().getName()); |
| 86 | + } |
| 87 | + |
| 88 | + private static NoticeSerdesResult toSerdesResult(String key, JsonElement element) { |
| 89 | + if (element.isJsonNull()) { |
| 90 | + return new NoticeSerdesResult.Empty(); |
| 91 | + } |
| 92 | + |
| 93 | + if (element.isJsonPrimitive() && element.getAsJsonPrimitive().isString()) { |
| 94 | + return new NoticeSerdesResult.Single(element.getAsString()); |
| 95 | + } |
| 96 | + |
| 97 | + if (element.isJsonArray()) { |
| 98 | + return new NoticeSerdesResult.Multiple(toStringList(key, element.getAsJsonArray())); |
| 99 | + } |
| 100 | + |
| 101 | + if (element.isJsonObject()) { |
| 102 | + return new NoticeSerdesResult.Section(toStringMap(key, element.getAsJsonObject())); |
| 103 | + } |
| 104 | + |
| 105 | + throw new JsonParseException("Unsupported JSON value for key '" + key + "'"); |
| 106 | + } |
| 107 | + |
| 108 | + private static List<String> toStringList(String key, JsonArray jsonArray) { |
| 109 | + List<String> values = new ArrayList<>(); |
| 110 | + for (JsonElement jsonElement : jsonArray) { |
| 111 | + if (!jsonElement.isJsonPrimitive() || !jsonElement.getAsJsonPrimitive().isString()) { |
| 112 | + throw new JsonParseException("All array elements for key '" + key + "' must be strings"); |
| 113 | + } |
| 114 | + values.add(jsonElement.getAsString()); |
| 115 | + } |
| 116 | + return values; |
| 117 | + } |
| 118 | + |
| 119 | + private static Map<String, String> toStringMap(String key, JsonObject jsonObject) { |
| 120 | + Map<String, String> values = new LinkedHashMap<>(); |
| 121 | + for (Map.Entry<String, JsonElement> jsonEntry : jsonObject.entrySet()) { |
| 122 | + JsonElement jsonElement = jsonEntry.getValue(); |
| 123 | + if (!jsonElement.isJsonPrimitive() || !jsonElement.getAsJsonPrimitive().isString()) { |
| 124 | + throw new JsonParseException("All object values for key '" + key + "' must be strings"); |
| 125 | + } |
| 126 | + values.put(jsonEntry.getKey(), jsonElement.getAsString()); |
| 127 | + } |
| 128 | + return values; |
| 129 | + } |
| 130 | + |
| 131 | + private static <T extends NoticeContent> void withPart( |
| 132 | + Notice.Builder builder, |
| 133 | + NoticeDeserializeResult<T> noticeResult |
| 134 | + ) { |
| 135 | + builder.withPart(noticeResult.noticeKey(), noticeResult.content()); |
| 136 | + } |
| 137 | + |
| 138 | +} |
0 commit comments