• nikolaymetchev

    nikolaymetchev

    3 weeks ago
    Hi, I feel like it should be possible to serialize a subclass of
    com.google.protobuf.GeneratedMessageV3
    automatically. Has somebody written a Contextual serializer that does that?
  • Eugen Martynov

    Eugen Martynov

    3 weeks ago
    I’m reading official documentation on GitHub, and it looks like I need to add
    SerialName
    to all implementations to serialise the sealed interface. However, when I try to test it, I see that serialiset is not found for the discriminator. Does it mean I have to define all subclasses when defining the serializable module?
    Eugen Martynov
    Dominaezzz
    +1
    14 replies
    Copy to Clipboard
  • RayeW47

    RayeW47

    3 weeks ago
    Hello, I'm just starting out on learning how to use KotlinX (and serdes in general) and I am running in to an issue I can't seem to understand how to solve. I have a
    @Serializable class A
    and I have a helper class for companions to automatically grab a file to deserialize something in to an
    A
    and put it in
    A(.Companion).INSTANCE
    . However, using
    .INSTANCE
    gets a bit annoying to use and looks kind of gross, so I am trying to convert
    A
    in to a
    @Serializable sealed interface A
    , so that
    A.Companion
    can be an
    A
    and I can simply reference
    A
    rather than
    A(.Companion).INSTANCE
    . I can't seem to understand enough to get this working with a
    sealed interface
    , I keep running in to polymorphism errors or NPEs if I try to delegate similar to how I do in
    Loader
    , so I am here for help. Here's a minimum example.
    abstract class Loader<T>(val type: StringFormat, val filePath: String, val serializer: () -> KSerializer<T>, ) {
        val INSTANCE by lazy {
            type.decodeFromString(serializer(), File(filePath).readText())
        }
    }
    current
    @Serializable
    class A(val someProperty: Int) {
        companion object: Loader<A>(
            type = Json.Default, filePath = "./config/A-config.json", serializer = { A.serializer() }
        )
    }
    
    fun main() {
        A.INSTANCE.someProperty
    }
    goal
    @Serializable
    sealed interface A {
        val someProperty: Int
        companion object: Loader<A>(
            type = Json.Default, filePath = "./config/A-config.json", serializer = { A.serializer() }
        )
    }
    
    fun main() {
        A.someProperty
    }
  • xxfast

    xxfast

    2 weeks ago
    Where can i find the documentation/examples for Okio’s BufferedSource and BufferedSink with Kotlinx serialization ?
  • s

    spand

    2 weeks ago
    I cant get the example of external serializers to compile. It fails with :
    Type mismatch: inferred type is ProjectSerializer but S
    erializationStrategy<TypeVariable(T)> was expected
    . What could I be doing wrong ?
    import kotlinx.serialization.*
    import kotlinx.serialization.json.*
    import kotlinx.serialization.encoding.*
    import kotlinx.serialization.descriptors.*
    
    // NOT @Serializable
    class Project(val name: String, val language: String)
    
    @Serializer(forClass = Project::class)
    object ProjectSerializer
    
    fun main() {
        val data = Project("kotlinx.serialization", "Kotlin")
        println(Json.encodeToString(ProjectSerializer, data))
    }
  • xxfast

    xxfast

    2 weeks ago
    Hi all, trying to use
    kotlinx-serialization-json-okio
    like this in
    commonMain
    class ValueStore<T: Any>(
      val path: Path,
      val serializer: Json = Json,
      ..
    ) {
      suspend inline fun <reified V: T> set(value: V?) = lock.withLock {
        stateFlow.emit(value)
        serializer.encodeToBufferedSink(value, FILE_SYSTEM.sink(path).buffer())
      }
    }
    And have a
    commonTest
    written up like
    private val store: ValueStore<Pet> = ValueStore(path = "test.json".toPath())
    
    @Test
    fun testWriteValue() = runTest {
      val mylo = Pet(name = "Mylo", age = 1, type = Cat)
      store.set(mylo)
      val actual: Pet? = store.get<Pet>()
      val expect: Pet = mylo
      assertSame(expect, actual)
    }
    This test is failing 🤔 Although, I can see the
    test.json
    file being created, but it is empty. Am I using this correctly? Full source here
    xxfast
    j
    6 replies
    Copy to Clipboard
  • a

    Ayfri

    2 weeks ago
    Hi, is there a way to serialize an enum as the ordinal without adding SerialName for each key ?
    a
    e
    3 replies
    Copy to Clipboard
  • e

    ephemient

    2 weeks ago
    Is it just me or is Json.decodeFromBufferedSource much slower than Json.decodeFromStream or Json.decodeFromString?
    e
    j
    10 replies
    Copy to Clipboard