|
| 1 | +package com.metaobjects.io.object.gson; |
| 2 | + |
| 3 | +import com.metaobjects.io.json.TemporalWireFormat; |
| 4 | +import com.google.gson.*; |
| 5 | + |
| 6 | +import java.lang.reflect.Type; |
| 7 | +import java.text.DateFormat; |
| 8 | +import java.text.ParseException; |
| 9 | +import java.util.Date; |
| 10 | + |
| 11 | +/** |
| 12 | + * Canonical {@code java.util.Date} wire form for every Gson path that does NOT go through |
| 13 | + * {@link MetaObjectSerializer} — i.e. any {@code Date} Gson reaches by plain reflection. |
| 14 | + * |
| 15 | + * <p><b>The gap this closes.</b> {@link MetaObjectGsonInitializer} registers |
| 16 | + * {@link MetaObjectSerializer}/{@link MetaObjectDeserializer} against each {@link |
| 17 | + * com.metaobjects.object.MetaObject}'s declared {@code @object} class. A value object bound to a |
| 18 | + * hand-written POJO through {@link com.metaobjects.registry.ObjectClassRegistry} is therefore |
| 19 | + * serialized by Gson's DEFAULT reflection instead, and its {@code Date} properties took Gson's |
| 20 | + * built-in adapter. That emitted a localized {@code DateFormat.DEFAULT} string — e.g. |
| 21 | + * {@code "Jun 3, 2026, 10:30:00 AM"} — which is: |
| 22 | + * <ul> |
| 23 | + * <li><b>locale-dependent</b> — the same instant serializes differently per JVM default locale;</li> |
| 24 | + * <li><b>timezone-dependent</b> — rendered in the JVM's local zone, not UTC, so the stored text |
| 25 | + * depends on where the process runs;</li> |
| 26 | + * <li><b>millisecond-lossy</b> — {@code .123} is discarded, so even a Java-only round-trip |
| 27 | + * does not return the original instant;</li> |
| 28 | + * <li><b>unreadable by the other ports</b> — TS/Python/C#/Kotlin all expect the ISO form |
| 29 | + * defined in {@code fixtures/persistence-conformance/normalization.md}.</li> |
| 30 | + * </ul> |
| 31 | + * The visible blast radius is an OMDB {@code @storage:jsonb} column holding a POJO-bound value |
| 32 | + * object with a temporal field (see {@code GenericSQLDriver#serializeJsonb}). This is the same |
| 33 | + * defect class as #275, on the one path #275 did not reach. |
| 34 | + * |
| 35 | + * <p><b>Write:</b> always {@link TemporalWireFormat#formatInstant} (the {@code ...Z} instant). |
| 36 | + * Field context is unavailable here, so the {@code field.date} and {@code @localTime} shapes |
| 37 | + * cannot be reproduced — a POJO-bound temporal property is written as a full instant. That is a |
| 38 | + * deliberate, documented narrowing: it is lossless and portable, where the previous behavior was |
| 39 | + * neither. A value object that needs the exact per-field shape should stay on the |
| 40 | + * metadata-driven path (no POJO binding), which consults its {@link |
| 41 | + * com.metaobjects.field.MetaField} and calls {@link TemporalWireFormat#format}. |
| 42 | + * |
| 43 | + * <p><b>Read:</b> {@link TemporalWireFormat#parse} first (all three canonical shapes), then a |
| 44 | + * fallback through Gson's own former default ({@link DateFormat#getDateTimeInstance()}) so rows |
| 45 | + * already written in the legacy localized format still load. Write canonical, read tolerant. |
| 46 | + * |
| 47 | + * <p>This adapter never sees a {@code Date} on the metadata-driven path: {@link |
| 48 | + * MetaObjectSerializer}'s {@code DATE} branch formats and calls {@code addProperty} itself rather |
| 49 | + * than delegating to {@code context.serialize}, so that output is unaffected. |
| 50 | + */ |
| 51 | +public final class TemporalGsonAdapter implements JsonSerializer<Date>, JsonDeserializer<Date> { |
| 52 | + |
| 53 | + @Override |
| 54 | + public JsonElement serialize(Date src, Type type, JsonSerializationContext context) { |
| 55 | + if (src == null) return JsonNull.INSTANCE; |
| 56 | + return new JsonPrimitive(TemporalWireFormat.formatInstant(src)); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public Date deserialize(JsonElement json, Type type, JsonDeserializationContext context) { |
| 61 | + if (json == null || json.isJsonNull()) return null; |
| 62 | + String s = json.getAsString(); |
| 63 | + try { |
| 64 | + return TemporalWireFormat.parse(s); |
| 65 | + } catch (IllegalArgumentException canonicalMiss) { |
| 66 | + // Legacy: a value written before this adapter existed, in Gson's default |
| 67 | + // localized form. Best-effort so old rows still read; millisecond precision |
| 68 | + // was already lost when it was written and cannot be recovered here. |
| 69 | + try { |
| 70 | + return DateFormat.getDateTimeInstance().parse(s); |
| 71 | + } catch (ParseException legacyMiss) { |
| 72 | + throw new JsonParseException( |
| 73 | + "Cannot parse temporal value [" + s + "] as either the canonical wire form " |
| 74 | + + "or the legacy localized form", canonicalMiss); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments