在Uno WASM中序列化JSON时"Unable to find constructor"



我一直在寻找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运行时的旧博客文章:,其中写道:

在项目属性的"Android选项"选项卡中,有一个"链接器"选项卡。是"链接"下拉列表中的选定选项吗;仅Sdk组件";还是";Sdk和用户程序集";?

如果是后者,则在链接时会跳过无参数构造函数,因为未检测到使用。所以把它改成";Sdk Assemblys only";。

我尝试切换到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.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 

我尝试在每个类中创建构造函数,并将它们标记为internal,这似乎将错误消息转移到了不同的类型,但并没有解决它

感谢提供的任何帮助

是的,这是一个错误,表明去掉未使用代码的链接器已经去掉了构造函数,因为它不知道Json.NET(或System.Text.Json(正在通过反射使用它。

没有确切等同于";仅Sdk组件";选项(因为本身没有"Wasm Sdk"(,但您只需要将包含Recording类型的程序集添加为Wasm平台头文件夹根目录LinkerConfig.xml中的一个条目。

例如:

<assembly fullname="Hqub.MusicBrainz.API />

(您可以在"属性"选项卡中检查包含该类型的项目的程序集名称。(

相关内容

最新更新