Skip to content

Commit

Permalink
refactor(composer): Port the deserialization code from Jackson to KxS
Browse files Browse the repository at this point in the history
Note that KxS requires to explicitly state default values even for
nullable types, while Jackson implicitly defaults to null for nullable
types.

Signed-off-by: Sebastian Schuberth <sebastian@doubleopen.org>
  • Loading branch information
sschuberth committed Aug 10, 2024
1 parent 424dfcb commit 34f3760
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
8 changes: 5 additions & 3 deletions plugins/package-managers/composer/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
plugins {
// Apply precompiled plugins.
id("ort-library-conventions")

// Apply third-party plugins.
alias(libs.plugins.kotlinSerialization)
}

dependencies {
Expand All @@ -37,9 +40,8 @@ dependencies {
implementation(projects.utils.ortUtils)
implementation(projects.utils.spdxUtils)

implementation(libs.jackson.core)
implementation(libs.jackson.databind)
implementation(libs.jackson.module.kotlin)
implementation(libs.kotlinx.serialization.core)
implementation(libs.kotlinx.serialization.json)

funTestImplementation(testFixtures(projects.analyzer))
}
44 changes: 22 additions & 22 deletions plugins/package-managers/composer/src/main/kotlin/Model.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,60 +19,60 @@

package org.ossreviewtoolkit.plugins.packagemanagers.composer

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.module.kotlin.readValue
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json

import org.ossreviewtoolkit.model.jsonMapper

@JsonIgnoreProperties(ignoreUnknown = true)
@Serializable
internal data class Lockfile(
val packages: List<PackageInfo> = emptyList(),
@JsonProperty("packages-dev")
@SerialName("packages-dev")
val packagesDev: List<PackageInfo> = emptyList()
)

@JsonIgnoreProperties(ignoreUnknown = true)
@Serializable
internal data class PackageInfo(
val name: String?,
val name: String? = null,
// See https://getcomposer.org/doc/04-schema.md#version.
val version: String?,
val homepage: String?,
val description: String?,
val version: String? = null,
val homepage: String? = null,
val description: String? = null,
val source: Source? = null,
val authors: List<Author> = emptyList(),
val license: List<String> = emptyList(),
val provide: Map<String, String> = emptyMap(),
val replace: Map<String, String> = emptyMap(),
val require: Map<String, String> = emptyMap(),
@JsonProperty("require-dev")
@SerialName("require-dev")
val requireDev: Map<String, String> = emptyMap(),
val dist: Dist? = null
) {
@JsonIgnoreProperties(ignoreUnknown = true)
@Serializable
data class Author(
val name: String? = null,
val email: String? = null,
val homepage: String? = null,
val role: String? = null
)

@JsonIgnoreProperties(ignoreUnknown = true)
@Serializable
data class Source(
val type: String? = null,
val url: String? = null,
val reference: String? = null
)

@JsonIgnoreProperties(ignoreUnknown = true)
@Serializable
data class Dist(
val type: String?,
val url: String?,
val reference: String?,
val shasum: String?
val type: String? = null,
val url: String? = null,
val reference: String? = null,
val shasum: String? = null
)
}

internal fun parseLockfile(json: String): Lockfile = jsonMapper.readValue<Lockfile>(json)
private val JSON = Json { ignoreUnknownKeys = true }

internal fun parseLockfile(json: String): Lockfile = JSON.decodeFromString<Lockfile>(json)

internal fun parsePackageInfo(json: String): PackageInfo = jsonMapper.readValue<PackageInfo>(json)
internal fun parsePackageInfo(json: String): PackageInfo = JSON.decodeFromString<PackageInfo>(json)

0 comments on commit 34f3760

Please sign in to comment.