Since its creation in 2011, Kotlin has evolved continuously. It is a fast-growing global community of developers and contributors. Google also declared official support for Kotlin Android development, and many organizations are leveraging Kotlin’s rich ecosystem to build apps that have a lasting impact.
Recently, Kotlinx.serialization 1.2 is released with high-speed JSON handling and many improvements.
In this post, we will be talking about key highlights of this latest update of the Kotlinx.serialization library. Kotlin Serialization is a type-safe, cross-platform, and multi-format framework for data serialization. It converts Kotlin objects into formats like JSON or other serial representations like ProtoBuf and vice-versa.
This library acts as a compiler plugin that is combined with the Kotlin compiler distribution.
Kotlinx.serialization 1.2 comes with several improvements, here are the key points:
Before we understand each in detail, let’s see what serialization is and how does it work.
Simply, serialization is the process of translating a data structure or object into a format that can be stored in a database or file, or transferred over a network.
It is one of the important parts of application development. This is because it saves the state of an object to be able to recreate it when required. Deserialization, which is the exact opposite of serialization, is a process of reconverting the serialized byte stream to an object.
Serialization is important for data exchange between systems.
In Kotlin, data serialization tools are available in a component named Kotlinx.serialization. It supports various serialization formats.
JSON and Protocol Buffers (Protobuf) are common data serialization formats as they are language-neutral and platform-neutral. Because of these characteristics, they enable easy data exchange between applications written in any modern language.
There are two main parts of this component: org.jetbrains.kotlin.plugin.serialization and the runtime libraries.
In this latest update, let’s explore the major improvements:
Version 1.2 comes with fast JSON serialization compared to previous versions. It also includes support for alternative names for JSON field, and an approach that automatically generates Protobuf schemas from Kotlin classes – a new experimental approach.
The Kotlin community has completely rebuilt the internal structure of Kotlinx.serialization. This resulted in better performance and speed. They have rewritten the JSON decoder and made significant changes in the encoder also.
This update helps you experience the notable high speed of typical encoding and decoding tasks. It will surely have a positive impact on developers’ productivity and speed of coding.
Another notable improvement is stable JSON (de)serialization for value classes and unsigned number types.
Kotlinx.serialization 1.2 offers improved JSON encoding and decoding support for value classes and unsigned integer types – two major additions of Kotlin 1.5.
With this version, built-in JSON serialization now supports value class. Value classes (known as inline classes) don’t have any identity and can only hold values. It wraps Kotlin type in a type-safe manner.
It ensures maximum type safety without unnecessary boxing or nesting.
Kotlin 1.5.0 offers the following unsigned number types:
UByte, UShort, UInt, ULong
Kotlinx.serialization now supports these types with the release of version 1.2. Unsigned integer values can be serialized in plain representation without truncation, wrapping, or conversion to signed types.
Note: Support for value classes and unsigned integers is currently available for JSON. Integrations for CBOR and Protobuf will be available in a future release.
This release provides developers with the new @JsonNames annotation that enables alternative names for JSON fields. It fulfills the need to parse JSON fields that have different names but the same meaning.
This feature will make it easier for the developers to work with services that return differently named fields representing the same values. It helps provide your applications with elegant upgrades and also helps in schema migration.
Protocol Buffers is a high-performance, compact binary format created by Google. They are using it internally and as it improves the speed of transmission and made communication faster. Also, it occupies less space than JSON or XML. It is widely used for application-to-application communication.
Version 1.2 of Kotlinx.serialization includes an experimental schema generator for Protocol Buffers. It allows you to generate .proto files from your Kotlin data classes. .proto file can be used to generate representations of your communication schema in other languages (Python, C++, TypeScript).
The use of Kotlinx.serialization’s Protobuf integration in multi-language applications helps conveniently and efficiently manage schemas directly from source code. As it is the first iteration of the Protobuf schema generator, developers will experience some limitations while using it.
Kotlin community is trying to make the learning experience as smooth and comfortable as possible. Kotlin Serialization Guide on GitHub provides detailed knowledge of functionality and also contains examples that are easy to understand.
Kotlinx.serialization API Documentation which is completely overhauled, now comes in responsive, modern, and intuitive design. It is based on a new version of Dokka, a documentation engine for Kotlin, similar to Javadoc for Java.
It’s time to upgrade to Kotlinx.serialization 1.2! With faster JSON handling, support for value class, Protobuf schema, and alternative name support for JSON fields, this version offers the best performance and many improvements.
Furthermore, Kotlin 1.5 is released with stable language features that were presented for preview in 1.4.30. This not only helps improve overall performance but also makes Kotlin a constantly evolving, growing, and stable language in the development market.
Have you tried Kotlin yet?
SPEC INDIA, as your single stop IT partner has been successfully implementing a bouquet of diverse solutions and services all over the globe, proving its mettle as a boutique ISO 9001:2015 certified IT solutions organization. With efficient project management practices, international standards to comply, flexible engagement models and superior infrastructure, SPEC INDIA is a customer’s delight. Our skilled technical resources are apt at putting thoughts in a perspective by offering value-added reads for all.