Skip to content

@JsonPropertyOrder ignored for dataformats-csv #279

@kenyee

Description

@kenyee

Using Kotlin, defining a class like this:


@JsonPropertyOrder(
    "Module", "FilePath", "Line", "Offset", "Severity", "UniqueID", "Category", "Description",
    "FileCount, LinesOfCode", "ComplexityTotal", "CoverageTotal",
    "NumBlockers", "NumCriticals", "NumMajors", "NumMinors", "NumInfos"
)
private data class SonarFileStatsCsv(
    @field:JsonProperty("Module")
    val module: String,
    @field:JsonProperty("FilePath")
    val filePath: String = "-",
    @field:JsonProperty("Line")
    val line: String = "-",
    @field:JsonProperty("Offset")
    val offset: String = "-",
    @field:JsonProperty("Severity")
    val severity: String = "-",
    @field:JsonProperty("UniqueID")
    val uniqueId: String = "-",
    @field:JsonProperty("Category")
    val category: String = "-",
    @field:JsonProperty("Description")
    val description: String = "-",
    @field:JsonProperty("FileCount")
    val fileCount: Int,
    @field:JsonProperty("LinesOfCode")
    val linesOfCode: Int,
    @field:JsonProperty("ComplexityTotal")
    val complexityTotal: Int,
    @field:JsonProperty("CoverageTotal")
    val coverageTotal: Float,
    @field:JsonProperty("NumBlockers")
    val numBlockers: Int,
    @field:JsonProperty("NumCriticals")
    val numCriticals: Int,
    @field:JsonProperty("NumMajors")
    val numMajors: Int,
    @field:JsonProperty("NumMinors")
    val numMinors: Int,
    @field:JsonProperty("NumInfos")
    val numInfos: Int
)

And writing it out using:


        val mapper = CsvMapper().apply {
            disable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)
        }
        val schema: CsvSchema = mapper.schemaFor(SonarFileStatsCsv::class.java)
            .withHeader()
     val csvWriter: ObjectWriter = mapper.writer(schema)
        withContext(Dispatchers.IO) {
            csvWriter.writeValue(File(fileName), sonarCsvStats)
        }

Doesn't work...the order is not preserved.
Workaround is to use the addColumn syntax to force the order:

        val schema = CsvSchema.builder()
            .addColumn("Module")
...

Also had to use @field:JsonProperty...using just @JsonProperty and withHeaders() wrote out the field names instead of the @JsonProperty name.

Used:
Kotlin 1.3.72
dataformats-csv 2.12.2
jackson 2.12.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    csvneed-test-caseFurther progress requires inclusion of problem reproduction, ideally test case

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions