forked from oss-review-toolkit/ort
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
clearly-defined: Port from Jackson to kotlinx.serialization
Note that Retrofit calls `toString()` to convert enums used as part of @get paths to strings. In particular, Retrofit does not use the underlying serializer's string representation. Also see [1]. [1]: JakeWharton/retrofit2-kotlinx-serialization-converter#39 Signed-off-by: Sebastian Schuberth <sebastian.schuberth@bosch.io>
1 parent
3cf5a3c
commit 2ea13c8
Showing
9 changed files
with
303 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright (C) 2022 Bosch.IO GmbH | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* License-Filename: LICENSE | ||
*/ | ||
|
||
package org.ossreviewtoolkit.clients.clearlydefined | ||
|
||
import java.io.File | ||
import java.net.URI | ||
|
||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.descriptors.PrimitiveKind | ||
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
import kotlinx.serialization.json.JsonDecoder | ||
import kotlinx.serialization.json.JsonObject | ||
import kotlinx.serialization.json.JsonPrimitive | ||
import kotlinx.serialization.json.jsonPrimitive | ||
|
||
object CoordinatesSerializer : KSerializer<Coordinates> by toStringSerializer(::Coordinates) { | ||
override fun deserialize(decoder: Decoder): Coordinates { | ||
require(decoder is JsonDecoder) | ||
return when (val element = decoder.decodeJsonElement()) { | ||
is JsonPrimitive -> Coordinates(element.content) | ||
is JsonObject -> Coordinates( | ||
ComponentType.fromString(element.getValue("type").jsonPrimitive.content), | ||
Provider.fromString(element.getValue("provider").jsonPrimitive.content), | ||
element["namespace"]?.jsonPrimitive?.content, | ||
element.getValue("name").jsonPrimitive.content, | ||
element["revision"]?.jsonPrimitive?.content | ||
) | ||
else -> throw IllegalArgumentException("Unsupported JSON element $element.") | ||
} | ||
} | ||
} | ||
|
||
object FileSerializer : KSerializer<File> by toStringSerializer(::File) | ||
|
||
object URISerializer : KSerializer<URI> by toStringSerializer(::URI) | ||
|
||
inline fun <reified T : Any> toStringSerializer(noinline create: (String) -> T): ToStringSerializer<T> = | ||
ToStringSerializer(T::class.java.name, create) | ||
|
||
open class ToStringSerializer<T : Any>(serialName: String, private val create: (String) -> T) : KSerializer<T> { | ||
override val descriptor = PrimitiveSerialDescriptor(serialName, PrimitiveKind.STRING) | ||
override fun serialize(encoder: Encoder, value: T) = encoder.encodeString(value.toString()) | ||
override fun deserialize(decoder: Decoder) = create(decoder.decodeString()) | ||
} |
57 changes: 57 additions & 0 deletions
57
clients/clearly-defined/src/test/kotlin/ClearlyDefinedServiceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright (C) 2022 Bosch.IO GmbH | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* License-Filename: LICENSE | ||
*/ | ||
|
||
package org.ossreviewtoolkit.clients.clearlydefined | ||
|
||
import io.kotest.core.spec.style.WordSpec | ||
import io.kotest.inspectors.forAll | ||
import io.kotest.matchers.shouldBe | ||
|
||
import java.io.File | ||
import java.net.URI | ||
|
||
import kotlinx.serialization.encodeToString | ||
|
||
class ClearlyDefinedServiceTest : WordSpec({ | ||
"Serialization to a string representation" should { | ||
"work for File" { | ||
ClearlyDefinedService.JSON.encodeToString( | ||
FileEntry(path = File("dummy")) | ||
) shouldBe """{"path":"dummy"}""" | ||
} | ||
|
||
"work for URI" { | ||
ClearlyDefinedService.JSON.encodeToString( | ||
URLs(registry = URI("https://example.com")) | ||
) shouldBe """{"registry":"https://example.com"}""" | ||
} | ||
|
||
"work for ComponentType" { | ||
enumValues<ComponentType>().forAll { | ||
ClearlyDefinedService.JSON.encodeToString(it) shouldBe "\"$it\"" | ||
} | ||
} | ||
|
||
"work for Provider" { | ||
enumValues<Provider>().forAll { | ||
ClearlyDefinedService.JSON.encodeToString(it) shouldBe "\"$it\"" | ||
} | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters