Feature: JSON Views

Idea

思路

"Views" in this context mean ability to define subsets of logical properties (things accessed via getters or fields) to serialize. Views are defined statically (using annotations), but view to use for serialization is chosen dynamically (per serialization). 

“Views”,在这个语境中,意思是为将要序列化的逻辑属性(通过get方法或者字段获取到的)定义一个子集。views是通过注解(annotations)来静态定义的,但是使用的时候是动态选择的(每个需要序列化的地方自己选择使用哪些views)


Design

设计

  • Views themselves are identified by using Classes, instead of Strings (or dedicated objects). Reasons:

  • views是使用类来定义的,而不是使用字符串,或者其它某些特定的实例。原因是:

    • With classes, can use inheritance to naturally (?) represent View hierarchies (if a field is part of a View, it'll be also part of parent view)

    • 使用类,就可以使用继承来更加自然的展示views的结构(如果一个字段是某个view的一部分,那么它也是父级view的一部分)

    • Classes can be used as annotation values: Enums not (Enums would have been one other obvious possibility)

    • 类可以用作注解中的值,而枚举不可以(枚举也是另一个可能用作view的设计)

    • For future extensibility, classes can also be annotated if need be (no plans for such annotations yet)

    • 考虑未来的扩展性,如果有必要的话,(用作vies定义的)类本身也可以被打上注解(不过不在现有标签的计划中)

  • View membership defined using annotations: specify which view(s)property will be included in. If no view annotation, assumed to mean View identified by Object.class: that is, included in all views

  • 我们是这样定义视图注解的使用方式的:为某个属性标记上某个注解,标示这个属性将被包含在(最终的json字符串之)内。如果某个属性上没有任何注解,则认为它被标记为“Object.class"的视图,并会被包含在所有视图中。

  • View to use for serialization (and with 2.0, deserialization) is specified dynamically; active view is a property of SerializationConfig (and DeserializationConfig). Conceptually defaults to Object.class; as if no View functionality was used at all.

  • 在序列化为json的过程中(以及,在2.0中,还包括反序列化的过程),view是动态指定的。一个view是一个SerializationConfig (对反序列化来说则是DeserializationConfig)的一个属性。这个属性的默认值是Object.class,其功能和不使用view是一样的。

  • Only single active view per serialization; but due to inheritance of Views, can combine Views via aggregation.

  • 每次序列化都只能使用一个视图;但是,考虑到视图的继承层次,可以把多个视图组合成一个。

  • All view membership inclusive, no need for exclusions? (however: could add an option to change default handling of 'unmarked' properties to mean "don't include unless specifically identified)

  • view的用法都是“包含在序列化结果”中的,没有“排除在序列化结果之外”的语义。(不过,可以增加一个选项来改变对“unmarked”属性的默认处理方式——把它改成“除非特别声明,否则不把被标记属性包含在序列化结果中”)


Implementation

实现

1.4 implementation is used as follows.

1.4版的实现是这样使用的。

First, defining views means declaring classes; you can reuse existing ones, or just create bogus classes -- they are just view identifiers with relationship information (child inherits view membership from parents):

首先,通过声明一些类,来定义一堆view。你可以重用现有的类或view,或者只创建一些“虚假的类”——所谓“虚假”是指,这些类只用来标识view以及view的依赖和关联(例如,子view会继承父view的信息)。以下是代码:

  // View definitions:
  class Views {
            static class Public { }
            static class ExtendedPublic extends PublicView { }
            static class Internal extends ExtendedPublicView { }
  }
          
  public class Bean {
            // Name is public
            @JsonView(Views.Public.class) String name;
            // Address semi-public
            @JsonView(Views.ExtendPublic.class) Address address;
            // SSN only for internal usage
            @JsonView(Views.Internal.class) SocialSecNumber ssn;
  }

With such view definitions, serialization would be done like so: 

定义好这些view之后,序列化过程会这样执行:

  // short-cut:
  objectMapper.writeValueUsingView(out, beanInstance, ViewsPublic.class);

  // or fully exploded:
  objectMapper.getSerializationConfig().setSerializationView(Views.Public.class);
  // (note: can also pre-construct config object with 'mapper.copySerializationConfig'; reuse)
  objectMapper.writeValue(out, beanInstance); // will use active view set via Config

  // or, starting with 1.5, more convenient (ObjectWriter is reusable too)
  objectMapper.viewWriter(ViewsPublic.class).writeValue(out, beanInstance);

and result would only contain 'name', not 'address' or 'ssn'. 

并且,最终结果会仅仅包含“name”,而没有“address”或“ssn”。

NOTE: even if you only want to use "default" view -- that is, just exclude things that are only to be included in specific "full" view -- you DO need to enable View processing by specifying a view. If you do not have explicit "basic" view setting, just use Object.class. 

注意:即使你只想使用“默认”view——也就是说,


Handling of "view-less" properties

By default all properties without explicit view definition are included in serialization. But starting with Jackson 1.5 you can change this default by:

  objectMapper.configure(SerializationConfig.Feature.DEFAULT_VIEW_INCLUSION, false);

where false means that such properties are NOT included when enabling with a view. Default for this property is 'true'.


Customization

Although default implementation is not very customizable, underlying design does allow for implementing elaborate custom filtering, using alternative means of defining custom views. Here is how.


Enabling "view" processing

Depending on exactly how custom serialization is implemented, you may (or may not) need to enable view processing. If it is needed, you will just do something like:

  ObjectWriter w = objectMapper.viewWriter(SomeClass.class); // 1.8 and prior
  ObjectWriter w = objectMapper.writerWithView(SomeClass.class); // 1.9 and above

which offers same set of writeValue (and writeValueAsString, writeValueAsBytes, ...) write methods and can be reused easily (or passed).

As of 2.0 this is also available for deserialization, like so:

  ObjectReader r = objectMapper.readerWithView(SomeClass.class); // 2.0 and above


Views with JAX-RS

With 2.3 (of JAX-RS module) it is also possible to annotate JAX-RS resources like so:

    public class Resource {

      @JsonView(Views.Public.class)
      @GET
      @Produces(MediaType.APPLICATION_JSON )
      public List<Object> getElements() {
        ...
        return someResultList;
      }
    }

so that you need not try to configure active view via ObjectReader / ObjectWriter.




本文转自 斯然在天边 51CTO博客,原文链接:http://blog.51cto.com/winters1224/1668947,如需转载请自行联系原作者

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
form: { sample_id: '', feature0: '', feature1: '', feature2: '', feature3: '', feature4: '', feature5: '', feature6: '', feature7: '', feature8: '', feature9: '', feature10: '', feature11: '', feature12: '', feature13: '', feature14: '', feature15: '', feature16: '', feature17: '', feature18: '', feature19: '', feature20: '', feature21: '', feature22: '', feature23: '', feature24: '', feature25: '', feature26: '', feature27: '', feature28: '', feature29: '', feature30: '', feature31: '', feature32: '', feature33: '', feature34: '', feature35: '', feature36: '', feature37: '', feature38: '', feature39: '', feature40: '', feature41: '', feature42: '', feature43: '', feature44: '', feature45: '', feature46: '', feature47: '', feature48: '', feature49: '', feature50: '', feature51: '', feature52: '', feature53: '', feature54: '', feature55: '', feature56: '', feature57: '', feature58: '', feature59: '', feature60: '', feature61: '', feature62: '', feature63: '', feature64: '', feature65: '', feature66: '', feature67: '', feature68: '', feature69: '', feature70: '', feature71: '', feature72: '', feature73: '', feature74: '', feature75: '', feature76: '', feature77: '', feature78: '', feature79: '', feature80: '', feature81: '', feature82: '', feature83: '', feature84: '', feature85: '', feature86: '', feature87: '', feature88: '', feature89: '', feature90: '', feature91: '', feature92: '', feature93: '', feature94: '', feature95: '', feature96: '', feature97: '', feature98: '', feature99: '', feature100: '', feature101: '', feature102: '', feature103: '', feature104: '', feature105: '', feature106: '' },利用vue实现根据sample_id进行查询
07-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值