jackson-dataformat-hocon icon indicating copy to clipboard operation
jackson-dataformat-hocon copied to clipboard

Convert dash case to camel case

Open jotaporras opened this issue 8 years ago • 3 comments

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.

jotaporras avatar May 02 '16 21:05 jotaporras

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.

jomarinb avatar May 02 '16 21:05 jomarinb

Have you tried using jackson's KebabCaseStrategy?

chonton avatar Apr 19 '17 05:04 chonton

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

MrPowerGamerBR avatar May 03 '19 22:05 MrPowerGamerBR