-
Notifications
You must be signed in to change notification settings - Fork 666
Closed
Labels
Description
A very common case of serialization is when API/JSON naming policy conflicts with your code naming, for example, snake case quite popular naming style for JSON fields, but for Kotlin it's camel case. Also, format naming style can be incompatible with JVM (for example field names with dashes).
Gson provides feature to solve that: https://google.github.io/gson/apidocs/com/google/gson/FieldNamingPolicy.html (implementation of FieldNamingStrategy)
In general, it's questionable feature, Moshi doesn't provide it and suggests to use the same case for your model classes.
But it can be a big blocker if you want to migrate to kotlinx.serialization, because you have 3 choices:
- Use
@SerialNamefor each field. Tedious and error-prone and requires a lot of refactoring - Rename all your data models to match API name convention. Tedious, requires even more refactoring, violates Kotlin code style guides and backward incompatible.
- Write own implementation of JSON (or any other format) that provides such feature, looks like a reasonable solution, but now you have one more copy JSON implementation in your project (because JSON implementation now a part of kotlinx.serialization) with different name, but the same API
Possible solutions:
- Add naming policy feature to JSON implementation. Some drawbacks: doesn't work for other formats, debatable nature of this feature, required not for all use cases.
- Provide universal API that allows defining name converters. A user can provide own implementation, so not necessary to include it to core fo kotlinx.serialization, works for all formats without additional effort. By default just uses field names as is, so current behavior.
Possible problems: If you use more than one serialization format, different formats probably require different strategies, so we still should integrate naming policy to format implementation. Also, you sometimes want to use different naming policy for particular requests (from different API), so looks like Serialization format still should provide API to register and use naming policies.
egorzhdan, psartini, charleskorn, fitzoh, BoxResin and 133 moreSergeyNikolaev88, y9san9, nachtien, murs313, hovi and 7 more