Skip to content

Commit

Permalink
Reconfigure Jackson to make JSON output stable
Browse files Browse the repository at this point in the history
  • Loading branch information
f4lco committed Nov 8, 2024
1 parent f89a256 commit 66845ba
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
{
"collected" : [ {
"module" : {
"version" : "1.9",
"group" : "org.apache.commons",
"module" : {
"group" : "org.apache.commons",
"name" : "commons-text"
},
"name" : "commons-text"
"name" : "commons-text",
"version" : "1.9"
},
"lag_days" : 1361
}, {
"module" : {
"version" : "4.4",
"group" : "org.apache.commons",
"module" : {
"group" : "org.apache.commons",
"name" : "commons-collections4"
},
"name" : "commons-collections4"
"name" : "commons-collections4",
"version" : "4.4"
},
"lag_days" : 1806
} ],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.libyear.traversal

import com.fasterxml.jackson.databind.MapperFeature
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import com.libyear.sourcing.VersionOracle
import com.libyear.util.formatApproximate
Expand Down Expand Up @@ -85,14 +87,14 @@ class ReportingVisitor(
val report = mapOf(
"collected" to collected.map {
mapOf(
"module" to it.module,
"module" to ReportModule(it.module),
"lag_days" to it.lag.toDays()
)
},
"missing_info" to missingInfo,
"errors" to errors
"missing_info" to missingInfo.map(::ReportModule),
"errors" to errors.map(::ReportModule)
)
val objectMapper = ObjectMapper().registerKotlinModule()
val objectMapper = ObjectMapper().registerKotlinModule().configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
val json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(report)
val reportFile = File(project.buildDir, "reports/libyear/libyear.json")
reportFile.parentFile.mkdirs()
Expand All @@ -106,4 +108,15 @@ class ReportingVisitor(
ModuleVersionIdentifier::getName,
ModuleVersionIdentifier::getVersion
)

/** JSON-serializable subset of [ModuleVersionIdentifier]. **/
data class ReportModule(
val group: String,
val name: String,
val version: String
) {

constructor(mvi: ModuleVersionIdentifier):
this(mvi.group, mvi.name, mvi.version)
}
}

0 comments on commit 66845ba

Please sign in to comment.