Skip to content

Commit

Permalink
Changes wrt databind/3043 (SerializerProvider -> `SerializationCont…
Browse files Browse the repository at this point in the history
…ext`) (#686)
  • Loading branch information
cowtowncoder authored Nov 29, 2024
1 parent f24411f commit ee34c8a
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 58 deletions.
6 changes: 3 additions & 3 deletions src/main/java/tools/jackson/dataformat/xml/XmlMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ public XmlMapper(XmlFactory xmlFactory)
/*
// Need to override serializer provider (due to root name handling);
// deserializer provider fine as is
super(xmlFactory, new XmlSerializerProvider(xmlFactory, new XmlRootNameLookup()), null);
super(xmlFactory, new XmlSerializationContext(xmlFactory, new XmlRootNameLookup()), null);
_xmlModule = module;
// but all the rest is done via Module interface!
if (module != null) {
Expand Down Expand Up @@ -441,7 +441,7 @@ public FromXmlParser createParser(XMLStreamReader r) throws IOException {
* for given Stax {@link XMLStreamWriter}.
*/
public ToXmlGenerator createGenerator(XMLStreamWriter w) throws IOException {
SerializationContextExt prov = _serializerProvider(serializationConfig());
SerializationContextExt prov = _serializationContext(serializationConfig());
return tokenStreamFactory().createGenerator(prov, w);
}

Expand Down Expand Up @@ -491,7 +491,7 @@ public void writeValue(XMLStreamWriter w, Object value) throws IOException
if (config.isEnabled(SerializationFeature.CLOSE_CLOSEABLE) && (value instanceof Closeable)) {
_writeCloseableValue(g, value, config);
} else {
_serializerProvider(config).serializeValue(g, value);
_serializationContext(config).serializeValue(g, value);
if (config.isEnabled(SerializationFeature.FLUSH_AFTER_WRITE_VALUE)) {
g.flush();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,17 @@ protected BeanSerializerBase asArraySerializer() {
* {@link BeanPropertyWriter} instances.
*/
@Override
public final void serialize(Object bean, JsonGenerator jgen, SerializerProvider provider)
public final void serialize(Object bean, JsonGenerator g, SerializationContext ctxt)
throws JacksonException
{
if (_objectIdWriter != null) {
_serializeWithObjectId(bean, jgen, provider, false);
_serializeWithObjectId(bean, g, ctxt, false);
return;
}
if (_propertyFilterId != null) {
_serializePropertiesFiltered(bean, jgen, provider, _propertyFilterId);
_serializePropertiesFiltered(bean, g, ctxt, _propertyFilterId);
} else {
_serializeProperties(bean, jgen, provider);
_serializeProperties(bean, g, ctxt);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private QName _qname(PropertyName n)
* and as necessary.
*/
@Override
public void serializeAsProperty(Object bean, JsonGenerator g, SerializerProvider prov)
public void serializeAsProperty(Object bean, JsonGenerator g, SerializationContext ctxt)
throws Exception
{
Object value = get(bean);
Expand Down Expand Up @@ -113,13 +113,13 @@ public void serializeAsProperty(Object bean, JsonGenerator g, SerializerProvider
PropertySerializerMap map = _dynamicSerializers;
ser = map.serializerFor(cls);
if (ser == null) {
ser = _findAndAddDynamic(map, cls, prov);
ser = _findAndAddDynamic(map, cls, ctxt);
}
}
// and then see if we must suppress certain values (default, empty)
if (_suppressableValue != null) {
if (MARKER_FOR_EMPTY == _suppressableValue) {
if (ser.isEmpty(prov, value)) {
if (ser.isEmpty(ctxt, value)) {
return;
}
} else if (_suppressableValue.equals(value)) {
Expand All @@ -129,7 +129,7 @@ public void serializeAsProperty(Object bean, JsonGenerator g, SerializerProvider
// For non-nulls: simple check for direct cycles
if (value == bean) {
// NOTE: method signature here change 2.3->2.4
if (_handleSelfReference(bean, g, prov, ser)) {
if (_handleSelfReference(bean, g, ctxt, ser)) {
return;
}
}
Expand All @@ -142,9 +142,9 @@ public void serializeAsProperty(Object bean, JsonGenerator g, SerializerProvider
}
g.writeName(_name);
if (_typeSerializer == null) {
ser.serialize(value, g, prov);
ser.serialize(value, g, ctxt);
} else {
ser.serializeWithType(value, g, prov, _typeSerializer);
ser.serializeWithType(value, g, ctxt, _typeSerializer);
}
if (xmlGen != null) {
xmlGen.finishWrappedValue(_wrapperQName, _wrappedQName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import tools.jackson.core.JsonGenerator;

import tools.jackson.databind.ValueSerializer;
import tools.jackson.databind.SerializerProvider;
import tools.jackson.databind.SerializationContext;
import tools.jackson.databind.ser.BeanPropertyWriter;
import tools.jackson.databind.ser.BeanSerializer;
import tools.jackson.databind.ser.bean.BeanAsArraySerializer;
Expand Down Expand Up @@ -116,18 +116,18 @@ protected BeanSerializerBase asArraySerializer()
* {@link BeanPropertyWriter} instances.
*/
@Override
public void serialize(Object bean, JsonGenerator g, SerializerProvider provider)
public void serialize(Object bean, JsonGenerator g, SerializationContext ctxt)
throws JacksonException
{
if (_objectIdWriter != null) {
_serializeWithObjectId(bean, g, provider, true);
_serializeWithObjectId(bean, g, ctxt, true);
return;
}
g.writeStartObject();
if (_propertyFilterId != null) {
_serializePropertiesFiltered(bean, g, provider, _propertyFilterId);
_serializePropertiesFiltered(bean, g, ctxt, _propertyFilterId);
} else {
_serializeProperties(bean, g, provider);
_serializeProperties(bean, g, ctxt);
}
g.writeEndObject();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import tools.jackson.core.*;

import tools.jackson.databind.DatabindException;
import tools.jackson.databind.SerializerProvider;
import tools.jackson.databind.SerializationContext;
import tools.jackson.databind.jsontype.TypeSerializer;
import tools.jackson.databind.ser.BeanPropertyWriter;
import tools.jackson.databind.ser.PropertyFilter;
Expand Down Expand Up @@ -163,17 +163,17 @@ protected XmlBeanSerializerBase(XmlBeanSerializerBase src,
* elements.
*/
@Override
protected void _serializeProperties(Object bean, JsonGenerator gen0, SerializerProvider provider)
protected void _serializeProperties(Object bean, JsonGenerator gen0, SerializationContext ctxt)
throws JacksonException
{
// 19-Aug-2013, tatu: During 'convertValue()', need to skip
if (!(gen0 instanceof ToXmlGenerator)) {
super._serializeProperties(bean, gen0, provider);
super._serializeProperties(bean, gen0, ctxt);
return;
}
final ToXmlGenerator xgen = (ToXmlGenerator) gen0;
final BeanPropertyWriter[] props;
if (_filteredProps != null && provider.getActiveView() != null) {
if (_filteredProps != null && ctxt.getActiveView() != null) {
props = _filteredProps;
} else {
props = _props;
Expand Down Expand Up @@ -205,10 +205,10 @@ protected void _serializeProperties(Object bean, JsonGenerator gen0, SerializerP
if (prop != null) { // can have nulls in filtered list
if ((cdata != null) && cdata.get(i)) {
xgen.setNextIsCData(true);
prop.serializeAsProperty(bean, xgen, provider);
prop.serializeAsProperty(bean, xgen, ctxt);
xgen.setNextIsCData(false);
} else {
prop.serializeAsProperty(bean, xgen, provider);
prop.serializeAsProperty(bean, xgen, ctxt);
}
}
// Reset to avoid next value being written as unwrapped,
Expand All @@ -221,11 +221,11 @@ protected void _serializeProperties(Object bean, JsonGenerator gen0, SerializerP
// For [#117]: not a clean fix, but with @JsonTypeInfo, we'll end up
// with accidental attributes otherwise
xgen.setNextIsAttribute(false);
_anyGetterWriter.getAndSerialize(bean, xgen, provider);
_anyGetterWriter.getAndSerialize(bean, xgen, ctxt);
}
} catch (Exception e) {
String name = (i == props.length) ? "[anySetter]" : props[i].getName();
wrapAndThrow(provider, e, bean, name);
wrapAndThrow(ctxt, e, bean, name);
} catch (StackOverflowError e) { // Bit tricky, can't do more calls as stack is full; so:
final String name = (i == props.length) ? "[anySetter]" : props[i].getName();
throw DatabindException.from(gen0, "Infinite recursion (StackOverflowError)")
Expand All @@ -235,27 +235,27 @@ protected void _serializeProperties(Object bean, JsonGenerator gen0, SerializerP

@Override
protected void _serializePropertiesFiltered(Object bean, JsonGenerator gen0,
SerializerProvider provider, Object filterId)
SerializationContext ctxt, Object filterId)
throws JacksonException
{
// 19-Aug-2013, tatu: During 'convertValue()', need to skip
if (!(gen0 instanceof ToXmlGenerator)) {
super._serializePropertiesFiltered(bean, gen0, provider, filterId);
super._serializePropertiesFiltered(bean, gen0, ctxt, filterId);
return;
}

final ToXmlGenerator xgen = (ToXmlGenerator) gen0;

final BeanPropertyWriter[] props;
if (_filteredProps != null && provider.getActiveView() != null) {
if (_filteredProps != null && ctxt.getActiveView() != null) {
props = _filteredProps;
} else {
props = _props;
}
final PropertyFilter filter = findPropertyFilter(provider, _propertyFilterId, bean);
final PropertyFilter filter = findPropertyFilter(ctxt, _propertyFilterId, bean);
// better also allow missing filter actually..
if (filter == null) {
_serializeProperties(bean, gen0, provider);
_serializeProperties(bean, gen0, ctxt);
return;
}

Expand Down Expand Up @@ -285,10 +285,10 @@ protected void _serializePropertiesFiltered(Object bean, JsonGenerator gen0,
if (prop != null) { // can have nulls in filtered list
if ((cdata != null) && cdata.get(i)) {
xgen.setNextIsCData(true);
filter.serializeAsProperty(bean, xgen, provider, prop);
filter.serializeAsProperty(bean, xgen, ctxt, prop);
xgen.setNextIsCData(false);
} else {
filter.serializeAsProperty(bean, xgen, provider, prop);
filter.serializeAsProperty(bean, xgen, ctxt, prop);
}
}
// Reset to avoid next value being written as unwrapped,
Expand All @@ -302,11 +302,11 @@ protected void _serializePropertiesFiltered(Object bean, JsonGenerator gen0,
// with accidental attributes otherwise
xgen.setNextIsAttribute(false);
// 24-Jul-2019, tatu: Fixed for [dataformat-xml#351]
_anyGetterWriter.getAndFilter(bean, xgen, provider, filter);
_anyGetterWriter.getAndFilter(bean, xgen, ctxt, filter);
}
} catch (Exception e) {
String name = (i == props.length) ? "[anySetter]" : props[i].getName();
wrapAndThrow(provider, e, bean, name);
wrapAndThrow(ctxt, e, bean, name);
} catch (StackOverflowError e) {
final String name = (i == props.length) ? "[anySetter]" : props[i].getName();
throw DatabindException.from(gen0, "Infinite recursion (StackOverflowError)", e)
Expand All @@ -315,43 +315,43 @@ protected void _serializePropertiesFiltered(Object bean, JsonGenerator gen0,
}

@Override
public void serializeWithType(Object bean, JsonGenerator gen, SerializerProvider provider,
public void serializeWithType(Object bean, JsonGenerator gen, SerializationContext ctxt,
TypeSerializer typeSer)
throws JacksonException
{
if (_objectIdWriter != null) {
_serializeWithObjectId(bean, gen, provider, typeSer);
_serializeWithObjectId(bean, gen, ctxt, typeSer);
return;
}
// Ok: let's serialize type id as attribute, but if (and only if!)
// we are using AS_PROPERTY
if (typeSer.getTypeInclusion() == JsonTypeInfo.As.PROPERTY) {
ToXmlGenerator xgen = (ToXmlGenerator)gen;
xgen.setNextIsAttribute(true);
super.serializeWithType(bean, gen, provider, typeSer);
super.serializeWithType(bean, gen, ctxt, typeSer);
if (_attributeCount == 0) { // if no attributes, need to reset
xgen.setNextIsAttribute(false);
}
} else {
super.serializeWithType(bean, gen, provider, typeSer);
super.serializeWithType(bean, gen, ctxt, typeSer);
}
}

@Override
protected void _serializeObjectId(Object bean, JsonGenerator gen, SerializerProvider provider,
protected void _serializeObjectId(Object bean, JsonGenerator gen, SerializationContext ctxt,
TypeSerializer typeSer, WritableObjectId objectId)
throws JacksonException
{
// Ok: let's serialize type id as attribute, but if (and only if!) we are using AS_PROPERTY
if (typeSer.getTypeInclusion() == JsonTypeInfo.As.PROPERTY) {
ToXmlGenerator xgen = (ToXmlGenerator)gen;
xgen.setNextIsAttribute(true);
super._serializeObjectId(bean, gen, provider, typeSer, objectId);
super._serializeObjectId(bean, gen, ctxt, typeSer, objectId);
if (_attributeCount == 0) { // if no attributes, need to reset
xgen.setNextIsAttribute(false);
}
} else {
super._serializeObjectId(bean, gen, provider, typeSer, objectId);
super._serializeObjectId(bean, gen, ctxt, typeSer, objectId);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@

/**
* We need to override some parts of
* {@link tools.jackson.databind.SerializerProvider}
* {@link tools.jackson.databind.SerializationContext}
* implementation to handle oddities of XML output, like "extra" root element.
*/
public class XmlSerializerProvider extends SerializationContextExt
public class XmlSerializationContext extends SerializationContextExt
{
protected final XmlRootNameLookup _rootNameLookup;

public XmlSerializerProvider(TokenStreamFactory streamFactory,
public XmlSerializationContext(TokenStreamFactory streamFactory,
SerializationConfig config, GeneratorSettings genSettings,
SerializerFactory f, SerializerCache cache,
XmlRootNameLookup rootLookup)
Expand Down Expand Up @@ -182,7 +182,7 @@ public void serializePolymorphic(JsonGenerator gen, Object value, JavaType rootT
_startRootArray(xgen, rootName);
}
}
// 21-May-2020: See comments in `jackson-databind/DefaultSerializerProvider`
// 21-May-2020: See comments in `jackson-databind/DefaultSerializationContext`
if (valueSer == null) {
if ((rootType != null) && rootType.isContainerType()) {
valueSer = handleRootContextualization(findValueSerializer(rootType));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import tools.jackson.dataformat.xml.util.XmlRootNameLookup;

/**
* Factory implementation we need to provide custom {@link SerializerProvider}
* Factory implementation we need to provide custom {@link SerializationContext}
* variants.
*
* @since 3.0
Expand Down Expand Up @@ -49,7 +49,7 @@ public SerializationContexts forMapper(Object mapper,
@Override
public SerializationContextExt createContext(SerializationConfig config,
GeneratorSettings genSettings) {
return new XmlSerializerProvider(_streamFactory,
return new XmlSerializationContext(_streamFactory,
config, genSettings, _serializerFactory, _cache,
_rootNameLookup);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package tools.jackson.dataformat.xml.ser;

import tools.jackson.core.JsonGenerator;
import tools.jackson.databind.SerializerProvider;
import tools.jackson.databind.SerializationContext;
import tools.jackson.databind.module.SimpleModule;
import tools.jackson.databind.ser.std.StdScalarSerializer;
import tools.jackson.dataformat.xml.XmlMapper;
Expand All @@ -14,9 +14,9 @@ static class CustomSerializer extends StdScalarSerializer<String>
public CustomSerializer() { super(String.class); }

@Override
public void serialize(String value, JsonGenerator jgen,
SerializerProvider provider) {
jgen.writeString("custom:"+value);
public void serialize(String value, JsonGenerator g,
SerializationContext provider) {
g.writeString("custom:"+value);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected MyVirtualPropertyWriter(BeanPropertyDefinition propDef, Annotations co
}

@Override
protected Object value(Object bean, JsonGenerator g, SerializerProvider prov) {
protected Object value(Object bean, JsonGenerator g, SerializationContext ctxt) {
return "bar";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import tools.jackson.core.JsonGenerator;

import tools.jackson.databind.SerializerProvider;
import tools.jackson.databind.SerializationContext;
import tools.jackson.databind.ser.FilterProvider;
import tools.jackson.databind.ser.PropertyFilter;
import tools.jackson.databind.ser.PropertyWriter;
Expand Down Expand Up @@ -41,16 +41,16 @@ public void testPullRequest616() throws Exception

PropertyFilter filter = new SimpleBeanPropertyFilter() {
@Override
public void serializeAsProperty(Object pojo, JsonGenerator jgen,
SerializerProvider provider, PropertyWriter writer)
public void serializeAsProperty(Object pojo, JsonGenerator g,
SerializationContext ctxt, PropertyWriter writer)
throws Exception
{
if (include(writer) && writer.getName().equals("a")) {
int a = ((Item) pojo).a;
if (a <= 0)
return;
}
super.serializeAsProperty(pojo, jgen, provider, writer);
super.serializeAsProperty(pojo, g, ctxt, writer);
}
};
FilterProvider filterProvider = new SimpleFilterProvider().addFilter("filter", filter);
Expand Down
Loading

0 comments on commit ee34c8a

Please sign in to comment.