Reputation: 45
How could I write the Scala equivalent code for the below Java snippet that uses @JSONView. I'm using Scala and Jackson's JSON. I have a requirement were in certain fields need to be dynamically included or excluded based on certain condition - during serialization. Based on Jackson's wiki, @JSONView seems to be a good option - but I have not been successful in getting the scala equivalent.
public class Employee {
public static class All { }
public static class View1 extends All { }
public static class View2 extends View1 { }
public static class View3 extends All { }
@JsonView(All.class)
public Long empid;
@JsonView(View1.class)
public String name;
@JsonView({View2.class, View3.class})
public String addr;
}
Upvotes: 2
Views: 354
Reputation: 3065
The direct Scala equivalent would look something like this:
object Employee
{
class All
class View1 extends All
class View2 extends View1
class View3 extends All
}
class Employee
{
import Employee._
@JsonView(Array(classOf[All]))
var empid: Long = _
@JsonView(Array(classOf[View1]))
var name: String = _
@JsonView(Array(classOf[View2], classOf[View3]))
var addr: String = _
}
This conversion doesn't take advantage of any Scala-specific Jackson support; it should work as-is with or without the Jackson Scala module installed.
Upvotes: 3