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

Cannot deserialize BrowseResult (and others) due to no default constructor #4

Closed
mattjhosking opened this issue May 20, 2020 · 6 comments

Comments

@mattjhosking
Copy link

This occurs when calling BrowseArtistReleasesAsync - could this have occurred due to shifting to System.Text.Json? Newtonsoft is much more forgiving in this and a number of other cases. Either a a converter will be needed to create the classes successfully, or default constructors added to all classes that need to support JSON deserialization.

@Zastai
Copy link
Owner

Zastai commented May 20, 2020

Could you add an example method call and the stack trace of the exception?
I wrote custom converters for maximum flexibility (and slightly better speed). I may have neglected to register one of the converters

@mattjhosking
Copy link
Author

mattjhosking commented May 20, 2020

        public async Task<ArtistRelease[]> RetrieveReleases(Guid artistId)
        {
            var searchResult = await new Query("Dev Test", "1.0", "matt.j.hosking@gmail.com").BrowseArtistReleasesAsync(artistId).ConfigureAwait(false);
            return searchResult.Results
                .Select(x => new ArtistRelease { Title = x.Title, Year = x.Date?.Year })
                .ToArray();
        }

System.NotSupportedException
HResult=0x80131515
Message=Deserialization of reference types without parameterless constructor is not supported. Type 'MetaBrainz.MusicBrainz.Objects.Browses.BrowseResult'
Source=System.Text.Json
StackTrace:
at System.Text.Json.ThrowHelper.ThrowNotSupportedException_DeserializeCreateObjectDelegateIsNull(Type invalidType)
at System.Text.Json.JsonSerializer.HandleStartObject(JsonSerializerOptions options, ReadStack& state)
at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack)
at System.Text.Json.JsonSerializer.ReadCore(Type returnType, JsonSerializerOptions options, Utf8JsonReader& reader)
at System.Text.Json.JsonSerializer.Deserialize(String json, Type returnType, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)
at MetaBrainz.Common.Json.JsonUtils.Deserialize[T](String json, JsonSerializerOptions options)
at MetaBrainz.MusicBrainz.Query.Deserialize[T](String json)
at MetaBrainz.MusicBrainz.Objects.Browses.BrowseResults1.Deserialize(String json) at MetaBrainz.MusicBrainz.Objects.PagedQueryResults3.d__12.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at xxxxxx.Music.Services.MusicBrainz.MusicBrainzArtistRepository.d__2.MoveNext() in C:\Dev\xxxxxx.Music\xxxxxx.Music.Services\MusicBrainz\MusicBrainzArtistRepository.cs:line 26

This exception was originally thrown at this call stack:
[External Code]
xxxxxx.Music.Services.MusicBrainz.MusicBrainzArtistRepository.RetrieveReleases(System.Guid) in MusicBrainzArtistRepository.cs

@Zastai
Copy link
Owner

Zastai commented May 20, 2020

Thanks. For future reference, it's generally useful to provide the parameter values too - often a bug is specific to a combination of entities.
In this case, I was able to reproduce the crash, so that won't be needed. Will look at later after work is finished. (But it's Microsoft Build too, so I may not have a huge amount of free time.)

Side note: if it's feasible in your code, I would recommend creating longer-lived Query objects, instead of creating a new one per request, especially if you are likely to have a large volume of requests.

@mattjhosking
Copy link
Author

Thanks @Zastai and noted. Let me know if you need anything else or I could look into it as well if that helps.

@Zastai Zastai closed this as completed in 9236181 May 20, 2020
@Zastai
Copy link
Owner

Zastai commented May 20, 2020

Pushed a fix; could you see whether this resolves your issue? If it does, I'll push a new release package later today.

@mattjhosking
Copy link
Author

That's fixed it, thanks!

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

2 participants