taler-android

Android apps for GNU Taler (wallet, PoS, cashier)
Log | Files | Refs | README | LICENSE

ObservabilityEvent.kt (2268B)


      1 /*
      2  * This file is part of GNU Taler
      3  * (C) 2024 Taler Systems S.A.
      4  *
      5  * GNU Taler is free software; you can redistribute it and/or modify it under the
      6  * terms of the GNU General Public License as published by the Free Software
      7  * Foundation; either version 3, or (at your option) any later version.
      8  *
      9  * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
     10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11  * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License along with
     14  * GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15  */
     16 
     17 package net.taler.wallet.events
     18 
     19 import kotlinx.serialization.KSerializer
     20 import kotlinx.serialization.Serializable
     21 import kotlinx.serialization.descriptors.SerialDescriptor
     22 import kotlinx.serialization.encoding.Decoder
     23 import kotlinx.serialization.encoding.Encoder
     24 import kotlinx.serialization.json.JsonDecoder
     25 import kotlinx.serialization.json.JsonElement
     26 import kotlinx.serialization.json.JsonObject
     27 import kotlinx.serialization.json.jsonObject
     28 import kotlinx.serialization.json.jsonPrimitive
     29 import java.time.LocalDateTime
     30 
     31 
     32 @Serializable(with = ObservabilityEventSerializer::class)
     33 class ObservabilityEvent(
     34     val body: JsonObject,
     35     val timestamp: LocalDateTime,
     36     val type: String,
     37 )
     38 
     39 class ObservabilityEventSerializer: KSerializer<ObservabilityEvent> {
     40     private val jsonElementSerializer = JsonElement.serializer()
     41 
     42     override val descriptor: SerialDescriptor
     43         get() = jsonElementSerializer.descriptor
     44 
     45     override fun deserialize(decoder: Decoder): ObservabilityEvent {
     46         require(decoder is JsonDecoder)
     47         val jsonObject = decoder
     48             .decodeJsonElement()
     49             .jsonObject
     50 
     51         val type = jsonObject["type"]
     52             ?.jsonPrimitive
     53             ?.content
     54             ?: "unknown"
     55 
     56         return ObservabilityEvent(
     57             body = jsonObject,
     58             timestamp = LocalDateTime.now(),
     59             type = type,
     60         )
     61     }
     62 
     63     override fun serialize(encoder: Encoder, value: ObservabilityEvent) {
     64         encoder.encodeSerializableValue(JsonObject.serializer(), value.body)
     65     }
     66 }