# Difference in serialization of data class fields

**URL:** <https://discuss.kotlinlang.org/t/difference-in-serialization-of-data-class-fields/3802>\
**Category:** Web Development\
**Created:** [July 19, 2017, 2:38pm UTC](https://discuss.kotlinlang.org/t/difference-in-serialization-of-data-class-fields/3802 "2017-07-19T14:38:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Void](https://avatars.discourse-cdn.com/v4/letter/v/8baadc/32.png) [@Void](https://discuss.kotlinlang.org/u/Void)\
**Post date:** [July 19, 2017, 2:38pm UTC](https://discuss.kotlinlang.org/t/difference-in-serialization-of-data-class-fields/3802/1 "2017-07-19T14:38:06Z")

</div>

Found out that constructor fields are handled differently by Jackson ( and Gson ).

Spring response and controller:

```auto
data class SampleResponse (
  @JsonProperty("changedFieldA")
  val fieldA: String = "valueA"
) {
  @JsonProperty("changedFieldB")
  val fieldB: String = "valueB"
}

class SampleController {
  @GetMapping
  fun handle(): SampleResponse {
    return SampleResponse()
  }
}

```

Which should return:

```auto
{
    "changedFieldA": "valueA",
    "changedFieldB": "valueB"
}

```

But returns:

```auto
{
    "fieldA": "valueA",
    "changedFieldB": "valueB"
}

```

It seems `JsonProperty` for constructor field was ignored, and another field name was changed. It turns out that constructor field was not ignored, but in class `com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector` getter name `getFieldA` was replaced with `fieldA` and annotation of field was ignored.

Is it intended behaviour? And field annotations are not applied to getters? Or this is a problem of Jackson (Gson) to handle such getters and annotations?

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [July 20, 2017, 10:17am UTC](https://discuss.kotlinlang.org/t/difference-in-serialization-of-data-class-fields/3802/2 "2017-07-20T10:17:11Z")

</div>

Yes, this is intended behavior. See [http://kotlinlang.org/docs/reference/annotations.html#annotation-use-site-targets](http://kotlinlang.org/docs/reference/annotations.html#annotation-use-site-targets) for documentation. Use `@get:JsonProperty` to get the behavior you expect.
