commit | 26af9198d34835e236a742a56d1451b4541a2b3f | [log] [tgz] |
---|---|---|
author | Roman Elizarov <elizarov@gmail.com> | Thu Sep 21 18:12:04 2017 +0300 |
committer | Roman Elizarov <elizarov@gmail.com> | Thu Sep 21 18:12:04 2017 +0300 |
tree | e2edee6e5845b659001cd6217712d2331e147792 | |
parent | 6fbc21541d43ee4d4cacfcaf8483c0500fc3849f [diff] |
Readme and docs updated, license and copyright added
Kotlin serialization support consists of three parts: a gradle compiler plugin, an IntelliJ plugin and a runtime library.
@Serializable
and standard collections.This project contains the runtime library. Runtime library provides:
KInput
, KOutput
).ElementValueInput/Output
, NamedValueInput/Output
, ElementValueTransformer
)You can open example projects for JVM or JS to get started playing with it.
import kotlinx.serialization.* import kotlinx.serialization.json.JSON @Serializable data class Data(val a: Int, @Optional val b: String = "42") fun main(args: Array<String>) { println(JSON.stringify(Data(42))) // {"a": 42, "b": "42"} val obj = JSON.parse<Data>("""{"a":42}""") // Data(a=42, b="42") }
More examples of various kinds of Kotlin classes that can be serialized can be found here.
Runtime library provides three ready-to use formats: JSON, CBOR and ProtoBuf. Usage of the first two formats is pretty straightforward and obvious from the example above. Notes on them: because JSON doesn't support maps with keys other than strings (and primitives), Kotlin maps with non-trivial key types are serialized as JSON lists. CBOR doesn't have this limitation, and Kotlin maps are serialized as CBOR maps, but some parsers (like jackson-dataformat-cbor
) don't support this.
Because protobuf relies on serial ids of fields, called 'tags', you have to provide this information, using serial annotation @SerialId
:
@Serializable data class KTestInt32(@SerialId(1) val a: Int)
This class is equivalent to the following proto definition:
message Int32 { required int32 a = 1; }
Note that we are using proto2 semantics, where all fields are explicitly required or optional.
Number format is set via @ProtoType
annotation. ProtoNumberType.DEFAULT
is default varint encoding (intXX
), SIGNED
is signed ZigZag representation (sintXX
), and FIXED
is fixedXX
type. uintXX
and sfixedXX
are not supported yet.
Repeated fields represented as lists. Because format spec says that if the list is empty, there will be no elements in the stream with such tag, you must explicitly mark any filed of list type with @Optional
annotation with default = emptyList()
. Same for maps.
Other known issues and limitations:
More examples of mappings from proto definitions to Koltin classes can be found in test data: here and here
Using Kotlin Serialization requires compiler with version higher than 1.1.50
. Example projects on JVM are available for Gradle and Maven.
Ensure the proper version of Kotlin and add dependencies on plugin in addition to Kotlin compiler:
buildscript { ext.kotlinVersion = '1.1.50' ext.serializationVersion = '0.1' repositories { jcenter() maven { url "https://kotlin.bintray.com/kotlinx" } } } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion" classpath "org.jetbrains.kotlinx:kotlinx-gradle-serialization-plugin:$serializationVersion" } apply plugin: 'kotlin' apply plugin: 'kotlinx-serialization'
Add serialization runtime library in addition to Kotlin standard library and reflection (optional).
dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion" compile "org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion" compile "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serializationVersion" }
Ensure the proper version of Kotlin and serialization version:
<properties> <kotlin.version>1.1.50</kotlin.version> <serialization.version>0.1</serialization.version> </properties>
Include bintray repository:
<repositories> <repository> <id>bintray-kotlin-kotlinx</id> <name>bintray</name> <url>https://kotlin.bintray.com/kotlinx</url> </repository> </repositories>
Add serialization plugin to Kotlin compiler plugin:
<build> <plugins> <plugin> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-maven-plugin</artifactId> <version>${kotlin.version}</version> <executions> <execution> <id>compile</id> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> </executions> <configuration> <compilerPlugins> <plugin>kotlinx-serialization</plugin> </compilerPlugins> </configuration> <dependencies> <dependency> <groupId>org.jetbrains.kotlinx</groupId> <artifactId>kotlinx-maven-serialization-plugin</artifactId> <version>${serialization.version}</version> </dependency> </dependencies> </plugin> </plugins> </build>
Add dependency on serialization runtime library:
<dependency> <groupId>org.jetbrains.kotlinx</groupId> <artifactId>kotlinx-serialization-runtime</artifactId> <version>${serialization.version}</version> </dependency>
Replace kotlinx-serialization-runtime
with kotlinx-serialization-runtime-js
to use it in JavaScript projects. JavaScript example is located at example-js
folder.