Skip to content

Commit 03aa161

Browse files
committed
Merge branch '2.19' into 2.x
2 parents 6e5fb71 + 9545bdd commit 03aa161

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2568,6 +2568,8 @@ public ObjectMapper setTimeZone(TimeZone tz) {
25682568
* construction, see {@link com.fasterxml.jackson.databind.json.JsonMapper#builder}
25692569
* (and {@link MapperBuilder#defaultAttributes}).
25702570
*
2571+
* @see ContextAttributes for details on setting default attributes.
2572+
*
25712573
* @since 2.13
25722574
*/
25732575
public ObjectMapper setDefaultAttributes(ContextAttributes attrs) {

src/main/java/com/fasterxml/jackson/databind/cfg/ContextAttributes.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
/**
66
* Helper class used for storing and accessing per-call attributes.
77
* Storage is two-layered: at higher precedence, we have actual per-call
8+
* ({@code ObjectMapper.readValue} or {@code ObjectMapper.writeValue})
89
* attributes; and at lower precedence, default attributes that may be
910
* defined for Object readers and writers.
1011
*<p>
@@ -15,11 +16,24 @@
1516
* sharing, by creating new copies instead of modifying state.
1617
* This allows sharing of default values without per-call copying, but
1718
* requires two-level lookup on access.
19+
*<p>
20+
* To set default attributes, use {@link #withSharedAttributes(Map)}
21+
* or {@link #withSharedAttribute(Object, Object)}, starting with
22+
* "empty" instance (see {@link #getEmpty()}). For example:
23+
*<pre>
24+
* ContextAttributes attrs = ContextAttributes.getEmpty()
25+
* .withSharedAttribute("foo", "bar")
26+
* .withSharedAttribute("attr2", "value2");
27+
*</pre>
1828
*
1929
* @since 2.3
2030
*/
2131
public abstract class ContextAttributes
2232
{
33+
/**
34+
* Accessor for an empty instance of {@link ContextAttributes}: usable
35+
* as-is, or as a starting point for building up a set of default attributes.
36+
*/
2337
public static ContextAttributes getEmpty() {
2438
return Impl.getEmpty();
2539
}
@@ -30,8 +44,27 @@ public static ContextAttributes getEmpty() {
3044
/**********************************************************
3145
*/
3246

47+
/**
48+
* Fluent factory method for creating a new instance with an additional
49+
* shared attribute.
50+
*
51+
* @param key Name of the attribute to add
52+
* @param value Value of the attribute to add; may be null, in which case
53+
* attribute will be removed if it already exists.
54+
*
55+
* @return New instance of {@link ContextAttributes} that has specified change.
56+
*/
3357
public abstract ContextAttributes withSharedAttribute(Object key, Object value);
3458

59+
/**
60+
* Fluent factory method for creating a new instance with specified set of
61+
* shared attributes.
62+
* Any shared attributes that already exist will be replaced
63+
*
64+
* @param attributes Map of shared attributes to add, replacing any existing ones.
65+
*
66+
* @return New instance of {@link ContextAttributes} that has specified shared attributes.
67+
*/
3568
public abstract ContextAttributes withSharedAttributes(Map<?,?> attributes);
3669

3770
public abstract ContextAttributes withoutSharedAttribute(Object key);
@@ -115,7 +148,7 @@ public ContextAttributes withSharedAttribute(Object key, Object value)
115148
Map<Object,Object> m;
116149
// need to cover one special case, since EMPTY uses Immutable map:
117150
if (this == EMPTY) {
118-
m = new HashMap<Object,Object>(8);
151+
m = new HashMap<>(8);
119152
} else {
120153
m = _copy(_shared);
121154
}

src/main/java/com/fasterxml/jackson/databind/cfg/MapperBuilder.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,8 @@ public B annotationIntrospector(AnnotationIntrospector intr) {
453453
* @param attrs Default instance to use: may not be {@code null}.
454454
*
455455
* @return This Builder instance to allow call chaining
456+
*
457+
* @seee ContextAttributes for details on constructing default attributes.
456458
*/
457459
public B defaultAttributes(ContextAttributes attrs) {
458460
_mapper.setDefaultAttributes(attrs);

0 commit comments

Comments
 (0)