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

Allow customising the serializer settings for Snapshots #79

Closed
SbiCA opened this issue Jan 7, 2021 · 2 comments
Closed

Allow customising the serializer settings for Snapshots #79

SbiCA opened this issue Jan 7, 2021 · 2 comments
Labels
enhancement New feature or request

Comments

@SbiCA
Copy link

SbiCA commented Jan 7, 2021

Is your feature request related to a problem? Please describe.
Allow adjustment of the JsonSerializerSettings in JObjectHelper that is used by JsonSnapshotStore, so that snapshots can be made more readable e.g. when using custom JsonConverters they are taken into account.

e.g. an example of the current behaviour

"myList": [
    {
      "Name": "My SmartEnum",
      "Value": "My Value that should be used instead"
    }
],

after allowing to add converters for instance

"myList": [
      "My Value that should be used instead"
],

Describe the solution you'd like

Not yet full thought through, but maybe

  • Allow defining a convention based class e.g. **CustomSnapshotSerialzation** that could expose either the complete JsonSerializerSettings or hooks to be called by the default implementation when adding converters.

What would be great is to at least be able to add the following

 serializerSettings.Converters.Add(new SmartEnumValueConverter<MySmartEnum, string>());

Describe alternatives you've considered

Additional context

@SbiCA SbiCA added the enhancement New feature or request label Jan 7, 2021
@theramis
Copy link
Owner

Hey @SbiCA,

Thanks for making the issue.
I've definitely thought about doing this in the past so that users could use their own serialiser settings.

I've shied away from it in the past as I didn't want to expose the internal working of Snapper. Specifically the usage of Newtonsoft.Json. I will most likely be switching to System.Text.Json in the future once it is more feature rich and ideally the change should not be noticeable by consumers.

As an alternative have you thought about converting your object into a jobject using your own serialiser before calling Snapper?
e.g. something like this. You could abstract out the conversion into a method if needed.

JObject.FromObject(obj, JsonSerializer.Create(JsonSettings)).ShouldMatchSnapshot();

@theramis
Copy link
Owner

Hey @SbiCA ,
Sorry it took a bit longer than expected!

Just added Custom Snapshot Settings in the latest version 2.4.0. See the docs here.

That should hopefully solve your issue!

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

No branches or pull requests

2 participants