Skip to content

Commit

Permalink
Added serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
fluidsonic committed Aug 12, 2020
1 parent d41ba11 commit 5de9a9b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
17 changes: 7 additions & 10 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ name: Tests
on: [push]

jobs:
js:
name: JS
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- run: ./gradlew jsTest
iOS:
name: iOS x64
runs-on: macos-latest
Expand All @@ -23,21 +29,12 @@ jobs:
- run: xcodebuild -version
- run: xcrun simctl list
- run: ./gradlew macosX64Test
jdk7:
name: JDK 7 (on JDK 8)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-java@v1
with:
java-version: 8
- run: ./gradlew jvmJdk7test
jdk:
name: JDK ${{ matrix.java_version }}
runs-on: ubuntu-latest
strategy:
matrix:
java_version: ['8', '9', '10', '11', '12', '13']
java_version: ['8', '9', '10', '11', '12', '13', '14']
steps:
- uses: actions/checkout@v1
- uses: actions/setup-java@v1
Expand Down
23 changes: 23 additions & 0 deletions sources/common/Locale.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package io.fluidsonic.locale

import kotlinx.serialization.*
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.*


@Serializable(with = LocaleSerializer::class)
public class Locale internal constructor(
internal val platform: Any
) {
Expand Down Expand Up @@ -30,5 +35,23 @@ public class Locale internal constructor(
}


@Serializer(forClass = Locale::class)
internal object LocaleSerializer : KSerializer<Locale> {

override val descriptor = PrimitiveSerialDescriptor("io.fluidsonic.locale.Locale", PrimitiveKind.STRING)


override fun deserialize(decoder: Decoder) =
decoder.decodeString().let { tag ->
Locale.parseOrNull(tag) ?: throw SerializationException("Invalid Locale tag: $tag")
}


override fun serialize(encoder: Encoder, value: Locale) {
encoder.encodeString(value.toString())
}
}


internal expect fun parseLocaleOrNull(tag: String): Locale?
internal expect fun serializeLocale(locale: Locale): String
8 changes: 8 additions & 0 deletions tests/common/LocaleTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tests

import io.fluidsonic.locale.*
import kotlin.test.*
import kotlinx.serialization.json.*


class LocaleTest {
Expand All @@ -13,4 +14,11 @@ class LocaleTest {

assertEquals(expected = expected, actual = actual)
}


@Test
fun testSerializer() {
assertEquals(expected = "en-US", actual = Json.decodeFromString(Locale.serializer(), "\"en-us\"").toString())
assertEquals(expected = "\"en-US\"", actual = Json.encodeToString(Locale.serializer(), Locale.parseOrNull("en-us")!!))
}
}

0 comments on commit 5de9a9b

Please sign in to comment.