Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert dash case to camel case #8

Open
jotaporras opened this issue May 2, 2016 · 3 comments
Open

Convert dash case to camel case #8

jotaporras opened this issue May 2, 2016 · 3 comments

Comments

@jotaporras
Copy link

jotaporras commented May 2, 2016

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 jotaporras changed the title Your software sucks. Convert Dash case to camel case May 2, 2016
@jomarinb
Copy link

jomarinb commented May 2, 2016

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.

@jotaporras jotaporras changed the title Convert Dash case to camel case Convert dash case to camel case May 2, 2016
@chonton
Copy link

chonton commented Apr 19, 2017

Have you tried using jackson's KebabCaseStrategy?

@MrPowerGamerBR
Copy link

MrPowerGamerBR commented May 3, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants