DEV Community

Luis Mierez
Luis Mierez

Posted on

Simple Android Studio Live Template for Serialized Name

Most times when building an application we have to talk to some back end service we do not control. That back end service might use a different naming scheme than what we use in Kotlin/Java. In comes the @SerializedName from the Gson library (or @Json from Moshi) to the rescue. It allows us to tell our deserializer where to look for the fields. However that means that we have to write some extra code in our objects since we have to add that @SerializedName annotation to each field that has a different naming scheme.

Let's look at the poke api as an example. The Poke Api has a endpoint to get info on berries. That object looks like:

{
  "id": 1,
  "name": "cheri",
  "growth_time": 3,
  "max_harvest": 5,
  "natural_gift_power": 60,
  "size": 20,
  "smoothness": 25,
  "soil_dryness": 15,
  "firmness": {
    "name": "soft",
    "url": "https://pokeapi.co/api/v2/berry-firmness/2/"
  },
  "flavors": [
    {
      "potency": 10,
      "flavor": {
        "name": "spicy",
        "url": "https://pokeapi.co/api/v2/berry-flavor/1/"
      }
    }
  ],
  "item": {
    "name": "cheri-berry",
    "url": "https://pokeapi.co/api/v2/item/126/"
  },
  "natural_gift_type": {
    "name": "fire",
    "url": "https://pokeapi.co/api/v2/type/10/"
  }
}
Enter fullscreen mode Exit fullscreen mode

Which as you can see has a couple of fields that use underscores. When making that object in our code we'll have to add a lot of the SerializedName annotation. Since I'm lazy and didn't want to have to copy that a bunch of time I created a Live Template that does most of the heavy lifting.

To create a Live Template you need to go to Preferences -> Editor -> Live Templates. In there you'll see all the Live Templates you currently have in the app. For our case, we want to add one for kotlin so go to the kotlin group, hit the + button and then hit Live Template.

Alt Text

Once you create the Live Template now it's time to fill it with what we want it to do. We should give it a good abbreviation since that is how we'll call our Live Template from our code.

Alt Text

I like the abbreviation sername for SerializedName so we'll go with that. You can see in the image above that it's giving you a warning that there is no applicable context so hit Define and let's select the drop down for kotlin, and select class and object declaration. This context is where we can call this live template from, in our case we only want to call it from a kotlin class when declaring an object.

Now to the cool part of Live Templates. In the Template Text input box, we'll write this

@com.google.gson.annotations.SerializedName("$PARAM$") val $PARAM1$: $END$
Enter fullscreen mode Exit fullscreen mode

Let's run through this line to explain it.
Live Templates are smart enough to auto import your dependencies if they aren't there already, but for that to work we need to give it the full path with the @ at the front.
The $PARAM$ means that it is inputted by the user. Same with $PARAM1$.
$END$ is a special case which tells Live Template where to put the cursor once you are done.

Once we write that, hit the Edit Variables button to add some more settings.

Alt Text

The $PARAM1$ field will read from the $PARAM$ since we want to copy the name from the back end service. However, we want to change the text to be camel case.

Once we add that we are done! To see it in action, create a new class and when adding an object that needs the @SerializedName annotation, type sername and you'll see how it automatically adds everything. Happy coding!

Top comments (0)