2014-01-10 44 views
1

我如何爲下面的使用@JSONView的Java代碼片段編寫Scala等效代碼。 我正在使用Scala和傑克遜的JSON。我有一個需求是在某些條件下需要動態包含或排除某些條件 - 在序列化期間。基於傑克遜的wiki,@JSONView似乎是一個不錯的選擇 - 但我沒有成功獲得相同的Scala。傑克遜的@JSONView的斯卡拉等效代碼

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; 

} 

回答

2

直接斯卡拉相當於將是這個樣子:

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 = _ 
} 

這種轉換沒有利用任何斯卡拉特定傑克遜的支持;它應該按原樣安裝或不安裝Jackson Scala module

+0

謝謝。我會試試這個。 – user1498953

+0

謝謝!我沒有想到在classOf [] – Tom

+0

Correction - Array()!周圍添加Array []! – Tom