Systers, an AnitaB.org community

[Mentorship - Android] Use GSON's field naming policy to change field names to snake case.

Prerequisite:

  1. Fork and clone the project from the github repository
  2. Follow the documentation for successfully setting up the project
  3. Open Project in Android Studio

Background:

  1. Camel case and snake case are two styles of naming a variable
    • ThisIsCamelCase
    • this_is_snake_case
  2. The backend is written in python which follows snake case naming style
  3. The android client is written in Kotlin which follows camel case naming style
  4. Because of this difference between the projects we have to convert every single field name in each retrofit request by using the @SerializedName() annotation.
  5. Instead, we can use Gson's FieldNamingPolicy LOWER_CASE_WITH_UNDERSCORES, this will automatically change the field names for us and we can remove the overused @SerializedName() form data classes. # Steps:
  6. Create an instance of Gson.
  7. Add a FieldNamePolicy using the setFieldNamingPolicy() method of GsonBuilder class.
  8. Create an instance of GsonConverterFactory using the previously created instance of Gson.
  9. Use the instance of GsonConverterFactory in retrofit.

You can find an example here in the Customize Your Gson Configuration section.

Resources:

Work Submission:

After successfully fixing this issue, commit and make a Pull Request. Title of PR should contain "Program:GCI"

  1. Commit Message Style Guide
  2. Sample PR structure
  3. Make sure to attach either screenshot or GIF of the fixed issue

Task tags

  • kotlin
  • android
  • hard
  • advanced

Students who completed this task

_daksha_

Task type

  • code Code
close

2019