From ee34c8a6908d3a6df3327428749fc53d7e923f7d Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Fri, 29 Nov 2024 11:32:09 -0800 Subject: [PATCH] Changes wrt databind/3043 (`SerializerProvider` -> `SerializationContext`) (#686) --- .../jackson/dataformat/xml/XmlMapper.java | 6 +-- .../xml/ser/UnwrappingXmlBeanSerializer.java | 8 ++-- .../xml/ser/XmlBeanPropertyWriter.java | 12 ++--- .../dataformat/xml/ser/XmlBeanSerializer.java | 10 ++-- .../xml/ser/XmlBeanSerializerBase.java | 48 +++++++++---------- ...ider.java => XmlSerializationContext.java} | 8 ++-- .../xml/ser/XmlSerializationContexts.java | 4 +- .../xml/ser/CustomSerializerTest.java | 8 ++-- .../dataformat/xml/ser/JsonAppend578Test.java | 2 +- .../xml/ser/TestSerializationWithFilter.java | 8 ++-- .../xml/ser/TestSerializerCustom.java | 2 +- 11 files changed, 58 insertions(+), 58 deletions(-) rename src/main/java/tools/jackson/dataformat/xml/ser/{XmlSerializerProvider.java => XmlSerializationContext.java} (98%) diff --git a/src/main/java/tools/jackson/dataformat/xml/XmlMapper.java b/src/main/java/tools/jackson/dataformat/xml/XmlMapper.java index fe2c8cd44..22beff79b 100644 --- a/src/main/java/tools/jackson/dataformat/xml/XmlMapper.java +++ b/src/main/java/tools/jackson/dataformat/xml/XmlMapper.java @@ -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) { @@ -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); } @@ -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(); } diff --git a/src/main/java/tools/jackson/dataformat/xml/ser/UnwrappingXmlBeanSerializer.java b/src/main/java/tools/jackson/dataformat/xml/ser/UnwrappingXmlBeanSerializer.java index 2bc1d94d0..370a13abc 100644 --- a/src/main/java/tools/jackson/dataformat/xml/ser/UnwrappingXmlBeanSerializer.java +++ b/src/main/java/tools/jackson/dataformat/xml/ser/UnwrappingXmlBeanSerializer.java @@ -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); } } diff --git a/src/main/java/tools/jackson/dataformat/xml/ser/XmlBeanPropertyWriter.java b/src/main/java/tools/jackson/dataformat/xml/ser/XmlBeanPropertyWriter.java index 7b0efb4de..7c2437dc8 100644 --- a/src/main/java/tools/jackson/dataformat/xml/ser/XmlBeanPropertyWriter.java +++ b/src/main/java/tools/jackson/dataformat/xml/ser/XmlBeanPropertyWriter.java @@ -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); @@ -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)) { @@ -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; } } @@ -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); diff --git a/src/main/java/tools/jackson/dataformat/xml/ser/XmlBeanSerializer.java b/src/main/java/tools/jackson/dataformat/xml/ser/XmlBeanSerializer.java index ae9f0e633..962f4ef6c 100644 --- a/src/main/java/tools/jackson/dataformat/xml/ser/XmlBeanSerializer.java +++ b/src/main/java/tools/jackson/dataformat/xml/ser/XmlBeanSerializer.java @@ -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; @@ -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(); } diff --git a/src/main/java/tools/jackson/dataformat/xml/ser/XmlBeanSerializerBase.java b/src/main/java/tools/jackson/dataformat/xml/ser/XmlBeanSerializerBase.java index dc288a51a..0a01c42ed 100644 --- a/src/main/java/tools/jackson/dataformat/xml/ser/XmlBeanSerializerBase.java +++ b/src/main/java/tools/jackson/dataformat/xml/ser/XmlBeanSerializerBase.java @@ -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; @@ -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; @@ -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, @@ -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)") @@ -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; } @@ -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, @@ -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) @@ -315,12 +315,12 @@ 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!) @@ -328,17 +328,17 @@ public void serializeWithType(Object bean, JsonGenerator gen, SerializerProvider 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 { @@ -346,12 +346,12 @@ protected void _serializeObjectId(Object bean, JsonGenerator gen, SerializerProv 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); } } diff --git a/src/main/java/tools/jackson/dataformat/xml/ser/XmlSerializerProvider.java b/src/main/java/tools/jackson/dataformat/xml/ser/XmlSerializationContext.java similarity index 98% rename from src/main/java/tools/jackson/dataformat/xml/ser/XmlSerializerProvider.java rename to src/main/java/tools/jackson/dataformat/xml/ser/XmlSerializationContext.java index fb5cce52d..63fd6b0b6 100644 --- a/src/main/java/tools/jackson/dataformat/xml/ser/XmlSerializerProvider.java +++ b/src/main/java/tools/jackson/dataformat/xml/ser/XmlSerializationContext.java @@ -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) @@ -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)); diff --git a/src/main/java/tools/jackson/dataformat/xml/ser/XmlSerializationContexts.java b/src/main/java/tools/jackson/dataformat/xml/ser/XmlSerializationContexts.java index 8edf539e0..110d1c3cc 100644 --- a/src/main/java/tools/jackson/dataformat/xml/ser/XmlSerializationContexts.java +++ b/src/main/java/tools/jackson/dataformat/xml/ser/XmlSerializationContexts.java @@ -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 @@ -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); } diff --git a/src/test/java/tools/jackson/dataformat/xml/ser/CustomSerializerTest.java b/src/test/java/tools/jackson/dataformat/xml/ser/CustomSerializerTest.java index 13ca50952..86c117b5b 100644 --- a/src/test/java/tools/jackson/dataformat/xml/ser/CustomSerializerTest.java +++ b/src/test/java/tools/jackson/dataformat/xml/ser/CustomSerializerTest.java @@ -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; @@ -14,9 +14,9 @@ static class CustomSerializer extends StdScalarSerializer 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); } } diff --git a/src/test/java/tools/jackson/dataformat/xml/ser/JsonAppend578Test.java b/src/test/java/tools/jackson/dataformat/xml/ser/JsonAppend578Test.java index 8967a68fb..0cb6f6822 100644 --- a/src/test/java/tools/jackson/dataformat/xml/ser/JsonAppend578Test.java +++ b/src/test/java/tools/jackson/dataformat/xml/ser/JsonAppend578Test.java @@ -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"; } diff --git a/src/test/java/tools/jackson/dataformat/xml/ser/TestSerializationWithFilter.java b/src/test/java/tools/jackson/dataformat/xml/ser/TestSerializationWithFilter.java index 2086906bc..c852120e4 100644 --- a/src/test/java/tools/jackson/dataformat/xml/ser/TestSerializationWithFilter.java +++ b/src/test/java/tools/jackson/dataformat/xml/ser/TestSerializationWithFilter.java @@ -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; @@ -41,8 +41,8 @@ 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")) { @@ -50,7 +50,7 @@ public void serializeAsProperty(Object pojo, JsonGenerator jgen, if (a <= 0) return; } - super.serializeAsProperty(pojo, jgen, provider, writer); + super.serializeAsProperty(pojo, g, ctxt, writer); } }; FilterProvider filterProvider = new SimpleFilterProvider().addFilter("filter", filter); diff --git a/src/test/java/tools/jackson/dataformat/xml/ser/TestSerializerCustom.java b/src/test/java/tools/jackson/dataformat/xml/ser/TestSerializerCustom.java index 6d14e3633..2726e190c 100644 --- a/src/test/java/tools/jackson/dataformat/xml/ser/TestSerializerCustom.java +++ b/src/test/java/tools/jackson/dataformat/xml/ser/TestSerializerCustom.java @@ -55,7 +55,7 @@ public ItemSerializer() { } @Override - public void serialize(Item value, JsonGenerator g, SerializerProvider provider) { + public void serialize(Item value, JsonGenerator g, SerializationContext ctxt) { g.writeStartObject(); g.writePOJOProperty("obj", value.obj); g.writeStringProperty("name", value.name);