|
6 | 6 | import com.fasterxml.jackson.annotation.JacksonInject;
|
7 | 7 | import com.fasterxml.jackson.annotation.JsonCreator;
|
8 | 8 |
|
| 9 | +import com.fasterxml.jackson.annotation.JsonKey; |
| 10 | +import com.fasterxml.jackson.annotation.JsonValue; |
9 | 11 | import com.fasterxml.jackson.databind.*;
|
10 | 12 |
|
11 | 13 | import com.fasterxml.jackson.databind.cfg.HandlerInstantiator;
|
@@ -112,7 +114,12 @@ public class POJOPropertiesCollector
|
112 | 114 | protected LinkedList<AnnotatedMember> _anySetterField;
|
113 | 115 |
|
114 | 116 | /**
|
115 |
| - * Method(s) marked with 'JsonValue' annotation |
| 117 | + * Accessors (field or "getter" method annotated with {@link JsonKey} |
| 118 | + */ |
| 119 | + protected LinkedList<AnnotatedMember> _jsonKeyAccessors; |
| 120 | + |
| 121 | + /** |
| 122 | + *Accessors (field or "getter" method) annotated with {@link JsonValue} |
116 | 123 | *<p>
|
117 | 124 | * NOTE: before 2.9, was `AnnotatedMethod`; with 2.9 allows fields too
|
118 | 125 | */
|
@@ -192,6 +199,23 @@ public Map<Object, AnnotatedMember> getInjectables() {
|
192 | 199 | return _injectables;
|
193 | 200 | }
|
194 | 201 |
|
| 202 | + public AnnotatedMember getJsonKeyAccessor() { |
| 203 | + if (!_collected) { |
| 204 | + collectAll(); |
| 205 | + } |
| 206 | + // If @JsonKey defined, must have a single one |
| 207 | + if (_jsonKeyAccessors != null) { |
| 208 | + if (_jsonKeyAccessors.size() > 1) { |
| 209 | + reportProblem("Multiple 'as-key' properties defined (%s vs %s)", |
| 210 | + _jsonKeyAccessors.get(0), |
| 211 | + _jsonKeyAccessors.get(1)); |
| 212 | + } |
| 213 | + // otherwise we won't greatly care |
| 214 | + return _jsonKeyAccessors.get(0); |
| 215 | + } |
| 216 | + return null; |
| 217 | + } |
| 218 | + |
195 | 219 | /**
|
196 | 220 | * @since 2.9
|
197 | 221 | */
|
@@ -421,6 +445,13 @@ protected void _addFields(Map<String, POJOPropertyBuilder> props)
|
421 | 445 | final boolean transientAsIgnoral = _config.isEnabled(MapperFeature.PROPAGATE_TRANSIENT_MARKER);
|
422 | 446 |
|
423 | 447 | for (AnnotatedField f : _classDef.fields()) {
|
| 448 | + // @JsonKey? |
| 449 | + if (Boolean.TRUE.equals(ai.hasAsKey(_config, f))) { |
| 450 | + if (_jsonKeyAccessors == null) { |
| 451 | + _jsonKeyAccessors = new LinkedList<>(); |
| 452 | + } |
| 453 | + _jsonKeyAccessors.add(f); |
| 454 | + } |
424 | 455 | // @JsonValue?
|
425 | 456 | if (Boolean.TRUE.equals(ai.hasAsValue(f))) {
|
426 | 457 | if (_jsonValueAccessors == null) {
|
@@ -646,6 +677,14 @@ protected void _addGetterMethod(Map<String, POJOPropertyBuilder> props,
|
646 | 677 | _anyGetters.add(m);
|
647 | 678 | return;
|
648 | 679 | }
|
| 680 | + // @JsonKey? |
| 681 | + if (Boolean.TRUE.equals(ai.hasAsKey(_config, m))) { |
| 682 | + if (_jsonKeyAccessors == null) { |
| 683 | + _jsonKeyAccessors = new LinkedList<>(); |
| 684 | + } |
| 685 | + _jsonKeyAccessors.add(m); |
| 686 | + return; |
| 687 | + } |
649 | 688 | // @JsonValue?
|
650 | 689 | if (Boolean.TRUE.equals(ai.hasAsValue(m))) {
|
651 | 690 | if (_jsonValueAccessors == null) {
|
|
0 commit comments