You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The FieldRenamer currently overrides the field name specified by @Json(name="fieldName") making it difficult to override the FieldRenamer for specific fields.
The FieldRenamer is very useful when many of the JSON field names follow a different pattern to the Kotlin field names.
However when just one or two fields follow a different pattern, it would be useful to be able to use the @Json(name="fieldName") annotation to specify the exception to the rule.
The current implementation of the parsing means that the @Json annotation is processed first, then the result of that is passed to the FieldRenamer, resulting in the name provided in the annotation also being renamed.
It seems like it would be more flexible if the @Json annotation overrode the FieldRenamer.
I.e. if a @Json(name="fieldName") is present on a field, then the FieldRenamer would be skipped. The FieldRenamer would only rename fields with no name explicitly provided by an annotation.
Example
For example the following JSON could have many underscore_case fields, and just one or two fields following a different pattern.
{
"first_name": "John",
"last_name": "Smith",
// ... Many more underscore_case fields ..."birthDate": "2024-01-01"
}
Ideally it would be possible to parse the above JSON with the following data class.
The one differently named field has the @Json("birthDate") annotation to override/skip automatic field renaming.
classPerson(
valfirstName:String,
vallastName:String,
// ... Many more fields ...
@Json("birthDate")
varbirthDate:String
)
Klaxon would have a FieldRenamer that renames camelCase to underscore_case to parse the majority of fields.
val renamer =object:FieldRenamer {
overridefuntoJson(fieldName:String) =FieldRenamer.camelToUnderscores(fieldName)
overridefunfromJson(fieldName:String) =FieldRenamer.underscoreToCamel(fieldName)
}
val klaxon =Klaxon().fieldRenamer(renamer)
val person = klaxon.parse<Person>(json)
This currently does not behave as desired and results in an error parsing the JSON com.beust.klaxon.KlaxonException: Unable to instantiate Person:No argument provided for a required parameter: parameter #2 birthDate of fun <init>(kotlin.String, kotlin.String, kotlin.String): Person
Proposed code change
Proposed retrieveJsonFieldName implementation which uses the @Json(name="fieldName") if present, otherwise renames the field property name.
Issue
The
FieldRenamer
currently overrides the field name specified by@Json(name="fieldName")
making it difficult to override theFieldRenamer
for specific fields.The
FieldRenamer
is very useful when many of the JSON field names follow a different pattern to the Kotlin field names.However when just one or two fields follow a different pattern, it would be useful to be able to use the
@Json(name="fieldName")
annotation to specify the exception to the rule.The current implementation of the parsing means that the
@Json
annotation is processed first, then the result of that is passed to theFieldRenamer
, resulting in the name provided in the annotation also being renamed.It seems like it would be more flexible if the
@Json
annotation overrode theFieldRenamer
.I.e. if a
@Json(name="fieldName")
is present on a field, then theFieldRenamer
would be skipped. TheFieldRenamer
would only rename fields with no name explicitly provided by an annotation.Example
For example the following JSON could have many underscore_case fields, and just one or two fields following a different pattern.
Ideally it would be possible to parse the above JSON with the following data class.
The one differently named field has the
@Json("birthDate")
annotation to override/skip automatic field renaming.Klaxon would have a
FieldRenamer
that renames camelCase to underscore_case to parse the majority of fields.This currently does not behave as desired and results in an error parsing the JSON
com.beust.klaxon.KlaxonException: Unable to instantiate Person:No argument provided for a required parameter: parameter #2 birthDate of fun <init>(kotlin.String, kotlin.String, kotlin.String): Person
Proposed code change
Proposed
retrieveJsonFieldName
implementation which uses the@Json(name="fieldName")
if present, otherwise renames the field property name.The text was updated successfully, but these errors were encountered: