|
1 | 1 | package dev.flexmodel.codegen; |
2 | 2 |
|
3 | | -import java.io.*; |
| 3 | +import dev.flexmodel.JsonUtils; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.ObjectInputStream; |
| 7 | +import java.io.ObjectOutputStream; |
| 8 | +import java.nio.charset.StandardCharsets; |
4 | 9 | import java.util.Base64; |
5 | 10 |
|
6 | 11 | /** |
| 12 | + * Object serialization utilities. |
| 13 | + * <p> |
| 14 | + * Uses Jackson JSON serialization (wrapped in Base64) instead of Java native |
| 15 | + * serialization ({@link ObjectOutputStream}/{@link ObjectInputStream}) to avoid |
| 16 | + * {@code serialVersionUID} incompatibility between JDK versions. JDK classes |
| 17 | + * like {@link java.util.HashMap} and {@link java.util.ArrayList} change their |
| 18 | + * {@code serialVersionUID} across releases, which breaks deserialization when |
| 19 | + * the codegen JDK differs from the runtime JDK (e.g., in GraalVM native-image). |
| 20 | + * </p> |
| 21 | + * |
7 | 22 | * @author cjbi |
8 | 23 | */ |
9 | 24 | public class ObjectUtils { |
10 | 25 |
|
| 26 | + private ObjectUtils() { |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Serialize an object to a Base64-encoded JSON string. |
| 31 | + * <p> |
| 32 | + * The serialized format embeds the class name as the first line, followed by |
| 33 | + * the JSON representation. This allows deserialization to reconstruct the |
| 34 | + * exact object type without requiring an explicit type parameter. |
| 35 | + * </p> |
| 36 | + */ |
11 | 37 | public static String serialize(Object obj) throws IOException { |
12 | | - ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
13 | | - ObjectOutputStream oos = new ObjectOutputStream(baos); |
14 | | - oos.writeObject(obj); |
15 | | - oos.flush(); |
16 | | - return Base64.getEncoder().encodeToString(baos.toByteArray()); |
| 38 | + String json = JsonUtils.toJsonString(obj); |
| 39 | + String className = obj.getClass().getName(); |
| 40 | + String combined = className + "\n" + json; |
| 41 | + return Base64.getEncoder().encodeToString(combined.getBytes(StandardCharsets.UTF_8)); |
17 | 42 | } |
18 | 43 |
|
| 44 | + /** |
| 45 | + * Deserialize a Base64-encoded JSON string back to an object. |
| 46 | + * <p> |
| 47 | + * The first line of the decoded data is the fully qualified class name, |
| 48 | + * which is used to determine the target type for Jackson deserialization. |
| 49 | + * </p> |
| 50 | + * |
| 51 | + * @param str Base64-encoded serialized data |
| 52 | + * @return the deserialized object |
| 53 | + * @throws IOException if I/O error occurs |
| 54 | + * @throws ClassNotFoundException if the target class cannot be found |
| 55 | + */ |
19 | 56 | public static Object deserialize(String str) throws IOException, ClassNotFoundException { |
20 | 57 | byte[] data = Base64.getDecoder().decode(str); |
21 | | - ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data)); |
22 | | - return ois.readObject(); |
| 58 | + String combined = new String(data, StandardCharsets.UTF_8); |
| 59 | + int newlineIdx = combined.indexOf('\n'); |
| 60 | + if (newlineIdx < 0) { |
| 61 | + throw new IOException("Invalid serialized format: missing class name header"); |
| 62 | + } |
| 63 | + String className = combined.substring(0, newlineIdx); |
| 64 | + String json = combined.substring(newlineIdx + 1); |
| 65 | + @SuppressWarnings("unchecked") |
| 66 | + Class<?> clazz = Class.forName(className); |
| 67 | + return JsonUtils.parseToObject(json, clazz); |
23 | 68 | } |
24 | 69 |
|
25 | 70 | } |
0 commit comments