Simple Android Studio Live Template for Serialized Name
Tags: android, kotlin, beginners
Published: 2021-05-18 23:11:21

---
title: Simple Android Studio Live Template for Serialized Name
published: true
description: 
tags: android,kotlin,beginners
//cover_image: https://direct_url_to_image.jpg
---

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](https://github.com/google/gson) library (or `@Json` from [Moshi](https://github.com/square/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](https://pokeapi.co/) 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/"
  }
}
```
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](https://www.jetbrains.com/help/idea/using-live-templates.html) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/chu1k8pllr4dfco0dyhl.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pcfg5e0mex9tq3w1shzu.png)

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$
```
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/al76qg327lu7dp7p00sx.png)

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!