Skip to content

DataContractJsonSerializer not passing ISerializationSurrogateProvider to internal xml serializer #100553

Open
@lucienmaloney

Description

@lucienmaloney

Description

If you have a DataContractJsonSerializer object that you call SetSerializationSurrogateProvider on to provide an ISerializationSurrogateProvider instance, that instance doesn't seem to be passed down to the internal serializer class used by DataContractJsonSerializer.

For example, calling DataContractJsonSerializer.ReadObject eventually hits DataContractJsonSerializer.InternalReadObject, which calls XmlObjectSerializerReadContextComplexJson.CreateContext. That calls the XmlObjectSerializerReadContextComplexJson(DataContractJsonSerializer serializer, DataContract rootTypeDataContract) constructor, which calls the XmlObjectSerializerReadContextComplex(XmlObjectSerializer serializer, int maxItemsInObjectGraph, StreamingContext streamingContext, bool ignoreExtensionDataObject) constructor.

This is where it seems to go wrong, since this constructor doesn't set _serializationSurrogateProvider - only the XmlObjectSerializerReadContextComplex(DataContractSerializer serializer, DataContract rootTypeDataContract, DataContractResolver? dataContractResolver) constructor can do that. So _serializationSurrogateProvider just stays null on that inner serializer, and doesn't come into play.

Reproduction Steps

I can include a simplified version of the code I'm using as an example snippet, if requested. However, I think any attempt to use SetSerializationSurrogateProvider on DataContractJsonSerializer will have the same problem. (If not, and there's a workaround that allows it to work, that'd be good.)

Expected behavior

That the surrogate would be used in deserialization/serialization.

Actual behavior

The surrogate is null internally, so in our deserialization use case, we get an incorrectly initialized instance, or just an exception.

Regression?

No response

Known Workarounds

No response

Configuration

.NET 8

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions