|
| 1 | +Help the user serialize Storm entities to JSON for REST APIs using Kotlin. |
| 2 | + |
| 3 | +Fetch https://orm.st/llms-full.txt for complete reference. |
| 4 | + |
| 5 | +This is about serializing entities for API responses or caching (Jackson, kotlinx.serialization), not about JSON database columns (use /storm-json-kotlin for that). |
| 6 | + |
| 7 | +Ask: which serialization library (Jackson or kotlinx.serialization), whether entities have `Ref<T>` fields, and whether they use Spring Boot or Redis caching. |
| 8 | + |
| 9 | +## When You Need the Storm Module |
| 10 | + |
| 11 | +Entities without `Ref` fields serialize with plain Jackson or kotlinx.serialization. No Storm module needed. |
| 12 | + |
| 13 | +Entities with `Ref<T>` fields require the Storm serialization module, because `Ref` can be unloaded (only the FK ID) or loaded (full entity/projection). Standard libraries cannot handle this distinction without help. |
| 14 | + |
| 15 | +## Jackson Setup |
| 16 | + |
| 17 | +Register `StormModule` on the `ObjectMapper`: |
| 18 | +```kotlin |
| 19 | +val mapper = ObjectMapper() |
| 20 | +mapper.registerModule(StormModule()) |
| 21 | +``` |
| 22 | + |
| 23 | +Spring Boot auto-detects Module beans: |
| 24 | +```kotlin |
| 25 | +@Configuration |
| 26 | +class JacksonConfig { |
| 27 | + @Bean |
| 28 | + fun stormModule(): StormModule = StormModule() |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +`StormModule` is in `st.orm.jackson`, available in both `storm-jackson2` and `storm-jackson3`. |
| 33 | + |
| 34 | +## Kotlinx Serialization Setup |
| 35 | + |
| 36 | +```kotlin |
| 37 | +val json = Json { |
| 38 | + serializersModule = StormSerializersModule() |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +Or use the pre-built convenience instance: |
| 43 | +```kotlin |
| 44 | +val json = Json { |
| 45 | + serializersModule = StormSerializers |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +**Critical**: Every `Ref` field in a `@Serializable` class must be annotated with `@Contextual`: |
| 50 | +```kotlin |
| 51 | +@Serializable |
| 52 | +data class Pet( |
| 53 | + @PK val id: Int = 0, |
| 54 | + val name: String, |
| 55 | + @FK @Contextual val owner: Ref<Owner>? |
| 56 | +) : Entity<Int> |
| 57 | +``` |
| 58 | + |
| 59 | +For collections of refs, annotate both the field and the type argument: |
| 60 | +```kotlin |
| 61 | +@Serializable |
| 62 | +data class TeamMembers( |
| 63 | + @Contextual val members: List<@Contextual Ref<User>> |
| 64 | +) |
| 65 | +``` |
| 66 | + |
| 67 | +Without `@Contextual`, the kotlinx compiler plugin tries to serialize `Ref` directly and fails at runtime with "RefImpl is not found in the polymorphic scope". |
| 68 | + |
| 69 | +## Kotlinx Serialization Cascade Rule |
| 70 | + |
| 71 | +When an entity is annotated with `@Serializable`, all entities reachable from it must also be `@Serializable`. This includes entities referenced via `@FK` fields (both direct entity fields and `Ref<T>` fields). For `Ref<T>`, the target type `T` must be `@Serializable`; `StormSerializers` handles the `Ref` wrapper itself. |
| 72 | + |
| 73 | +```kotlin |
| 74 | +@Serializable |
| 75 | +data class Order( |
| 76 | + @PK val id: Int = 0, |
| 77 | + @FK val customer: Customer, // Customer must be @Serializable |
| 78 | + @FK @Contextual val product: Ref<Product>?, // Product must be @Serializable |
| 79 | +) : Entity<Int> |
| 80 | +``` |
| 81 | + |
| 82 | +### Java time types |
| 83 | + |
| 84 | +Kotlinx.serialization does not support `java.time` types out of the box. If your entities contain `Instant`, `LocalDate`, `LocalTime`, or similar types, you need custom serializers: |
| 85 | + |
| 86 | +```kotlin |
| 87 | +@Serializable |
| 88 | +data class Event( |
| 89 | + @PK val id: Int = 0, |
| 90 | + @Serializable(with = InstantAsStringSerializer::class) val timestamp: Instant, |
| 91 | + @Serializable(with = LocalDateAsStringSerializer::class) val eventDate: LocalDate, |
| 92 | +) : Entity<Int> |
| 93 | +``` |
| 94 | + |
| 95 | +This does not apply to Jackson, which supports `java.time` natively (with the `jackson-datatype-jsr310` module, included by Spring Boot). |
| 96 | + |
| 97 | +## Caching (Redis, etc.) |
| 98 | + |
| 99 | +Entities cached in external stores like Redis need full serialization support. When using kotlinx.serialization with Redis (e.g., `KotlinxSerializationRedisSerializer`), configure the serializer with `StormSerializers` to handle `Ref<T>`: |
| 100 | + |
| 101 | +```kotlin |
| 102 | +val serializer = KotlinxSerializationRedisSerializer( |
| 103 | + Json { serializersModule = StormSerializers } |
| 104 | +) |
| 105 | +``` |
| 106 | + |
| 107 | +The same cascade rule applies: all entities reachable from a cached entity must be `@Serializable`, and all `Ref<T>` fields must have `@Contextual`. |
| 108 | + |
| 109 | +## Ref Serialization Format |
| 110 | + |
| 111 | +| Ref state | JSON output | Example | |
| 112 | +|-----------|-------------|---------| |
| 113 | +| Unloaded | Raw primary key | `1` | |
| 114 | +| Loaded entity | `{"@entity": {...}}` | `{"@entity": {"id": 1, "name": "Betty"}}` | |
| 115 | +| Loaded projection | `{"@id": ..., "@projection": {...}}` | `{"@id": 1, "@projection": {"id": 1, "name": "Betty"}}` | |
| 116 | +| Null | `null` | `null` | |
| 117 | + |
| 118 | +The format is fully round-trippable. Jackson and kotlinx.serialization produce identical JSON. |
| 119 | + |
| 120 | +## Rules |
| 121 | + |
| 122 | +- Refs deserialized from JSON are **detached**: they carry the ID but have no database connection. Calling `fetch()` on a deserialized ref throws `PersistenceException`. Use the deserialized ID to query the database directly. |
| 123 | +- Entities without `Ref` fields need no Storm module registration. |
| 124 | +- Both Jackson modules (`storm-jackson2`, `storm-jackson3`) provide the same `StormModule` API. |
| 125 | +- **Kotlinx cascade**: if an entity is `@Serializable`, every entity it references (directly or via `Ref<T>`) must also be `@Serializable`. |
| 126 | +- **`@Contextual` on `Ref<T>`**: without it, the kotlinx compiler plugin tries to serialize `Ref` directly and fails at runtime with "RefImpl is not found in the polymorphic scope". |
0 commit comments