Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacing Newtonsoft with System.Text.Json #1257

Open
rogierv opened this issue Aug 7, 2023 · 5 comments
Open

Replacing Newtonsoft with System.Text.Json #1257

rogierv opened this issue Aug 7, 2023 · 5 comments
Labels

Comments

@rogierv
Copy link

rogierv commented Aug 7, 2023

Describe the feature.

Are there plans to replace Newtonsoft with System.Text.Json in NServiceBus.Persistence.Sql? Currently, we are implementing NServiceBus 8.1, in which we can use SystemJsonSerializer. This allows us to use the handy JsonDerivedTypes attribute on base classes. It would be great to use them in Sagas as well.

Additional Context

No response

@rogierv rogierv added the Feature label Aug 7, 2023
@DavidBoike
Copy link
Member

Hi @rogierv,

Unfortunately it will not be straightforward to replace Newtonsoft with System.Text.Json because users will have stored sagas encoded with Newtonsoft that must still be readable even if new sagas are encoded with System.Text.Json. (Though the fact that SQL Persistence stores the version along with each saga will help.)

But if all you're after is serialization of polymorphic types in your saga data, Newtonsoft can already handle that.

Add this to your endpoint configuration:

var persistence = endpointConfiguration.UsePersistence<SqlPersistence>();
var sagaSettings = persistence.SagaSettings();
sagaSettings.JsonSettings(new JsonSerializerSettings
{
   TypeNameHandling = TypeNameHandling.Auto 
});

Then if the actual type of the object differs from the declared type, Newtonsoft will output a "$type":"YourTypeName" in the object that it will use on deserialization.

Here is the Newtonsoft documentation for the TypeNameHandling options.

@rogierv
Copy link
Author

rogierv commented Aug 15, 2023

Thank you, @DavidBoike, for your explanation. It's good to know that there is an option to configure serialization at the endpoint.

@SimonCropp
Copy link
Contributor

any progress on this?

users will have stored sagas encoded with Newtonsoft that must still be readable even if new sagas are encoded with System.Text.Json

an interim version that reads in Newtonsoft + System.Text.Json and writes in System.Text.Json should make this possible

@magahl
Copy link

magahl commented Sep 17, 2024

any progress on this?

users will have stored sagas encoded with Newtonsoft that must still be readable even if new sagas are encoded with System.Text.Json

an interim version that reads in Newtonsoft + System.Text.Json and writes in System.Text.Json should make this possible

This is a good idea tbh. NServiceBus is our last dependency that still uses Newtonsoft. Would be nice to kill it all together.

@SimonCropp
Copy link
Contributor

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants
@SimonCropp @DavidBoike @rogierv @magahl and others