MOSHI json - Automatic convertion of property names to/from snake case

Viewed 1466

Is there any way to automatically apply naming conventions to Moshi when serializing data using Moshi?

e.g.

I have this class which follows Kotlin naming conventions for properties (camel case)

class Player {
    var currentHealth: Int = 100
    var firstName = "John"
    var lastName = "Doe"
}

I would like to serialize it to something which will stick to the naming conventions we have on the server (snake case):

{ 
    current_health: 100,
    first_name: "John",
    last_name: "Doe"
} 

I already know we can customize column names on each property manually. Just looking for a way to do that automatically (for instance if we add properties to the POJO or on server side).

1 Answers

I'm one of the Moshi developers and it doesn't have this feature because I think it is harmful. I've described why in full detail here. In brief, case mapping breaks your ability to search across your codebase.

Related