|
| 1 | +package it.saabel.kotlinnotionclient.models.pages |
| 2 | + |
| 3 | +import kotlinx.serialization.DeserializationStrategy |
| 4 | +import kotlinx.serialization.KSerializer |
| 5 | +import kotlinx.serialization.SerializationException |
| 6 | +import kotlinx.serialization.descriptors.SerialDescriptor |
| 7 | +import kotlinx.serialization.descriptors.buildClassSerialDescriptor |
| 8 | +import kotlinx.serialization.encoding.Decoder |
| 9 | +import kotlinx.serialization.encoding.Encoder |
| 10 | +import kotlinx.serialization.json.JsonContentPolymorphicSerializer |
| 11 | +import kotlinx.serialization.json.JsonDecoder |
| 12 | +import kotlinx.serialization.json.JsonElement |
| 13 | +import kotlinx.serialization.json.jsonObject |
| 14 | +import kotlinx.serialization.json.jsonPrimitive |
| 15 | + |
| 16 | +/** |
| 17 | + * Custom serializer for [PageProperty] sealed class. |
| 18 | + * |
| 19 | + * This serializer handles the polymorphic serialization and deserialization of PageProperty objects |
| 20 | + * based on the "type" discriminator field in the JSON, maintaining compatibility with the |
| 21 | + * Notion API's response format. |
| 22 | + * |
| 23 | + * Unlike other serializers in the codebase, this one gracefully handles unknown property types |
| 24 | + * by deserializing them as [PageProperty.Unknown], ensuring forward compatibility as Notion |
| 25 | + * adds new property types (e.g., "button", "unique_id", "verification", etc.). |
| 26 | + * |
| 27 | + * ## Supported Property Types |
| 28 | + * - title, rich_text, number, checkbox, url, email, phone_number |
| 29 | + * - select, multi_select, status |
| 30 | + * - date, people, files |
| 31 | + * - relation, formula, rollup |
| 32 | + * - created_time, last_edited_time, created_by, last_edited_by |
| 33 | + * |
| 34 | + * ## Unknown Property Types |
| 35 | + * Any property type not in the list above will be deserialized as [PageProperty.Unknown], |
| 36 | + * with the raw JSON preserved in the `rawContent` field for inspection or manual handling. |
| 37 | + */ |
| 38 | +object PagePropertySerializer : KSerializer<PageProperty> { |
| 39 | + override val descriptor: SerialDescriptor = buildClassSerialDescriptor("PageProperty") |
| 40 | + |
| 41 | + override fun deserialize(decoder: Decoder): PageProperty { |
| 42 | + require(decoder is JsonDecoder) { "PagePropertySerializer can only deserialize JSON" } |
| 43 | + |
| 44 | + val element = decoder.decodeJsonElement() |
| 45 | + val jsonObject = element.jsonObject |
| 46 | + |
| 47 | + val type = |
| 48 | + jsonObject["type"]?.jsonPrimitive?.content |
| 49 | + ?: throw SerializationException("Missing 'type' field in PageProperty JSON") |
| 50 | + |
| 51 | + val id = |
| 52 | + jsonObject["id"]?.jsonPrimitive?.content |
| 53 | + ?: throw SerializationException("Missing 'id' field in PageProperty JSON") |
| 54 | + |
| 55 | + return when (type) { |
| 56 | + "title" -> decoder.json.decodeFromJsonElement(PageProperty.Title.serializer(), element) |
| 57 | + "rich_text" -> decoder.json.decodeFromJsonElement(PageProperty.RichTextProperty.serializer(), element) |
| 58 | + "number" -> decoder.json.decodeFromJsonElement(PageProperty.Number.serializer(), element) |
| 59 | + "checkbox" -> decoder.json.decodeFromJsonElement(PageProperty.Checkbox.serializer(), element) |
| 60 | + "url" -> decoder.json.decodeFromJsonElement(PageProperty.Url.serializer(), element) |
| 61 | + "email" -> decoder.json.decodeFromJsonElement(PageProperty.Email.serializer(), element) |
| 62 | + "phone_number" -> decoder.json.decodeFromJsonElement(PageProperty.PhoneNumber.serializer(), element) |
| 63 | + "select" -> decoder.json.decodeFromJsonElement(PageProperty.Select.serializer(), element) |
| 64 | + "multi_select" -> decoder.json.decodeFromJsonElement(PageProperty.MultiSelect.serializer(), element) |
| 65 | + "status" -> decoder.json.decodeFromJsonElement(PageProperty.Status.serializer(), element) |
| 66 | + "date" -> decoder.json.decodeFromJsonElement(PageProperty.Date.serializer(), element) |
| 67 | + "people" -> decoder.json.decodeFromJsonElement(PageProperty.People.serializer(), element) |
| 68 | + "files" -> decoder.json.decodeFromJsonElement(PageProperty.Files.serializer(), element) |
| 69 | + "relation" -> decoder.json.decodeFromJsonElement(PageProperty.Relation.serializer(), element) |
| 70 | + "formula" -> decoder.json.decodeFromJsonElement(PageProperty.Formula.serializer(), element) |
| 71 | + "rollup" -> decoder.json.decodeFromJsonElement(PageProperty.Rollup.serializer(), element) |
| 72 | + "created_time" -> decoder.json.decodeFromJsonElement(PageProperty.CreatedTime.serializer(), element) |
| 73 | + "last_edited_time" -> decoder.json.decodeFromJsonElement(PageProperty.LastEditedTime.serializer(), element) |
| 74 | + "created_by" -> decoder.json.decodeFromJsonElement(PageProperty.CreatedBy.serializer(), element) |
| 75 | + "last_edited_by" -> decoder.json.decodeFromJsonElement(PageProperty.LastEditedBy.serializer(), element) |
| 76 | + else -> { |
| 77 | + // Fallback to Unknown for unsupported property types |
| 78 | + // Manually construct Unknown with the raw JSON element |
| 79 | + PageProperty.Unknown( |
| 80 | + id = id, |
| 81 | + type = type, |
| 82 | + rawContent = element, |
| 83 | + ) |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + override fun serialize( |
| 89 | + encoder: Encoder, |
| 90 | + value: PageProperty, |
| 91 | + ) { |
| 92 | + // For serialization, delegate to the appropriate serializer |
| 93 | + when (value) { |
| 94 | + is PageProperty.Title -> encoder.encodeSerializableValue(PageProperty.Title.serializer(), value) |
| 95 | + is PageProperty.RichTextProperty -> |
| 96 | + encoder.encodeSerializableValue( |
| 97 | + PageProperty.RichTextProperty.serializer(), |
| 98 | + value, |
| 99 | + ) |
| 100 | + is PageProperty.Number -> encoder.encodeSerializableValue(PageProperty.Number.serializer(), value) |
| 101 | + is PageProperty.Checkbox -> encoder.encodeSerializableValue(PageProperty.Checkbox.serializer(), value) |
| 102 | + is PageProperty.Url -> encoder.encodeSerializableValue(PageProperty.Url.serializer(), value) |
| 103 | + is PageProperty.Email -> encoder.encodeSerializableValue(PageProperty.Email.serializer(), value) |
| 104 | + is PageProperty.PhoneNumber -> encoder.encodeSerializableValue(PageProperty.PhoneNumber.serializer(), value) |
| 105 | + is PageProperty.Select -> encoder.encodeSerializableValue(PageProperty.Select.serializer(), value) |
| 106 | + is PageProperty.MultiSelect -> encoder.encodeSerializableValue(PageProperty.MultiSelect.serializer(), value) |
| 107 | + is PageProperty.Status -> encoder.encodeSerializableValue(PageProperty.Status.serializer(), value) |
| 108 | + is PageProperty.Date -> encoder.encodeSerializableValue(PageProperty.Date.serializer(), value) |
| 109 | + is PageProperty.People -> encoder.encodeSerializableValue(PageProperty.People.serializer(), value) |
| 110 | + is PageProperty.Files -> encoder.encodeSerializableValue(PageProperty.Files.serializer(), value) |
| 111 | + is PageProperty.Relation -> encoder.encodeSerializableValue(PageProperty.Relation.serializer(), value) |
| 112 | + is PageProperty.Formula -> encoder.encodeSerializableValue(PageProperty.Formula.serializer(), value) |
| 113 | + is PageProperty.Rollup -> encoder.encodeSerializableValue(PageProperty.Rollup.serializer(), value) |
| 114 | + is PageProperty.CreatedTime -> encoder.encodeSerializableValue(PageProperty.CreatedTime.serializer(), value) |
| 115 | + is PageProperty.LastEditedTime -> |
| 116 | + encoder.encodeSerializableValue( |
| 117 | + PageProperty.LastEditedTime.serializer(), |
| 118 | + value, |
| 119 | + ) |
| 120 | + is PageProperty.CreatedBy -> encoder.encodeSerializableValue(PageProperty.CreatedBy.serializer(), value) |
| 121 | + is PageProperty.LastEditedBy -> |
| 122 | + encoder.encodeSerializableValue( |
| 123 | + PageProperty.LastEditedBy.serializer(), |
| 124 | + value, |
| 125 | + ) |
| 126 | + is PageProperty.Unknown -> encoder.encodeSerializableValue(PageProperty.Unknown.serializer(), value) |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments