

What’s New in kotlinx.serialization 1.3
source link: https://proandroiddev.com/kotlin-kotlinx-serialization-1-3-e41313dcb4d2
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Responses
Also publish to my profile
There are currently no responses for this story.
Be the first to respond.
In this article, we will learn about the major new features that kotlinx.serialization 1.3
brings for developers to manage JSON parsing more efficiently.
Java IO stream-based JSON serialization
We can now read and write JSON directly to network streams or files.
IO stream serialization is currently available only on the JVM platform and for the JSON format.
API includes two main methods:
- Json.decodeFromStream()
- Json.encodeToStream()
Property-level control over default value encoding
We can force the library to encode the default values
by setting the encodeDefaults
property of a Json
instance to true
:
val format = Json { encodeDefaults = true } // false by default
- In
1.3.0
we can control it at the property level by using the experimental@EncodeDefault
annotation. - It has a higher priority level than the
encodeDefaults
property.
It has two possible values:
The default value is ALWAYS
- ALWAYS: encodes a property’s default value.
- NEVER: doesn’t
encode the default value
regardless of theJson
configuration.
Excluding null values from serialization
explicitNulls
: Another way to reduce the size of the generatedJSON Strings
is by omittingnull
values.- It defines whether
null property values
should be included in theSerialized JSON String
. - It’s
true
by default, so allnulls
are stored as the values of their corresponding properties.
- Missing field exception 🥵
To deserialize objects from JSON with omitted nulls, we need to make sure that
Json instance
withexplicitNulls == false
is used.It sets all omitted nullable properties to
null
unless they havedefault values
.
- Try to decode a
JSON string
with omitted nulls withexplicitNulls == true (default)
it willthrow
a MissingFieldException

Custom polymorphic class discriminators
- In hierarchy serialization, a useful attribute comes into play — class discriminator.
- It stores the exact class of the object that was encoded.
- By default, it has the name
type
and contains a fully qualified class name of theobject
beingserialized
.
Custom polymorphic class discriminators.ktIn 1.3.0, By using @JsonClassDiscriminator’s
discriminator
property we can set a custom discriminator name for each class hierarchy.
😊😊 👏👏👏👏 HAPPY CODING 👏👏👏👏 😊😊
Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more
Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore
If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Start a blog
Recommend
-
272
Kotlin multiplatform / multi-format reflectionless serialization Kotlin serialization consists of a compiler plugin, that generates visitor code for serializable classes, runtime library with core serialization API and support libraries w...
-
87
-
69
An Extensive Kotlinx Serializer Library For Serialization
-
48
Recently, I have been working on a project where I need to store sealed classes data inside the Room Persistence using the Kotlinx Serialization library. We all know that Room only supports primitives types for storage. I struggle for almost two...
-
11
Libraries kotlinx.serialization 1.2 Released: High-Speed JSON Handling, Value Class Support, Overhauled Docs, and more ...
-
7
#kotlin #serialization #jsonkotlinx.serializ...
-
9
Libraries kotlinx.serialization 1.2 のリリース: JSON 高速処理、値クラスのサポート、ドキュメントの見直しなど
-
11
Libraries kotlinx.serialization 1.3 Released: Experimental IO Stream-Based JSON Serialization, Fine-Grained Defaults Control, and More...
-
7
Serializers This is the third chapter of the Kotlin Serialization Guide. In this chapter we'll take a look at serializer...
-
10
Kotlin Sealed Interfaces with KotlinX Serialization JSON – Handstand Sam I heavily use sealed interfaces to model...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK