Skip to content

Commit 90dc316

Browse files
authored
Adds @JsonKey annotation. (#179)
Add `@JsonKey` annotation. This annotation will be used like `@JsonValue` but only when serializing Map keys. See jackson-databind/#2871
1 parent abf6fb4 commit 90dc316

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.fasterxml.jackson.annotation;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* Marker annotation
10+
* that indicates that the value of annotated accessor (either field
11+
* or "getter" method [a method with non-void return type, no args])
12+
* is to be used as the single value to serialize for the instance.
13+
* This value will be used only if the instance is being serialized
14+
* as a key in a Map type.
15+
* Usually value will be of a simple scalar type
16+
* (String or Number), but it can be any serializable type (Collection,
17+
* Map or Bean).
18+
*<p>
19+
* At most one accessor of a <code>Class</code> can be annotated with this annotation;
20+
* if more than one is found, an exception may be thrown.
21+
* Also, if method signature of annotated method is not compatible with Getters,
22+
* an exception may be thrown (whether exception is thrown or not is an
23+
* implementation detail (due to filtering during introspection, some annotations
24+
* may be skipped) and applications should not rely on specific behavior).
25+
*<p>
26+
* A typical usage is that of annotating <code>toString()</code>
27+
* method so that returned String value is used as the JSON serialization;
28+
* and if deserialization is needed, there is matching constructor
29+
* or factory method annotated with {@link JsonCreator} annotation.
30+
*<p>
31+
* Boolean argument is only used so that sub-classes can "disable"
32+
* annotation if necessary.
33+
*
34+
* @see JsonValue
35+
*/
36+
@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD,
37+
ElementType.FIELD
38+
})
39+
@Retention(RetentionPolicy.RUNTIME)
40+
@JacksonAnnotation
41+
public @interface JsonKey
42+
{
43+
/**
44+
* Optional argument that defines whether this annotation is active
45+
* or not. The only use for value 'false' if for overriding purposes.
46+
* Overriding may be necessary when used
47+
* with "mix-in annotations" (aka "annotation overrides").
48+
* For most cases, however, default value of "true" is just fine
49+
* and should be omitted.
50+
*/
51+
boolean value() default true;
52+
}

src/main/java/com/fasterxml/jackson/annotation/JsonValue.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,13 @@
3636
* This is possible since set of Enum values is constant and it is possible
3737
* to define mapping, but can not be done in general for POJO types; as such,
3838
* this is not used for POJO deserialization.
39+
*<p>
40+
* NOTE: When the instance is being serialized as the key of a Map type,
41+
* this will be ignored if an accessor is annotated with
42+
* {@link JsonKey}.
3943
*
4044
* @see JsonCreator
45+
* @see JsonKey
4146
*/
4247
@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD,
4348
ElementType.FIELD // since 2.9

0 commit comments

Comments
 (0)