Skip to content

Commit e15321a

Browse files
committed
Fix #3028
1 parent 28b476c commit e15321a

File tree

7 files changed

+69
-33
lines changed

7 files changed

+69
-33
lines changed

release-notes/VERSION

+2
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,7 @@ Versions: 3.x (for earlier see VERSION-2.x)
4141
to allow detection of explicitly supported types
4242
#2713: Change wording of `UnrecognizedPropertyException` to refer to "property" not "field"
4343
#2828: Add `DatabindException` as intermediate subtype of `JsonMappingException`
44+
#3028: Change `UUIDSerializer` to use `StreamWriteCapability` check instead of
45+
`JsonGenerator.canWriteBinaryNatively()`
4446
- Remove `MappingJsonFactory`
4547
- Add context parameter for `TypeSerializer` contextualization (`forProperty()`)

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

+18-12
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ public <T> T readValue(JsonParser p, JavaType type) throws JacksonException {
387387

388388
/*
389389
/**********************************************************************
390-
/* Public API, config setting accessors
390+
/* Public API, config feature accessors
391391
/**********************************************************************
392392
*/
393393

@@ -425,16 +425,6 @@ public final boolean hasSomeOfFeatures(int featureMask) {
425425
return (_featureFlags & featureMask) != 0;
426426
}
427427

428-
/**
429-
* Method for accessing the currently active parser.
430-
* May be different from the outermost parser
431-
* when content is buffered.
432-
*<p>
433-
* Use of this method is discouraged: if code has direct access
434-
* to the active parser, that should be used instead.
435-
*/
436-
public final JsonParser getParser() { return _parser; }
437-
438428
/**
439429
* Accessor for checking whether input format has specified capability
440430
* or not.
@@ -445,6 +435,22 @@ public final boolean isEnabled(StreamReadCapability cap) {
445435
return _readCapabilities.isEnabled(cap);
446436
}
447437

438+
/*
439+
/**********************************************************************
440+
/* Public API, accessor for helper objects
441+
/**********************************************************************
442+
*/
443+
444+
/**
445+
* Method for accessing the currently active parser.
446+
* May be different from the outermost parser
447+
* when content is buffered.
448+
*<p>
449+
* Use of this method is discouraged: if code has direct access
450+
* to the active parser, that should be used instead.
451+
*/
452+
public final JsonParser getParser() { return _parser; }
453+
448454
public final Object findInjectableValue(Object valueId,
449455
BeanProperty forProperty, Object beanInstance)
450456
{
@@ -543,7 +549,7 @@ public boolean hasExplicitDeserializerFor(Class<?> valueType) {
543549

544550
/*
545551
/**********************************************************************
546-
/* Public API, CoercionConfig access (2.12+)
552+
/* Public API, CoercionConfig access
547553
/**********************************************************************
548554
*/
549555

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

+25-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import com.fasterxml.jackson.core.io.CharacterEscapes;
1414
import com.fasterxml.jackson.core.tree.ArrayTreeNode;
1515
import com.fasterxml.jackson.core.tree.ObjectTreeNode;
16-
16+
import com.fasterxml.jackson.core.util.JacksonFeatureSet;
1717
import com.fasterxml.jackson.databind.cfg.ContextAttributes;
1818
import com.fasterxml.jackson.databind.cfg.GeneratorSettings;
1919
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
@@ -88,6 +88,13 @@ public abstract class SerializerProvider
8888
*/
8989
protected transient JsonGenerator _generator;
9090

91+
/**
92+
* Capabilities of the output format.
93+
*
94+
* @since 3.0
95+
*/
96+
protected JacksonFeatureSet<StreamWriteCapability> _writeCapabilities;
97+
9198
/**
9299
* View used for currently active serialization, if any.
93100
*/
@@ -415,7 +422,7 @@ public SerializerProvider setAttribute(Object key, Object value)
415422

416423
/*
417424
/**********************************************************************
418-
/* Access to general configuration
425+
/* Access to other on/off features
419426
/**********************************************************************
420427
*/
421428

@@ -438,6 +445,22 @@ public final boolean isEnabled(SerializationFeature feature) {
438445
public final boolean hasSerializationFeatures(int featureMask) {
439446
return _config.hasSerializationFeatures(featureMask);
440447
}
448+
449+
/**
450+
* Accessor for checking whether input format has specified capability
451+
* or not.
452+
*
453+
* @return True if input format has specified capability; false if not
454+
*/
455+
public final boolean isEnabled(StreamWriteCapability cap) {
456+
return _writeCapabilities.isEnabled(cap);
457+
}
458+
459+
/*
460+
/**********************************************************************
461+
/* Access to other helper objects
462+
/**********************************************************************
463+
*/
441464

442465
/**
443466
* Convenience method for accessing provider to find serialization filters used,

src/main/java/com/fasterxml/jackson/databind/ser/DefaultSerializerProvider.java

+15-4
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public JsonGenerator getGenerator() {
220220
*/
221221
public void serializeValue(JsonGenerator gen, Object value) throws JacksonException
222222
{
223-
_generator = gen;
223+
_assignGenerator(gen);
224224
if (value == null) {
225225
_serializeNull(gen);
226226
return;
@@ -254,7 +254,7 @@ public void serializeValue(JsonGenerator gen, Object value) throws JacksonExcept
254254
*/
255255
public void serializeValue(JsonGenerator gen, Object value, JavaType rootType) throws JacksonException
256256
{
257-
_generator = gen;
257+
_assignGenerator(gen);
258258
if (value == null) {
259259
_serializeNull(gen);
260260
return;
@@ -291,7 +291,7 @@ public void serializeValue(JsonGenerator gen, Object value, JavaType rootType) t
291291
public void serializeValue(JsonGenerator gen, Object value, JavaType rootType,
292292
JsonSerializer<Object> ser) throws JacksonException
293293
{
294-
_generator = gen;
294+
_assignGenerator(gen);
295295
if (value == null) {
296296
_serializeNull(gen);
297297
return;
@@ -328,7 +328,7 @@ public void serializePolymorphic(JsonGenerator gen, Object value, JavaType rootT
328328
JsonSerializer<Object> valueSer, TypeSerializer typeSer)
329329
throws JacksonException
330330
{
331-
_generator = gen;
331+
_assignGenerator(gen);
332332
if (value == null) {
333333
_serializeNull(gen);
334334
return;
@@ -422,6 +422,17 @@ public void acceptJsonFormatVisitor(JavaType javaType, JsonFormatVisitorWrapper
422422
findRootValueSerializer(javaType).acceptJsonFormatVisitor(visitor, javaType);
423423
}
424424

425+
/*
426+
/**********************************************************************
427+
/* Other helper methods
428+
/**********************************************************************
429+
*/
430+
431+
private void _assignGenerator(JsonGenerator g) {
432+
_generator = g;
433+
_writeCapabilities = g.streamWriteCapabilities();
434+
}
435+
425436
/*
426437
/**********************************************************************
427438
/* Helper classes

src/main/java/com/fasterxml/jackson/databind/ser/std/UUIDSerializer.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
import java.util.UUID;
55

66
import com.fasterxml.jackson.annotation.JsonFormat;
7+
78
import com.fasterxml.jackson.core.JacksonException;
89
import com.fasterxml.jackson.core.JsonGenerator;
10+
import com.fasterxml.jackson.core.StreamWriteCapability;
911

1012
import com.fasterxml.jackson.databind.*;
1113
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
1214
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonValueFormat;
13-
import com.fasterxml.jackson.databind.util.TokenBuffer;
1415

1516
/**
1617
* Specialized {@link JsonSerializer} to output {@link java.util.UUID}s.
@@ -73,11 +74,11 @@ public JsonSerializer<?> createContextual(SerializerProvider serializers,
7374
}
7475

7576
@Override
76-
public void serialize(UUID value, JsonGenerator gen, SerializerProvider provider)
77+
public void serialize(UUID value, JsonGenerator gen, SerializerProvider ctxt)
7778
throws JacksonException
7879
{
7980
// First: perhaps we could serialize it as raw binary data?
80-
if (_writeAsBinary(gen)) {
81+
if (_writeAsBinary(ctxt)) {
8182
gen.writeBinary(_asBytes(value));
8283
return;
8384
}
@@ -104,7 +105,7 @@ public void serialize(UUID value, JsonGenerator gen, SerializerProvider provider
104105
gen.writeString(ch, 0, 36);
105106
}
106107

107-
protected boolean _writeAsBinary(JsonGenerator g)
108+
protected boolean _writeAsBinary(SerializerProvider ctxt)
108109
{
109110
if (_asBinary != null) {
110111
return _asBinary;
@@ -113,10 +114,11 @@ protected boolean _writeAsBinary(JsonGenerator g)
113114
// technically retain binary data, we do not want to do use binary
114115
// with it, as that results in UUIDs getting converted to Base64 for
115116
// most conversions.
116-
return !(g instanceof TokenBuffer) && g.canWriteBinaryNatively();
117+
// 28-Jan-2021, tatu: [databind#3028] Use capability detection instead
118+
// return !(g instanceof TokenBuffer) && g.canWriteBinaryNatively();
119+
return ctxt.isEnabled(StreamWriteCapability.CAN_WRITE_BINARY_NATIVELY);
117120
}
118121

119-
120122
// Need to add bit of extra info, format
121123
@Override
122124
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint)

src/main/java/com/fasterxml/jackson/databind/util/TokenBuffer.java

-8
Original file line numberDiff line numberDiff line change
@@ -654,14 +654,6 @@ public int streamWriteFeatures() {
654654
/* JsonGenerator implementation: capability introspection
655655
/**********************************************************************
656656
*/
657-
658-
/**
659-
* Since we can efficiently store <code>byte[]</code>, yes.
660-
*/
661-
@Override
662-
public boolean canWriteBinaryNatively() {
663-
return true;
664-
}
665657

666658
// 20-May-2020, tatu: This may or may not be enough -- ideally access is
667659
// via `DeserializationContext`, not parser, but if latter is needed

src/test-jdk14/java/com/fasterxml/jackson/databind/failing/Java9ListsTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import java.util.List;
55

66
import com.fasterxml.jackson.databind.BaseMapTest;
7+
import com.fasterxml.jackson.databind.DefaultTyping;
78
import com.fasterxml.jackson.databind.ObjectMapper;
8-
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
99
import com.fasterxml.jackson.databind.json.JsonMapper;
1010
import com.fasterxml.jackson.databind.testutil.NoCheckSubTypeValidator;
1111

0 commit comments

Comments
 (0)