13

What’s New in kotlinx.serialization 1.3

 3 years ago
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.
neoserver,ios ssh client

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()
Java IO stream-based JSON serialization.kt

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 the Json configuration.
Property-level control over default value encoding.kt

Excluding null values from serialization

  • explicitNulls: Another way to reduce the size of the generated JSON Strings is by omitting null values.
  • It defines whether null property valuesshould be included in the Serialized JSON String.
  • It’s true by default, so all nulls are stored as the values of their corresponding properties.
Excluding null values from serialization.kt
  • Missing field exception 🥵

To deserialize objects from JSON with omitted nulls, we need to make sure that Json instance with explicitNulls == false is used.

It sets all omitted nullable properties to null unless they have default values.

  • Try to decode a JSON string with omitted nulls with explicitNulls == true (default) it will throw a MissingFieldException
MissingFieldException.kt
1*yn2oWo0xO-Kg4qdXU1a4sQ.png?q=20
kotlin-kotlinx-serialization-1-3-e41313dcb4d2
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 the object being serialized.

In 1.3.0, By using @JsonClassDiscriminator’s discriminator property we can set a custom discriminator name for each class hierarchy.

Custom polymorphic class discriminators.kt

😊😊 👏👏👏👏 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

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK