I've been hunting a bug that occurs in WASM for this error:
Newtonsoft.Json.JsonSerializationException: Unable to find a constructor to use for type Hqub.MusicBrainz.API.Entities.Recording.
A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'recordings[0].id', line 1, position 86.
The best lead that I currently have is this old blog post regarding the Mono runtime that Xamarin for Android uses: , which states:
In the 'Android options' tab of the project properties, there is a 'linker' tab. Is the selected option in the 'Linking' dropdown "Sdk Assemblies only" or is it "Sdk and user assemblies"?
If it is the latter, the parameterless constructor is skipped when linking, because no use is detected. So change it to "Sdk Assemblies only".
I've tried switching to System.Text.Json, which results in a similar error:
System.NotSupportedException: Deserialization of reference types without parameterless constructor is not supported. Type 'Hqub.MusicBrainz.API.Entities.Recording'
at System.Text.Json.ThrowHelper.ThrowNotSupportedException_DeserializeCreateObjectDelegateIsNull (System.Type invalidType) <0x4b45408 + 0x0004e> in <filename unknown>:0
at System.Text.Json.JsonSerializer.HandleStartObject (System.Text.Json.JsonSerializerOptions options, System.Text.Json.ReadStack& state) <0x4b17ec0 + 0x001f6> in <filename unknown>:0
at System.Text.Json.JsonSerializer.ReadCore (System.Text.Json.JsonSerializerOptions options, System.Text.Json.Utf8JsonReader& reader, System.Text.Json.ReadStack& readStack) <0x4b16610 + 0x0017c> in <filename unknown>:0
at System.Text.Json.JsonSerializer.ReadCore (System.Text.Json.JsonReaderState& readerState, System.Boolean isFinalBlock, System.ReadOnlySpan`1[T] buffer, System.Text.Json.JsonSerializerOptions options, System.Text.Json.ReadStack& readStack) <0x4b15610 + 0x00056> in <filename unknown>:0
at System.Text.Json.JsonSerializer.ReadAsync[TValue] (System.IO.Stream utf8Json, System.Type returnType, System.Text.Json.JsonSerializerOptions options, System.Threading.CancellationToken cancellationToken) <0x4abbcc8 + 0x00356> in <filename unknown>:0
at System.Threading.Tasks.ValueTask`1[TResult].get_Result () <0x4b71b40 + 0x00036> in <filename unknown>:0
at Hqub.MusicBrainz.API.MusicBrainzClient.GetAsync[T] (System.String url) <0x4924a28 + 0x006e0> in <filename unknown>:0
I've tried creating constructors in each class and marking them as internal
, which seems to have shifted the error message to a different type, but not solved it.
Any help is appreciated
Yes, that's an error that indicates that the linker which strips out unused code has stripped out the constructor, because it's not aware that
Json.NET
(orSystem.Text.Json
) is using it via reflection.There's no exact equivalent to the "Sdk Assemblies only" option (as there's no 'Wasm Sdk' per se), but you just need to add the assembly containing the
Recording
type as an entry inLinkerConfig.xml
at the root of your Wasm platform head folder.Eg:
(You can check the assembly name in the 'Properties' tab for the project containing the type.)