Fixes serialization issues when the code falls back to Kotlin Serialization
This commit is contained in:
@@ -21,11 +21,14 @@
|
||||
package com.vitorpamplona.quartz.nip01Core.core
|
||||
|
||||
import androidx.compose.runtime.Immutable
|
||||
import com.vitorpamplona.quartz.nip01Core.kotlinSerialization.EventKSerializer
|
||||
import com.vitorpamplona.quartz.nip01Core.signers.eventTemplate
|
||||
import com.vitorpamplona.quartz.utils.Log
|
||||
import com.vitorpamplona.quartz.utils.TimeUtils
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Immutable
|
||||
@Serializable(with = EventKSerializer::class)
|
||||
open class Event(
|
||||
val id: HexKey,
|
||||
val pubKey: HexKey,
|
||||
|
||||
+2
@@ -41,6 +41,8 @@ expect object OptimizedJsonMapper {
|
||||
|
||||
fun fromJsonToEventTemplate(json: String): EventTemplate<Event>
|
||||
|
||||
fun fromJsonToEventList(json: String): List<Event>
|
||||
|
||||
fun fromJsonToRumor(json: String): Rumor
|
||||
|
||||
fun toJson(tags: Array<Array<String>>): String
|
||||
|
||||
+2
@@ -64,6 +64,8 @@ class KotlinSerializationMapper {
|
||||
|
||||
fun fromJsonToEventTemplate(jsonStr: String): EventTemplate<Event> = json.decodeFromString(EventTemplateKSerializer, jsonStr)
|
||||
|
||||
fun fromJsonToEventList(jsonStr: String): List<Event> = json.decodeFromString(jsonStr)
|
||||
|
||||
fun toJson(event: Event): String = json.encodeToString(EventKSerializer, event)
|
||||
|
||||
fun toJson(tags: TagArray): String = json.encodeToString(TagArrayKSerializer, tags)
|
||||
|
||||
+3
@@ -21,7 +21,10 @@
|
||||
package com.vitorpamplona.quartz.nip01Core.relay.commands.toClient
|
||||
|
||||
import com.vitorpamplona.quartz.nip01Core.core.OptimizedSerializable
|
||||
import com.vitorpamplona.quartz.nip01Core.kotlinSerialization.MessageKSerializer
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable(with = MessageKSerializer::class)
|
||||
interface Message : OptimizedSerializable {
|
||||
fun label(): String
|
||||
}
|
||||
|
||||
+3
@@ -21,7 +21,10 @@
|
||||
package com.vitorpamplona.quartz.nip01Core.relay.commands.toRelay
|
||||
|
||||
import com.vitorpamplona.quartz.nip01Core.core.OptimizedSerializable
|
||||
import com.vitorpamplona.quartz.nip01Core.kotlinSerialization.CommandKSerializer
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable(with = CommandKSerializer::class)
|
||||
interface Command : OptimizedSerializable {
|
||||
fun label(): String
|
||||
|
||||
|
||||
+5
@@ -20,6 +20,7 @@
|
||||
*/
|
||||
package com.vitorpamplona.quartz.nip01Core.signers
|
||||
|
||||
import androidx.compose.runtime.Immutable
|
||||
import com.vitorpamplona.quartz.nip01Core.core.Event
|
||||
import com.vitorpamplona.quartz.nip01Core.core.OptimizedJsonMapper
|
||||
import com.vitorpamplona.quartz.nip01Core.core.OptimizedSerializable
|
||||
@@ -27,8 +28,12 @@ import com.vitorpamplona.quartz.nip01Core.core.TagArray
|
||||
import com.vitorpamplona.quartz.nip01Core.core.TagArrayBuilder
|
||||
import com.vitorpamplona.quartz.nip01Core.core.builder
|
||||
import com.vitorpamplona.quartz.nip01Core.core.tagArray
|
||||
import com.vitorpamplona.quartz.nip01Core.kotlinSerialization.EventTemplateKSerializer
|
||||
import com.vitorpamplona.quartz.utils.TimeUtils
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Immutable
|
||||
@Serializable(with = EventTemplateKSerializer::class)
|
||||
class EventTemplate<T : Event>(
|
||||
val createdAt: Long,
|
||||
val kind: Int,
|
||||
|
||||
@@ -20,14 +20,19 @@
|
||||
*/
|
||||
package com.vitorpamplona.quartz.nip59Giftwrap.rumors
|
||||
|
||||
import androidx.compose.runtime.Immutable
|
||||
import com.vitorpamplona.quartz.nip01Core.core.Event
|
||||
import com.vitorpamplona.quartz.nip01Core.core.HexKey
|
||||
import com.vitorpamplona.quartz.nip01Core.core.OptimizedJsonMapper
|
||||
import com.vitorpamplona.quartz.nip01Core.core.OptimizedSerializable
|
||||
import com.vitorpamplona.quartz.nip01Core.crypto.EventHasher
|
||||
import com.vitorpamplona.quartz.nip59Giftwrap.rumors.kotlinSerialization.RumorKSerializer
|
||||
import com.vitorpamplona.quartz.nip59Giftwrap.seals.SealedRumorEvent
|
||||
import com.vitorpamplona.quartz.utils.EventFactory
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Immutable
|
||||
@Serializable(with = RumorKSerializer::class)
|
||||
class Rumor(
|
||||
val id: HexKey?,
|
||||
val pubKey: HexKey?,
|
||||
|
||||
+7
@@ -65,6 +65,13 @@ actual object OptimizedJsonMapper {
|
||||
throw IllegalArgumentException(e.message, e)
|
||||
}
|
||||
|
||||
actual fun fromJsonToEventList(json: String): List<Event> =
|
||||
try {
|
||||
KotlinSerializationMapper.fromJsonToEventList(json)
|
||||
} catch (e: SerializationException) {
|
||||
throw IllegalArgumentException(e.message, e)
|
||||
}
|
||||
|
||||
actual fun fromJsonToRumor(json: String): Rumor =
|
||||
try {
|
||||
KotlinSerializationMapper.fromJsonToRumor(json)
|
||||
|
||||
+7
@@ -72,6 +72,13 @@ actual object OptimizedJsonMapper {
|
||||
throw IllegalArgumentException(e.message, e)
|
||||
}
|
||||
|
||||
actual fun fromJsonToEventList(json: String): List<Event> =
|
||||
try {
|
||||
JacksonMapper.fromJsonToEventList(json)
|
||||
} catch (e: com.fasterxml.jackson.core.JsonParseException) {
|
||||
throw IllegalArgumentException(e.message, e)
|
||||
}
|
||||
|
||||
actual fun toJson(tags: Array<Array<String>>): String = JacksonMapper.toJson(tags)
|
||||
|
||||
actual inline fun <reified T : OptimizedSerializable> fromJsonTo(json: String): T =
|
||||
|
||||
+3
@@ -117,6 +117,7 @@ class JacksonMapper {
|
||||
val tagArrayTypeInstance: JavaType = mapper.typeFactory.constructType(jacksonTypeRef<TagArray>())
|
||||
val rumorTypeInstance: JavaType = mapper.typeFactory.constructType(jacksonTypeRef<Rumor>())
|
||||
val eventTemplateTypeInstance: JavaType = mapper.typeFactory.constructType(jacksonTypeRef<EventTemplate<Event>>())
|
||||
val eventListTypeInstance: JavaType = mapper.typeFactory.constructType(jacksonTypeRef<List<Event>>())
|
||||
val messageTypeInstance: JavaType = mapper.typeFactory.constructType(jacksonTypeRef<Message>())
|
||||
val commandTypeInstance: JavaType = mapper.typeFactory.constructType(jacksonTypeRef<Command>())
|
||||
|
||||
@@ -132,6 +133,8 @@ class JacksonMapper {
|
||||
|
||||
fun fromJsonToEventTemplate(json: String): EventTemplate<Event> = mapper.readValue(json, eventTemplateTypeInstance)
|
||||
|
||||
fun fromJsonToEventList(json: String): List<Event> = mapper.readValue(json, eventListTypeInstance)
|
||||
|
||||
inline fun <reified T : OptimizedSerializable> fromJsonTo(json: String): T = mapper.readValue<T>(json)
|
||||
|
||||
inline fun <reified T : OptimizedSerializable> fromJsonTo(json: InputStream): T = mapper.readValue<T>(json)
|
||||
|
||||
Reference in New Issue
Block a user