{"id":893,"date":"2022-02-03T19:26:16","date_gmt":"2022-02-03T11:26:16","guid":{"rendered":"https:\/\/woohuiren.me\/blog\/?p=893"},"modified":"2022-02-03T19:26:21","modified_gmt":"2022-02-03T11:26:21","slug":"custom-kotlin-serializer-for-bigdecimal","status":"publish","type":"post","link":"https:\/\/woohuiren.me\/blog\/custom-kotlin-serializer-for-bigdecimal\/","title":{"rendered":"Custom Kotlin Serializer for BigDecimal"},"content":{"rendered":"\n<p>Since BigDecimal is part of Java Math and not Kotlin, a custom serializer is needed if you&#8217;re <strong>not<\/strong> using Jackson. After some thought, I chose to serialize it into a string using KSerializer. This means that you will need to have <strong>kolinx.serialization<\/strong> installed. The following is the Kotlin code for the serializer.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import kotlinx.serialization.KSerializer<br>import kotlinx.serialization.descriptors.*<br>import kotlinx.serialization.encoding.Decoder<br>import kotlinx.serialization.encoding.Encoder<br>import java.math.BigDecimal<br><br>object BigDecimalSerializer: KSerializer&lt;BigDecimal&gt; {<br>    override fun deserialize(decoder: Decoder): BigDecimal {<br>        return decoder.decodeString().<em>toBigDecimal<\/em>()<br>    }<br><br>    override fun serialize(encoder: Encoder, value: BigDecimal) {<br>        encoder.encodeString(value.toPlainString())<br>    }<br><br>    override val descriptor: SerialDescriptor<br>        get() = <em>PrimitiveSerialDescriptor<\/em>(\"BigDecimal\", PrimitiveKind.STRING)<br>}<\/pre>\n\n\n\n<p>It can be used in data classes like these:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">@Serializable\ndata class Product(\n    val id: Long,\n    val name: String,\n    val description: String,\n    @Serializable(with = BigDecimalSerializer::class)\n    val cost: BigDecimal,\n)<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since BigDecimal is part of Java Math and not Kotlin, a custom serializer is needed if you&#8217;re not using Jackson. After some thought, I chose to serialize it into a string using KSerializer. This means that you will need to have kolinx.serialization installed. The following is the Kotlin code for the serializer. import kotlinx.serialization.KSerializerimport kotlinx.serialization.descriptors.*import &hellip; <a href=\"https:\/\/woohuiren.me\/blog\/custom-kotlin-serializer-for-bigdecimal\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Custom Kotlin Serializer for BigDecimal&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[5],"tags":[208,215],"class_list":["post-893","post","type-post","status-publish","format-standard","hentry","category-programming","tag-kotlin","tag-kotlinx"],"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/woohuiren.me\/blog\/wp-json\/wp\/v2\/posts\/893"}],"collection":[{"href":"https:\/\/woohuiren.me\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/woohuiren.me\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/woohuiren.me\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/woohuiren.me\/blog\/wp-json\/wp\/v2\/comments?post=893"}],"version-history":[{"count":1,"href":"https:\/\/woohuiren.me\/blog\/wp-json\/wp\/v2\/posts\/893\/revisions"}],"predecessor-version":[{"id":894,"href":"https:\/\/woohuiren.me\/blog\/wp-json\/wp\/v2\/posts\/893\/revisions\/894"}],"wp:attachment":[{"href":"https:\/\/woohuiren.me\/blog\/wp-json\/wp\/v2\/media?parent=893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/woohuiren.me\/blog\/wp-json\/wp\/v2\/categories?post=893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/woohuiren.me\/blog\/wp-json\/wp\/v2\/tags?post=893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}