JsonToKotlinClass icon indicating copy to clipboard operation
JsonToKotlinClass copied to clipboard

Feature: allow users change their property name style

Open peterstev-fm opened this issue 3 years ago • 12 comments

Allow users to determine what style they want their property naming to follow.

Camel case raw json property names (as is) maybe both camelCase and raw types if possible

for example I want my class properties to be defined as

data class JsonLogin(
    val id: Int,
    val token: String,
    val user_avatar: String,
    val user_display_name: String,
    val user_email: String,
    val user_nicename: String
)

instead of

data class Login(
    val id: Int,
    val token: String,
    val userAvatar: String,
    val userDisplayName: String,
    val userEmail: String,
    val userNicename: String
)

so I do not have to explicitly change every generated property name to suite my style, because if I have to do all that I might as well define the class manually myself.

peterstev-fm avatar May 29 '21 00:05 peterstev-fm

Hi.if you choose any annotation in annotation tab in advanced config. It will generate properties' name as camelcase

wuseal avatar May 29 '21 01:05 wuseal

@peterstev-fm for your case, refer here : https://github.com/wuseal/JsonToKotlinClass/issues/17

wuseal avatar May 29 '21 01:05 wuseal

that is the point, I do not want camelCase, I want to use snake_case in my data layer and camelCase in domain.

data layer: 
data class JsonLogin(
    val id: Int,
    val token: String,
    val user_avatar: String,
    val user_display_name: String,
    val user_email: String,
    val user_nicename: String
)

domain layer:
data class Login(
    val id: Int,
    val token: String,
    val userAvatar: String,
    val userDisplayName: String,
    val userEmail: String,
    val userNicename: String
)

with with this, I would not need to annotate every field with @Json("field_name") PS I use converter moshi. I also use mappers to map my data layer objects to the domain equivalent.

so I need to be able to config the generator to use snake_case or camelCase

peterstev-fm avatar May 29 '21 03:05 peterstev-fm

Hey, is that your json is in snake_case style? If it is true, the default behavior this plugin does Will generate your data class as snake case style, And with selecting 'None(came case)' config. The data class Will generate as came case style. With these action, you could switch to snake case or camel case style. @peterstev-fm

Is that enough for you?

wuseal avatar May 29 '21 04:05 wuseal

oh I see, it does that, but I use 'other by customize' because I want to set my class like this:

import com.squareup.moshi.JsonClass

@JsonClass(generateAdapter = true)
data class JsonUser(
    val Id: Int,
    val token: String,
    val user_avatar: String,
    val user_display_name: String,
    val user_email: String,
    val user_nicename: String
)

I use 'other by customize' because I want to add imports for the @JsonClass annotation for moshi, because if I select moshi directly from the options list, it adds the @Json("field_name") which I do not want since I am using snake_case

so how can I use 'other by customize' and still use snake_case. Thanks

peterstev-fm avatar May 29 '21 04:05 peterstev-fm

Got it. Currently plugin doesn't support your requst, And I want to know that why you choose snake case style, as I know it's not kotlin standard style. Does it means that your code all in snake case style? @peterstev-fm

wuseal avatar May 29 '21 06:05 wuseal

To achieve your requst feature, We need creat a new extension to implement it, it's not hard

wuseal avatar May 29 '21 06:05 wuseal

Got it. Currently plugin doesn't support your requst, And I want to know that why you choose snake case style, as I know it's not kotlin standard style. Does it means that your code all in snake case style? @peterstev-fm

No my entire code isn't like this, I use this in the data layer of my application, so that I don't have to clutter my class with @serializedName("name") or @Json("name")

The domain layer uses camelCase, then I use mappers to map the data layer classes to the domain layer.

peterstev-fm avatar May 29 '21 06:05 peterstev-fm

To achieve your requst feature, We need creat a new extension to implement it, it's not hard

Oh nice, good to know it's not hard. How soon can it be done 😁

peterstev-fm avatar May 29 '21 06:05 peterstev-fm

So, what you want is just not use the property annotation with moshi😅?

It can take several hours to implement it, But I have other tasks prefer to do, If you have interest,Hope you can do it by yourself, it's easy. Refer here: src/main/kotlin/extensions/yuan/varenyzc/CamelCaseSupport.kt

Write one class like that(we may named it as let all property' name to be snake case style) and add it into here is ok

'src/main/kotlin/extensions/ExtensionsCollector.kt'

That's all need to be done

@peterstev-fm

wuseal avatar May 29 '21 06:05 wuseal

So, what you want is just not use the property annotation with moshi😅?

It can take several hours to implement it, But I have other tasks prefer to do, If you have interest,Hope you can do it by yourself, it's easy. Refer here: src/main/kotlin/extensions/yuan/varenyzc/CamelCaseSupport.kt

Okay I've seen the CamelCaseSupport file, I'll build the feature and create a PR.

That's good?

peterstev-fm avatar May 29 '21 06:05 peterstev-fm

Good! Very good!😄

wuseal avatar May 29 '21 08:05 wuseal