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

Serialization/Deserialization in .net core 5.0 #360

Closed
jstachera opened this issue Feb 19, 2021 · 9 comments
Closed

Serialization/Deserialization in .net core 5.0 #360

jstachera opened this issue Feb 19, 2021 · 9 comments

Comments

@jstachera
Copy link

jstachera commented Feb 19, 2021

There is problem during Json deserialization for list of TvShows which were previously serialized in .net core 5.0 using serializer from System.Text.Json:

var jsonStr = JsonSerializer.Serialize(obj); // obj is List<TvShow>

I got error message:

Newtonsoft.Json.JsonSerializationException: 'Could not create an instance of type TMDbLib.Objects.Changes.ChangeItemBase. Type is an interface or abstract class and cannot be instantiated. Path '[19].Changes.Changes[0].Items[0].Action', line 1, position 434211.'

@LordMike
Copy link
Collaborator

Yea, we use newtonsoft converters to handle some special cases, like the one you mention where the base type (an abstract) has multiple derived types. Our converter picks the correct type at runtime.

To support system.text.json, we need to do the same.

@jstachera
Copy link
Author

What do you mean 'our converter'? Is it provided with the library? How can I use it? do you have any sample of json serialization/dserialization ?

@LordMike
Copy link
Collaborator

Sure - it's here:

https://github.com/LordMike/TMDbLib/blob/8d61234d9f2094d0d1e3926cc784e9f419907a2a/TMDbLib/Client/TMDbClient.cs#L31-L37

All the converters are here:
https://github.com/LordMike/TMDbLib/tree/master/TMDbLib/Utilities/Converters

So yea - it's included and used in the client.

I'm a bit curious though. Why would you get that exception when serializing stuff? .. I'd expect it if you were deserializing data, as this is where the concrete type needs to be identified.

@lewishenson
Copy link

Do you have any current plans to switch to using System.Text.Json classes?

@LordMike
Copy link
Collaborator

LordMike commented Apr 8, 2021

No. But I could take a look at it.

If you wanted, you could also try changing out the serializer and seeing if it can become workable.

Alternatively, I could also make it easier to use the serializer I make.

@lewishenson
Copy link

I could have a little play. I've just grabbed the code and run the tests and unfortunately I've got failing tests so I'm not sure how confident I would be with my changes. Do I need to do anything special to run the tests?

@LordMike
Copy link
Collaborator

LordMike commented Apr 9, 2021

Some tests are failing right now, which is also (part of) why we don't run tests in CI (the other part being #300).

If you make changes and most of the tests work, I'd say that's good enough. There are some "UtilityTests" which I can see are related to json conversion - they test the converters directly, so in reality these tests should be rewritten to use a "TMDbLib Converter" ... which also should be used for all serialization ..

Then you could consistently make changes.

So basically a small refactoring to pack the newtonsoft dependency into one class, and then replace that class.

@lewishenson
Copy link

Ah good to know thanks. I might take a look this weekend then.

@LordMike
Copy link
Collaborator

I've refactored the serialization into a new ITMDbSerializer interface. This change removes all Newtonsoft.Json from all API surfaces..

  • The constructor will no longer take a JsonSerializer
  • The Newtonsoft.Json dependency is hidden, so it is no longer transient
  • There is a TMDbJsonSerializer.Instance which has the internal serializer

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

No branches or pull requests

3 participants