问题描述
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.
关于Xamarin for Android使用的Mono运行时,我目前拥有的最佳线索是this old blog post,其中指出:
在项目属性的“ Android选项”选项卡中,有一个“链接器”选项卡。在“链接”下拉列表中选择的选项是“仅Sdk程序集”还是“ SDK和用户程序集”?
我尝试切换到System.Text.Json,这会导致类似的错误:
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.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
我尝试在每个类中创建构造函数,并将其标记为internal
,这似乎已将错误消息移至其他类型,但并未解决。
感谢您的帮助
解决方法
是的,这是一个错误,表明剥离了未使用模式的链接器已经剥离了构造函数,因为它不知道Json.NET
(或System.Text.Json
)是通过反射使用它的。
没有与“仅Sdk程序集”选项完全相同的选项(因为本身没有“ Wasm Sdk”),但是您只需要添加包含Recording
类型的程序集作为{{1 }},位于Wasm平台头文件夹的根目录。
例如:
LinkerConfig.xml
(您可以在“属性”标签中查看包含该类型的项目的程序集名称。)