After reading the usage of scala.annotation.meta.field
https://www.scala-lang.org/api/current/scala/annotation/meta/index.html
Since I am using Xstream I am currently in need of setting the annotation @XStreamAlias("alias")
for the field to change how they will be deserialised. For simplicity, I use a type alias:
import com.thoughtworks.xstream.annotations.XStreamAlias
type xStreamAlias = XStreamAlias @field
@XStreamAlias("a")
case class A(@xStreamAlias("B") b: String)
I am using this in around 40 classes so I used a package object also for simplicity.
However I now would like to switch to Jackson and retain one single annotation that includes both Jackson and Xstream both for compatibility and simplicity. I would like to do something similar to this:
import com.fasterxml.jackson.annotation.JsonProperty
import com.thoughtworks.xstream.annotations.XStreamAlias
type alias = XStreamAlias JsonProperty @field
@alias("a")
case class A(@alias("B") b: String)
This way I hope to combine both Jackson and XStream into a single annotation, since they will be used in the same way.
My questions are:
- Does this usage look correct and maintainable?
- If you see the examples above, I am using such annotations only for fields and not for classes. Can the annotation be safely used even for classes?
I have never seen such usage of mixing 2 annotations with a type alias and to me it looks like as an improvement to maintainability and readability to centralise the behaviour of the chosen annotation.
I already saw this question: Scala Type aliases for annotations but I am not sure if there is a new way to do so and the users that answered did not seem to be sure about their solutions.