在Uno WASM中序列化JSON时出现“无法找到构造函数”

问题描述

我一直在寻找WASM中针对此错误而发生的错误

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和用户程序集”?

如果是后者,则在链接时会跳过无参数构造函数,因为未检测到任何用法。因此,将其更改为“仅限于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

(您可以在“属性”标签中查看包含该类型的项目的程序集名称。)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...