kotlinx.serialization 1.2.0: Faster JSON Handling, Value Classes, API Docs & More

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Fantastic news! I love the new docs too.

👍︎︎ 2 👤︎︎ u/Nbness2 📅︎︎ May 14 2021 🗫︎ replies
Captions
Kotlinx.serialization 1.2 is out! This  latest version of the multiplatform   serialization library comes packed with new  features. You can transform Kotlin objects to   JSON and back faster than ever before,  unlock the full power of Kotlin 1.5’s   type system with support for value classes  and unsigned number types, and much more. Stick around, to learn more about how  kotlinx.serialization can help you,   and get the details of this latest release! It doesn’t matter if you’re writing Android  apps, using Kotlin Multiplatform Mobile,   creating server-side services, or building  web front ends with Kotlin/JS – when you’re   interacting with other services, your Kotlin  app likely needs serialization – allowing you to   parse, for example, JSON into type-safe  Kotlin objects, and the other way around. kotlinx.serialization is our official  multiplatform solution for exactly those   tasks. It gives you a simple yet versatile Kotlin  API, to help you with everything serialization. You can add kotlinx.serialization to  your project by adding the compiler   plugin and the runtime library to your  build file, and you’re ready to go! At this point, turning a basic Kotlin  object into its JSON representation   and back just takes a few steps. First, you mark the class in question  with the @Serializable annotation.  And then, you can already use  the Json object from the library,   which provides the functions  encodeToString and decodeFromString. Of course, you can also apply a ton  of customizations to the serialization   process – but even these minimal snippets are  positively affected by the latest release of   kotlinx.serialization. Because version 1.2  comes with an overhauled internal structure.   That means when we call encodeToString,  we’re now using an optimized JSON encoder,   and when doing decodeFromString, we’re invoking  a completely rewritten JSON decoder. Compared to   previous versions, you can expect up to twice the  speed – and in some highly specific situations,   even more than that. The best way to get a  feeling for these improvements is to benchmark   your own application with the newest version of  our library. Generally, kotlinx.serialization   1.2 can certainly keep up with other comparable  serialization libraries, or even exceed their   performance! These speed-ups alone are a  great reason to upgrade to Serialization 1.2. By the way – whether you’ve used  kotlinx.serialization before,   or will start to do so right after  the end of this video – we want to   make sure the experience of learning  the ins and outs of the library is as   comfortable and fun as possible. To do that,  we provide a bunch of reference materials! One of them is the Kotlin Serialization Guide  on GitHub, which offers a walkthrough for the   functionality provided by the library and  includes self-contained examples to give   you an intuitive understanding of each  feature. And as with previous versions,   we’ve updated that guide for new features that  are included with 1.2, which we’ll see up next. And another is the kotlinx.serialization  API documentation. This one has received a   complete overhaul! It’s now based on a new  version of the documentation engine Dokka.   That means it gets a new coat of paint in  the form of a clean and responsive design,   as well as hyperlinks galore, so that you can easily jump between related topics easily, and  explore existing and new features in detail. Which does bring us to the next  topic – new supported features! In my opinion, two of the most exciting  changes in Kotlin 1.5 are the introduction   of value classes and unsigned integer types. As luck would have it, kotlinx.serialization   1.2 now also offers first-class JSON  encoding and decoding support for those. Let’s talk about value classes first.   They’re a new way to wrap another Kotlin  type (e.g. a number) in a type-safe manner   without introducing additional runtime  overhead. So, with value classes,   you can make your programs more expressive and  safe without incurring performance penalties. Just like other Kotlin classes, we just have to  annotate a `value class` with `@Serializable`. Because value classes are just  wrappers around an underlying type,   kotlinx.serialization uses this underlying type  directly to perform the encoding and decoding. We can see this for example when the   encodeToString to turn an inline  class into JSON – and back. So, with inline classes, you get to enjoy  maximum type-safety inside your Kotlin code,   and kotlinx.serialization makes sure the  serialized representation is nice and concise,   without unnecessary boxing or nesting. Also landing with Kotlin 1.5 are unsigned  integers. They provide types and operations   for non-negative numbers, all the way  from unsigned bytes to unsigned longs.   Serialization 1.2 supports these types out  of the box as well. They behave just like   you would expect them to: the numbers are directly  serialized in their plain number representation. If you’re just as excited about  the introduction of value classes   and unsigned number types in Kotlin 1.5 as I  am, then I’m sure you’ll enjoy this upgrade. For now, we provide out-of-the-box support for  value classes and unsigned integers for JSON.   Integrations for CBOR and Protobuf are in   the works, so if you’re using those  serialization formats – stay tuned! Another noteworthy change in version 1.2 is that   you now have the power to specify  alternative names for JSON fields.   Those aliases will be used during the decoding  process. Let’s look at a quick example. Imagine a service returns one of two fields:   `name` and `title`. They both represent the same  information, but maybe we need to be compatible   with two different versions of the service,  which are affected by this field name change. Both `name` and `title` have the same meaning.  So, with the new `@JsonNames` annotation,   we can specify `title` as an  alternative key for `name`,   meaning both will be mapped  to the same Kotlin property. Alternative names will hopefully make it easier  for you to survive schema migrations, and provide   upgrades with backwards compatibility for  your apps using kotlinx.serialization! The last topic for today is the new Protobuf  schema generator. If you haven’t worked with   protocol buffers yet, here’s the long story  short: it’s a binary serialization format for   structured data created by Google. Because it’s a binary format,   it needs to transfer less data than text-based  formats like JSON or XML, but it still provides   a language-agnostic structure which you  can use for app-to-app communication. Now Kotlinx.serialization has provided experimental  support for Protobuf for a while now. From a usage   perspective, you can probably immediately spot  the similarities to the JSON topics we talked   about earlier. Mark the class as serializable, and  use the encodeToByteArray and decodeFromByteArray   functions on the Protobuf object. But, as you can  imagine: No strings this time, just binary data. Based on your Kotlin classes  as the “source of truth”,   kotlinx.serialization is able to infer  the binary schema for your data. That   means that multiple Kotlin applications can  easily communicate using a binary format. With Serialization 1.2, this gets expanded,   with an experimental schema  generator for Protocol Buffers. What this schema generator does is  that it allows you to generate `.proto`   files from your Kotlin data classes. Those  proto-files are just regular Protobuf schemas,   so you can use them to generate representations  of your binary structures in other languages,   including Python, C++, TypeScript, and many more. This will make it easier to use the  library’s Protobuf integration even   in multi-language applications. And of course,   we want to maintain the convenience of managing  your schemas directly in Kotlin source code. For instructions on how to use this new  schema generator and its current limitations,   alongside information on the other features we  talked about today, check out the release blog   post for this version of kotlinx.serialization. As usual, please also don’t forget to share   your feedback with us! It’s your use cases and  experiences that help us improve our integrations. Well, that’s it with the highlights of the  kotlinx.serialization 1.2 release! We’ve   seen quite a diverse set of news! How this latest version brings   significant speed-ups for everything JSON, Our updated and refreshed reference materials,  The new support for value  classes and unsigned numbers,  Alternative names for JSON properties, and fancy new schema generation for Protobuf! Looks like it’s time to upgrade, and  bring those features into your apps! It’s your turn to serialize some objects,   everyone! I hope to see you  in another video. Take care!
Info
Channel: Kotlin by JetBrains
Views: 5,873
Rating: 5 out of 5
Keywords: libraries, android, jvm, webdev, kotlin, serialization, json, protobuf, Kotlin 1.5, Kotlin 1.5.0, Kotlin 1.5 release, Kotlin 1.5.0 Release, Kotlin release
Id: 698I_AH8h6s
Channel Id: undefined
Length: 8min 13sec (493 seconds)
Published: Fri May 07 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.