jclawson / jackson-dataformat-hocon

Jackson parser format for parsing HOCON
Apache License 2.0
20 stars 9 forks source link

Convert dash case to camel case #8

Open jotaporras opened 8 years ago

jotaporras commented 8 years ago

When parsing HOCON, the mapper does not recognize dash case (e.g. 'sample-key: "a"') to convert it to camel case ('sampleKey')

PS: Sorry for the previous title. My co-worker was messing with my computer.

jomarinb commented 8 years ago

jotaporras changed the title from Your software sucks. to Convert Dash case to camel case 2 minutes ago

Sorry, my fault, I was fooling around. Hope you understand.

chonton commented 7 years ago

Have you tried using jackson's KebabCaseStrategy?

MrPowerGamerBR commented 5 years ago

I'm not sure if this is what you want, but...

    val HOCON_MAPPER = ObjectMapper(HoconFactory()).apply {
        this.enable(MapperFeature.ALLOW_EXPLICIT_PROPERTY_RENAMING)

        this.propertyNamingStrategy = object: PropertyNamingStrategy.PropertyNamingStrategyBase() {
            override fun translate(p0: String): String {
                val newField = StringBuilder()

                for (ch in p0) {
                    if (ch.isUpperCase()) {
                        newField.append('-')
                    }
                    newField.append(ch.toLowerCase())
                }

                return newField.toString()
            }
        }
    }

This allows you to use properties with camelCase while the source config (in HOCON) uses dashed-variables

class TestConfig(
        @JsonProperty("camelCaseParam")
        val camelCaseParam: String
)
val testConfig = Constants.HOCON_MAPPER.readValue<TestConfig>("camel-case-param = hello world")
println(testConfig.camelCaseParam)

displays hello world