Kotlinx.serialization: Convert Int to Boolean with custom serializer

Created on 6 Jul 2018  路  7Comments  路  Source: Kotlin/kotlinx.serialization

Hi, I've already spent a lot of time trying to write custom serializer, to replace Int (TINYINT from mysql) to Boolean.

Using Gson I do it without problems, something like this (java):
` public class BooleanSerializer implements JsonSerializer, JsonDeserializer {

    @Override
    public JsonElement serialize(Boolean arg0, Type arg1, JsonSerializationContext arg2) {
        return new JsonPrimitive(arg0 ? 1 : 0);
    }

    @Override
    public Boolean deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException {
        return arg0.getAsInt() == 1;
    }
}

GsonBuilder().registerTypeAdapter(Boolean.class, serializer)
`

Is it possible to achieve the same result here and how?
Thank you.

question

Most helpful comment

As I understand it, this is not possible at the moment. I use for this:

@Serializable
data class BooleanInt(val value: Boolean) {

    @Serializer(forClass = BooleanInt::class)
    companion object : KSerializer<BooleanInt> {
        override fun save(output: KOutput, obj: BooleanInt) =
            output.writeIntValue(if (obj.value) 1 else 0)

        override fun load(input: KInput): BooleanInt =
            BooleanInt(input.readNullable(IntSerializer) == 1)
    }
}

And:

@Serializable
data class Example(
    @SerialName("my_bool") val myBool: BooleanInt
)

All 7 comments

As I understand it, this is not possible at the moment. I use for this:

@Serializable
data class BooleanInt(val value: Boolean) {

    @Serializer(forClass = BooleanInt::class)
    companion object : KSerializer<BooleanInt> {
        override fun save(output: KOutput, obj: BooleanInt) =
            output.writeIntValue(if (obj.value) 1 else 0)

        override fun load(input: KInput): BooleanInt =
            BooleanInt(input.readNullable(IntSerializer) == 1)
    }
}

And:

@Serializable
data class Example(
    @SerialName("my_bool") val myBool: BooleanInt
)

Yes, I did the same, but it's not very convenient

Or you can use @Serializable(with=...) on boolean properties which you need to convert with custom serializer.

Currently, indeed, there is no option to override default serializers globally.

Given the way the serialization code is generated by the plugin overriding the only straightforward way is to have a delegating writer and reader where the boolean option is redirected.

@xdma
Even though globally one can't override Boolean serializer, it's possible to achieve locally.
For your custom serialization format, it's possible to write custom KInput/KOutput, which can hook into writeBooleanValue and readBooleanValue.

If you need such feature in JSON (do you?), we can provide different implementation/builder which allows to hookup into specific serialization points by target ::class or KSerialClassDesc

@qwwdfsad

Thanks, I already solved the problem, as @Skeptick advised, it's less convenient, but it works and I don't want to go back to that part of project now :)

You can now also use @file:UseSerializers to override default serializers on a per-file basis.

Was this page helpful?
0 / 5 - 0 ratings