Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
sandwwraith committed Nov 19, 2019
2 parents cea9b43 + 8a8335d commit 0137868
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 16 deletions.
33 changes: 33 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Contributing

## Report a bug

Just use a 'bug report' template when creating a new issue.

## Submit a PR

The process of submitting a pull request depends on what kind of changes you have in it:

#### Fix typo/documentation clarification

You can open your PR directly against a `master` branch.

#### Fix a minor bug

Open your PR against the `dev` branch of the repository with a meaningful message in the description.

#### Fix a bug in the library core/implement a new feature

First, open an issue from 'feature request' template or describe in the existing issue what idea you are trying to implement
– probably it needs some tuning to be more aligned with the whole library design and vision.
After a design discussion, you can open a PR against the `dev` branch.

## Propose/implement a new serialization format

If you have created a new serialization format, you can keep it
in your repository to be able to perform proper maintenance and to have a separate release cycle.
You can submit a PR to the [list of community-supported formats](formats/README.md#other-community-supported-formats) with a description of your library.

## Building this library

You can find all the instructions [here](docs/building.md)
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ You can open example projects for [JS](examples/example-js), [JVM](examples/exam
* [Current status](#current-project-status)
* [Library installing](#setup)
+ [Gradle](#gradle)
+ [Gradle (with `plugins` block)](#gradle--with--plugins--block-)
+ [Android/JVM](#android-jvm)
+ [Multiplatform (common, JS, Native)](#multiplatform--common--js--native-)
+ [Maven/JVM](#maven-jvm)
+ [Gradle (with `plugins` block)](#gradle-with-plugins-block)
+ [Android/JVM](#androidjvm)
+ [Multiplatform (common, JS, Native)](#multiplatform-common-js-native)
+ [Maven/JVM](#mavenjvm)
+ [Incompatible changes from older versions](#incompatible-changes)
* [Troubleshooting IntelliJ IDEA](#troubleshooting-intellij-idea)
* [Usage](docs/runtime_usage.md)
Expand All @@ -55,7 +55,7 @@ import kotlinx.serialization.json.*
@Serializable
data class Data(val a: Int, val b: String = "42")

fun main(args: Array<String>) {
fun main() {
// Json also has .Default configuration which provides more reasonable settings,
// but is subject to change in future versions
val json = Json(JsonConfiguration.Stable)
Expand Down Expand Up @@ -164,7 +164,7 @@ You may also want to keep all custom serializers you've defined.

Platform artifacts have the same names as JVM one, but with additional suffix (e.g. `org.jetbrains.kotlinx:kotlinx-serialization-runtime-native`). For Native artifact, Gradle metadata is required (put the line `enableFeaturePreview('GRADLE_METADATA')` in your `gradle.properties`) and minimal supported version of Gradle is 5.3.

Typically, you need the following dependencies in your multiplatform project (don't forget to rename [source sets](https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#configuring-source-sets) according yo your setup):
Typically, you need the following dependencies in your multiplatform project (don't forget to rename [source sets](https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#configuring-source-sets) according to your setup):

```gradle
sourceSets {
Expand Down Expand Up @@ -287,10 +287,14 @@ Add dependency on serialization runtime library:

### Incompatible changes

All versions of library before `0.13.0` are using Gradle metadata v0.4 and therefore it is recommended to use Gradle 4.8-5.1 to build
All versions of library before `0.13.0` are using Gradle metadata v0.4 and therefore it is recommended to use Gradle 4.8-5.1 to build.

Library versions `0.11.0` and higher require Kotlin 1.3.30 and higher and incompatible with previous versions.

All versions of library before `0.10.0` are using Gradle metadata v0.3 and therefore require Gradle 4.7 for build.

Maven plugin coordinates before Kotlin 1.3.20 were `kotlinx-maven-serialization-plugin`.

For deprecated `kotlin-platform-native` plugin, you need to use `kotlinx-serialization-native` plugin (see [#2210](https://github.com/JetBrains/kotlin-native/issues/2210#issuecomment-429753168)).

## Troubleshooting IntelliJ IDEA
Expand Down
11 changes: 6 additions & 5 deletions docs/building.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

## Runtime library

Kotlin Serialization runtime library itself is a [multiplatform](http://kotlinlang.org/docs/reference/multiplatform.html) project,
but it is still using an old multiplatform system from Kotlin 1.2.
To build library from the source and run all tests, use `./gradlew build`. To install it into the local Maven repository, run `./gradlew publishToMavenLocal`.
Kotlin Serialization runtime library itself is a [multiplatform](http://kotlinlang.org/docs/reference/multiplatform.html) project.
To build library from the source and run all tests, use `./gradlew build`. Corresponding platform tasks like `jvmTest`, `jsTest` and so on are also available.

To install it into the local Maven repository, run `./gradlew publishToMavenLocal`.
After that, you can include this library in arbitrary projects like usual gradle dependency:

```gradle
Expand All @@ -19,8 +20,8 @@ dependencies {
```

To open project in Intellij IDEA, first run `./gradlew generateTestProto` from console.
Make sure you've set an option 'Use Gradle wrapper' on import to use a correct (4.7) version of Gradle. Note that Gradle 4.7 is not compatible with Java 10 or 11.
You may also need to mark `runtime/build/generated/source/proto/test/java` as 'Generated source root' to build project without delegating a build to Gradle.
Make sure you've set an option 'Use Gradle wrapper' on import to use a correct version of Gradle.
You may also need to mark `runtime/build/generated/source/proto/test/java` as 'Generated source root' to build project/run tests in IDE without delegating a build to Gradle.
This requires Kotlin 1.3.11 and higher.

To use snapshot version of compiler (if you have built it from sources), use flag `-Pbootstrap`. To compile and publish all Native artifacts, not only the host one, use `-Pnative.deploy=true`.
Expand Down
27 changes: 27 additions & 0 deletions formats/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,33 @@ You can learn about "Human-Optimized Config Object Notation" or HOCON from libra

## Other community-supported formats

### Avro

* GitHub repo: [sksamuel/avro4k](https://github.com/sksamuel/avro4k)
* Artifact ID: `com.sksamuel.avro4k:avro4k`
* Platform: JVM only

This library allows serialization and deserialization of objects to and from [Avro](https://avro.apache.org). It will read and write from Avro binary or json streams or generate Avro Generic Records directly. It will also generate Avro schemas from data classes. The library allows for easy extension and overrides for custom schema formats, compatiblity with schemas defined outside out of the JVM and for types not supported out of the box.

### Bson

* GitHub repo: [jershell/kbson](https://github.com/jershell/kbson)
* Artifact ID: `com.github.jershell:kbson`
* Platform: JVM only

Allows serialization and deserialization of objects to and from [BSON](https://docs.mongodb.com/manual/reference/bson-types/).

### XML
* GitHub repo: [pdvrieze/xmlutil](https://github.com/pdvrieze/xmlutil)
* Artifact ID: `net.devrieze:xmlutil-serialization`
* Platform: JVM, Android, JavaScript

This library allows for reading and writing of XML documents with the serialization library.
It is multiplatform, but as the xmlutil library (which handles the multiplatform xml bit)
delegates to platform specific parsers each platform needs to be implemented for each platform
specifically. The library is designed to handle existing formats that use features that would
not be available in other formats such as JSON.

### YAML

* GitHub repo: [charleskorn/kaml](https://github.com/charleskorn/kaml)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ class ConfigParser(context: SerialModule = EmptyModule): AbstractSerialFormat(co
}

private inner class ConfigReader(val conf: Config) : ConfigConverter<String>() {
private var ind = -1

override fun decodeElementIndex(desc: SerialDescriptor): Int {
while (++ind < desc.elementsCount) {
val name = desc.getTag(ind)
if (conf.hasPathOrNull(name)) {
return ind
}
}
return READ_DONE
}

private fun composeName(parentName: String, childName: String) =
if (parentName.isEmpty()) childName else parentName + "." + childName

Expand All @@ -79,6 +91,7 @@ class ConfigParser(context: SerialModule = EmptyModule): AbstractSerialFormat(co

override fun beginStructure(desc: SerialDescriptor, vararg typeParams: KSerializer<*>): CompositeDecoder = when {
desc.kind.listLike -> ListConfigReader(conf.getList(currentTag))
desc.kind.objLike -> if (ind > -1) ConfigReader(conf.getConfig(currentTag)) else this
desc.kind == StructureKind.MAP -> MapConfigReader(conf.getObject(currentTag))
else -> this
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ConfigParserObjectsTest {
data class Simple(val a: Int)

@Serializable
data class ConfigObjectInner(val e: String)
data class ConfigObjectInner(val e: String, val f: Float = 1.1f)

@Serializable
data class ConfigObject(val a: Int, val b: ConfigObjectInner)
Expand Down Expand Up @@ -68,7 +68,7 @@ class ConfigParserObjectsTest {
inner = [{ a: 100500 }]
ll = [[a, b],[x,z]]
m : {
kek: {e: foo }
kek: {e: foo, f: 5.6 }
bar: {e: baz }
}
"""
Expand All @@ -92,7 +92,7 @@ class ConfigParserObjectsTest {
assertEquals(listOf(1, 2, 3), iList)
assertEquals(listOf(Simple(100500)), inner)
assertEquals(listOf(listOf("a", "b"), listOf("x", "z")), ll)
assertEquals(mapOf("kek" to ConfigObjectInner("foo"), "bar" to ConfigObjectInner("baz")), m)
assertEquals(mapOf("kek" to ConfigObjectInner("foo", f = 5.6f), "bar" to ConfigObjectInner("baz")), m)
}
}

Expand Down Expand Up @@ -133,6 +133,7 @@ class ConfigParserObjectsTest {
val obj = ConfigParser.parse(conf, ConfigObject.serializer())
assertEquals(42, obj.a)
assertEquals("foo", obj.b.e)
assertEquals(1.1f, obj.b.f)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ class ConfigParserValuesTest() {
data class StringConfig(val c: Char, val s: String)

@Serializable
data class OtherConfig(val b: Boolean, val e: Choice, val u: Unit)
data class OtherConfig(val b: Boolean, val e: Choice, val u: Unit = Unit)

@Serializable
data class WithDefault(val i: Int = 5, val s: String = "foo")

@Serializable
data class WithNullable(val i: Int?, val s: String?)
Expand Down Expand Up @@ -67,6 +70,20 @@ class ConfigParserValuesTest() {
assertEquals(true, obj.b)
}

@Test
fun `deserialize default values`() {
val obj = deserializeConfig("", WithDefault.serializer())
assertEquals(5, obj.i)
assertEquals("foo", obj.s)
}

@Test
fun `overwrite default values`() {
val obj = deserializeConfig("i = 42, s = bar", WithDefault.serializer())
assertEquals(42, obj.i)
assertEquals("bar", obj.s)
}

@Test
fun `deserialize nullable types`() {
val obj = deserializeConfig("i = 10, s = null", WithNullable.serializer())
Expand Down

0 comments on commit 0137868

Please sign in to comment.