Skip to content

Commit

Permalink
Fix #3028
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 29, 2021
1 parent 28b476c commit e15321a
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 33 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,7 @@ Versions: 3.x (for earlier see VERSION-2.x)
to allow detection of explicitly supported types
#2713: Change wording of `UnrecognizedPropertyException` to refer to "property" not "field"
#2828: Add `DatabindException` as intermediate subtype of `JsonMappingException`
#3028: Change `UUIDSerializer` to use `StreamWriteCapability` check instead of
`JsonGenerator.canWriteBinaryNatively()`
- Remove `MappingJsonFactory`
- Add context parameter for `TypeSerializer` contextualization (`forProperty()`)
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ public <T> T readValue(JsonParser p, JavaType type) throws JacksonException {

/*
/**********************************************************************
/* Public API, config setting accessors
/* Public API, config feature accessors
/**********************************************************************
*/

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

/**
* Method for accessing the currently active parser.
* May be different from the outermost parser
* when content is buffered.
*<p>
* Use of this method is discouraged: if code has direct access
* to the active parser, that should be used instead.
*/
public final JsonParser getParser() { return _parser; }

/**
* Accessor for checking whether input format has specified capability
* or not.
Expand All @@ -445,6 +435,22 @@ public final boolean isEnabled(StreamReadCapability cap) {
return _readCapabilities.isEnabled(cap);
}

/*
/**********************************************************************
/* Public API, accessor for helper objects
/**********************************************************************
*/

/**
* Method for accessing the currently active parser.
* May be different from the outermost parser
* when content is buffered.
*<p>
* Use of this method is discouraged: if code has direct access
* to the active parser, that should be used instead.
*/
public final JsonParser getParser() { return _parser; }

public final Object findInjectableValue(Object valueId,
BeanProperty forProperty, Object beanInstance)
{
Expand Down Expand Up @@ -543,7 +549,7 @@ public boolean hasExplicitDeserializerFor(Class<?> valueType) {

/*
/**********************************************************************
/* Public API, CoercionConfig access (2.12+)
/* Public API, CoercionConfig access
/**********************************************************************
*/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import com.fasterxml.jackson.core.io.CharacterEscapes;
import com.fasterxml.jackson.core.tree.ArrayTreeNode;
import com.fasterxml.jackson.core.tree.ObjectTreeNode;

import com.fasterxml.jackson.core.util.JacksonFeatureSet;
import com.fasterxml.jackson.databind.cfg.ContextAttributes;
import com.fasterxml.jackson.databind.cfg.GeneratorSettings;
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
Expand Down Expand Up @@ -88,6 +88,13 @@ public abstract class SerializerProvider
*/
protected transient JsonGenerator _generator;

/**
* Capabilities of the output format.
*
* @since 3.0
*/
protected JacksonFeatureSet<StreamWriteCapability> _writeCapabilities;

/**
* View used for currently active serialization, if any.
*/
Expand Down Expand Up @@ -415,7 +422,7 @@ public SerializerProvider setAttribute(Object key, Object value)

/*
/**********************************************************************
/* Access to general configuration
/* Access to other on/off features
/**********************************************************************
*/

Expand All @@ -438,6 +445,22 @@ public final boolean isEnabled(SerializationFeature feature) {
public final boolean hasSerializationFeatures(int featureMask) {
return _config.hasSerializationFeatures(featureMask);
}

/**
* Accessor for checking whether input format has specified capability
* or not.
*
* @return True if input format has specified capability; false if not
*/
public final boolean isEnabled(StreamWriteCapability cap) {
return _writeCapabilities.isEnabled(cap);
}

/*
/**********************************************************************
/* Access to other helper objects
/**********************************************************************
*/

/**
* Convenience method for accessing provider to find serialization filters used,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public JsonGenerator getGenerator() {
*/
public void serializeValue(JsonGenerator gen, Object value) throws JacksonException
{
_generator = gen;
_assignGenerator(gen);
if (value == null) {
_serializeNull(gen);
return;
Expand Down Expand Up @@ -254,7 +254,7 @@ public void serializeValue(JsonGenerator gen, Object value) throws JacksonExcept
*/
public void serializeValue(JsonGenerator gen, Object value, JavaType rootType) throws JacksonException
{
_generator = gen;
_assignGenerator(gen);
if (value == null) {
_serializeNull(gen);
return;
Expand Down Expand Up @@ -291,7 +291,7 @@ public void serializeValue(JsonGenerator gen, Object value, JavaType rootType) t
public void serializeValue(JsonGenerator gen, Object value, JavaType rootType,
JsonSerializer<Object> ser) throws JacksonException
{
_generator = gen;
_assignGenerator(gen);
if (value == null) {
_serializeNull(gen);
return;
Expand Down Expand Up @@ -328,7 +328,7 @@ public void serializePolymorphic(JsonGenerator gen, Object value, JavaType rootT
JsonSerializer<Object> valueSer, TypeSerializer typeSer)
throws JacksonException
{
_generator = gen;
_assignGenerator(gen);
if (value == null) {
_serializeNull(gen);
return;
Expand Down Expand Up @@ -422,6 +422,17 @@ public void acceptJsonFormatVisitor(JavaType javaType, JsonFormatVisitorWrapper
findRootValueSerializer(javaType).acceptJsonFormatVisitor(visitor, javaType);
}

/*
/**********************************************************************
/* Other helper methods
/**********************************************************************
*/

private void _assignGenerator(JsonGenerator g) {
_generator = g;
_writeCapabilities = g.streamWriteCapabilities();
}

/*
/**********************************************************************
/* Helper classes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import java.util.UUID;

import com.fasterxml.jackson.annotation.JsonFormat;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.StreamWriteCapability;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonValueFormat;
import com.fasterxml.jackson.databind.util.TokenBuffer;

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

@Override
public void serialize(UUID value, JsonGenerator gen, SerializerProvider provider)
public void serialize(UUID value, JsonGenerator gen, SerializerProvider ctxt)
throws JacksonException
{
// First: perhaps we could serialize it as raw binary data?
if (_writeAsBinary(gen)) {
if (_writeAsBinary(ctxt)) {
gen.writeBinary(_asBytes(value));
return;
}
Expand All @@ -104,7 +105,7 @@ public void serialize(UUID value, JsonGenerator gen, SerializerProvider provider
gen.writeString(ch, 0, 36);
}

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


// Need to add bit of extra info, format
@Override
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -654,14 +654,6 @@ public int streamWriteFeatures() {
/* JsonGenerator implementation: capability introspection
/**********************************************************************
*/

/**
* Since we can efficiently store <code>byte[]</code>, yes.
*/
@Override
public boolean canWriteBinaryNatively() {
return true;
}

// 20-May-2020, tatu: This may or may not be enough -- ideally access is
// via `DeserializationContext`, not parser, but if latter is needed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import java.util.List;

import com.fasterxml.jackson.databind.BaseMapTest;
import com.fasterxml.jackson.databind.DefaultTyping;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.testutil.NoCheckSubTypeValidator;

Expand Down

0 comments on commit e15321a

Please sign in to comment.