buguge - Keep it simple,stupid

知识就是力量,但更重要的,是运用知识的能力why buguge?

导航

Javadoc can be as clear and readable as "@JsonUnwrapped"

🍀 🍀 The javadoc screenshot at first.

javadoc of @JsonUnwrapped



🍀 🍀 Let's take a glance at its javadoc bellow.


package: com.fasterxml.jackson.annotation

@Target({ElementType.ANNOTATION_TYPE,ElementType.FIELD,ElementType.METHOD,ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface JsonUnwrapped
extends java.lang.annotation.Annotation


Annotation used to indicate that a property should be serialized "unwrapped"; that is, if it would be serialized as JSON Object, its properties are instead included as properties of its containing Object.


For example, consider case of POJO like:

   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.

Maven: com.fasterxml.jackson.core:jackson-annotations:2.11.0 (jackson-annotations-2.11.0-sources.jar)



🍀 🍀 the end.

编写清晰易读的Javadoc是每个Java开发者应该具备的良好习惯,清晰易读的Javadoc对于代码维护和团队协作至关重要。以下是详细的好处:

  1. 提高代码可维护性:清晰的文档帮助开发者理解代码的意图和功能,减少维护成本。
  2. 促进团队协作:新成员或团队成员可以快速了解API的使用方法,减少沟通成本。
  3. 便于API使用:其他开发者在使用你的类或方法时,不需要深入阅读实现代码,只需查看Javadoc文档即可。
  4. 集成开发环境(IDE)支持:在IDE中编写代码时,良好的Javadoc会以提示形式出现,帮助开发者正确使用方法。
  5. 自动化生成文档:Javadoc可以自动生成HTML格式的文档,方便在线浏览和搜索。

posted on 2025-07-18 14:35  buguge  阅读(7)  评论(0)    收藏  举报