jackson 序列化成扁平化

@JsonUnwrapped注解,注解对应的字段对象上边.

序列化之后,json的层级就只有一层. 

做个必须  类似于复制文件夹,扁平化之后,就只复制文件,在目标地方不在创建子文件夹.

原json

{"name":"guofeipeng","password":"123","p":{"age":25,"sex":true,"birthday":"2014-12-22 07-15-29","word":"程序员","pname":"nomouse","salary":"2500.00"}}
扁平化json

{"name":"guofeipeng","password":"123","age":25,"sex":true,"birthday":"2014-12-22 07-16-38","word":"程序         员","pname":"nomouse","salary":"2500.00"}
其中的
prefix
suffix
还可以指定生成的key的前缀和后缀.

用处多为转实体类为出参.
官方注释:

 
   public class Parent {
     public int age;
     public Name name;
   }
   public class Name {
     public String first, last;
   }
 
which would normally be serialized as follows (assuming @JsonUnwrapped had no effect):
   {
     "age" : 18,
     "name" : {
       "first" : "Joey",
       "last" : "Sixpack"
     }
   }
 
can be changed to this:
   {
     "age" : 18,
     "first" : "Joey",
     "last" : "Sixpack"
   }
 
by changing Parent class to:
   public class Parent {
     public int age;
     @JsonUnwrapped
     public Name name;
   }
 
Annotation can only be added to properties, and not classes, as it is contextual.

Also note that annotation only applies if

  • Value is serialized as JSON Object (can not unwrap JSON arrays using this mechanism)
  • Serialization is done using BeanSerializer, not a custom serializer
  • No type information is added; if type information needs to be added, structure can not be altered regardless of inclusion strategy; so annotation is basically ignored.

你可能感兴趣的:(笔记记录)