Serialize built-in types
The Kotlin serialization library supports a variety of built-in types, including basic types such as primitives and strings, as well as certain standard library classes. The following sections describe these types in detail and show how to serialize them.
Basic types
Kotlin serialization provides built-in serializers for types that are represented as a single value in serialized data. This includes primitives, strings, and enums.
For example, here's how you can serialize a Long type:
Numbers
You can serialize all Kotlin number types, including integers and floating-point numbers, using their natural JSON representations:
Unsigned numbers
Kotlin serialization supports Kotlin's unsigned integer types like UByte and UInt. In JSON, these values are serialized as regular JSON numbers and preserve their full unsigned range:
Long numbers as strings
You can represent Long numbers as strings in JSON. This is useful in JavaScript environments, where JavaScript's Number type can't precisely represent all Kotlin Long values, which may lead to precision loss.
Use LongAsStringSerializer with the @Serializable annotation to encode Long values as strings in JSON:
Enum classes
All enum classes are serializable by default without the @Serializable annotation. When serialized in JSON, an enum is encoded as a string:
Customize serial names of enum entries
To customize the serial names of enum entries, use the @SerialName annotation and mark the enum class with @Serializable:
For more information on customizing serial names, see Customize serial names.
Standard library types
Kotlin serialization supports several types from the standard library, but some classes, such as ranges and the Regex class, aren't supported.
Pair and triple
You can serialize the Pair and Triple classes from the Kotlin standard library:
Collections
Kotlin serialization supports collection types, including both read-only and mutable variants of List, Set, and Map. It also supports their concrete implementations such as ArrayList and LinkedHashSet, as well as generic and primitive array types. The way these collections are represented depends on the serialization format.
In JSON, lists and sets are serialized as JSON arrays, and maps are represented as JSON objects.
Kotlin uses the declared type to deserialize JSON. During deserialization, the type of the resulting object is determined by the static type specified in the source code. This type can be either the type of the property or the type parameter of the decoding function.
Serialize lists
Kotlin serialization serializes List types as JSON arrays. Here's an example with a list of classes:
Serialize sets
Set types are serialized as JSON arrays, just like List types:
Serialize maps
Kotlin serialization supports Map types with primitive or enum keys:
Map serialization depends on the format. In JSON, maps are represented as objects. Since JSON object keys are always strings, keys are encoded as strings even if they are numbers in Kotlin. Other formats, such as CBOR, support maps with non-primitive keys and preserve them as such.
Deserialization behavior of collections
Kotlin uses the declared type to deserialize JSON. For example, with collections, a List preserves duplicates, while a Set enforces uniqueness:
Unit and singleton objects
Kotlin's Unit type and other singleton objects are serializable. A singleton is a class with only one instance, where the state is defined by the object itself rather than by external properties. In JSON, singleton objects are serialized as empty structures:
Duration and Instant
Kotlin's Duration type is serialized to a string using the ISO-8601-2 format:
You can also serialize Kotlin's Instant type as a string representing a point in time using the ISO-8601-1 format:
Nothing
The Nothing type is serializable by default. It has no instances, so encoding or decoding it throws an exception. Use Nothing when a type is syntactically required, but not involved in serialization, like in a polymorphic hierarchy with a generic base type:
What's next
Dive into Serialize classes to learn how to serialize classes and how to modify the default behavior of the
@Serializableannotation.To explore more complex JSON serialization scenarios, see JSON serialization overview.
Learn more about polymorphism and serializing different types through a shared base in Serialize polymorphic classes.