--- robots: index, follow lang: zh-tw dir: ltr breaks: true title: Jackson Annotation # 簡報的名稱 tags: Note # 簡報的標籤 --- # Jackson Annotation :::info ref: https://www.baeldung.com/jackson-annotations ::: --- ###### 使用原因 Q:com.google.gson.internal.LinkedTreeMap cannot be cast to my class [duplicate] :::info Ref: https://blog.csdn.net/github_38222176/article/details/79198886 ::: gson.fromJson 轉化欄位時因指定類別為List但 因為轉換之後的Json物件 型別皆為 LinkedTreeMap故無法順利轉換拋出exception Q1:fromJson number parse to float?? :::info Ref: https://stackoverflow.com/questions/15507997/how-to-prevent-gson-from-expressing-integers-as-floats ::: ```gherkin= List<PackageApplyInfo> packageApplyInfoList = gson.fromJson(packageConfig.getApplyDeviceDetail(), List.class); ``` 輸出時 會將數字轉成 float || double (因為比較好用緣故 & 預設為如此) 故轉成下方時: ⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩ ```gherkin= Type listType = new TypeToken<ArrayList<PackageApplyInfo>>(){}.getType(); List<PackageApplyInfo> packageApplyInfoList = gson.fromJson(packageConfig.getApplyDeviceDetail(), listType); ``` >可解 但因 PackageApplyInfo 有deviceId 故輸出時為null(承下Q2) Q2:entity annotation if null not show :::info REF: https://stackoverflow.com/questions/12505141/only-using-jsonignore-during-serialization-but-not-deserialization/12505165#12505165 https://www.baeldung.com/jackson-ignore-null-fields https://blog.csdn.net/u012373815/article/details/52266609 ::: --- ## Jackson Serialization Annotations ##### @JsonAnyGetter allows for the flexibility of using a Map field as standard properties. ##### @JsonGetter is an alternative to the @JsonProperty annotation, which marks a method as a getter method. ##### @JsonPropertyOrder to specify the order of properties on serialization. ##### @JsonRawValue can instruct Jackson to serialize a property exactly as is. ##### @JsonValue indicates a single method that the library will use to serialize the entire instance. ##### @JsonRootName to indicate the name of this potential wrapper entity: ##### @JsonSerialize indicates a custom serializer to use when marshalling the entity. ## Jackson Deserialization Annotations ##### @JsonCreator to tune the constructor/factory used in deserialization. ##### @JacksonInject indicates that a property will get its value from the injection and not from the JSON data. ##### @JsonAnySetter allows us the flexibility of using a Map as standard properties. ##### @JsonSetter allows us the flexibility of using a Map as standard properties. On deserialization, the properties from JSON will simply be added to the map. ##### @JsonDeserialize indicates the use of a custom deserializer. ##### @JsonAlias defines one or more alternative names for a property during deserialization. ## Jackson Property Inclusion Annotations ##### @JsonIgnoreProperties is a class-level annotation that marks a property or a list of properties that Jackson will ignore. ##### @JsonIgnore is used to mark a property to be ignored at the field level. 會忽略 就會一直忽略 (非要用的) :::info REF: https://www.java67.com/2019/09/3-ways-to-ignore-null-fields-in-json-java-jackson.html ::: ##### @JsonIgnoreType marks all properties of an annotated type to be ignored. ##### @JsonInclude exclude properties with empty/null/default values. ###### @JsonInclude(JsonInclude.Include.NON_NULL) 針對傳入值為NULL的項目 進而忽略 :::info REF: https://www.java67.com/2019/09/3-ways-to-ignore-null-fields-in-json-java-jackson.html https://blog.csdn.net/itguangit/article/details/78701110 ::: ##### @JsonAutoDetect can override the default semantics of which properties are visible and which are not. ## Jackson Polymorphic Type Handling Annotations @JsonTypeInfo – indicates details of what type information to include in serialization @JsonSubTypes – indicates sub-types of the annotated type @JsonTypeName – defines a logical type name to use for annotated class ## Jackson General Annotations ##### @JsonProperty to indicate the property name in JSON. ##### @JsonFormat specifies a format when serializing Date/Time values. ##### @JsonUnwrapped defines values that should be unwrapped/flattened when serialized/deserialized. ##### @JsonView indicates the View in which the property will be included for serialization/deserialization. ##### @JsonManagedReference, @JsonBackReference can handle parent/child relationships and work around loops. ##### @JsonIdentityInfo indicates that Object Identity should be used when serializing/deserializing values, like when dealing with infinite recursion types of problems, for instance. ##### @JsonFilter specifies a filter to use during serialization. ## Custom Jackson Annotation ## Jackson MixIn Annotations ## Disable Jackson Annotation