简体   繁体   English

C# AdaptiveCards 1.2.4 简单卡反序列化问题

[英]C# AdaptiveCards 1.2.4 problems with deserialization of simple card

I am using AdaptiveCards 1.2.4 from NuGet and there are some problems with deserialization.我正在使用 NuGet 中的 AdaptiveCards 1.2.4,反序列化存在一些问题。 I was able to drill down and identify where the error comes from exactly and shortened the card to 2 column sets.我能够深入研究并确定错误的确切来源,并将卡片缩短为 2 列集。 The card is parsed with them separately but it's not with both at the same time.卡片是分别与它们一起解析的,但不是同时与它们一起解析的。

Exception.Message = "Current error context error is different to requested error."
Exception.StackTrace = "   at 
Newtonsoft.Json.Serialization.JsonSerializerInternalBase.GetErrorContext(Object currentObject, Object 
member, String path, Exception error)
at Newtonsoft.Json.Serialization.JsonSerializerInternalBase.IsErrorHandled(Object currentObject, J 
JsonContract contract, Object keyValue, IJsonLineInfo lineInfo, String path, Exception ex)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type 
objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings 
settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
at AdaptiveCards.AdaptiveCard.FromJson(String json)
at AdaptiveCardsDeserializationProblem.Program.Main(String[] args) in 
C:\Work\Demo\AdaptiveCardsDeserializationProblem\AdaptiveCardsDeserializationProblem\Startup.cs:line 
34"

This is a sample code that shows the issue.这是显示问题的示例代码。

        try
        {
            var problematicCard = "{\"type\":\"AdaptiveCard\",\"body\":[{\"type\":\"ColumnSet\",\"spacing\":\"None\",\"columns\":[{\"type\":\"Column\",\"style\":\"default\",\"items\":[{\"type\":\"FactSet\"}],\"width\":\"stretch\"}]},{\"type\":\"ColumnSet\",\"spacing\":\"None\",\"columns\":[{\"type\":\"Column\",\"style\":\"default\",\"items\":[{\"type\":\"FactSet\"}],\"width\":\"stretch\"}]}],\"version\":\"1.0\"}";
            var firstColumnSet = "{\"type\":\"AdaptiveCard\",\"body\":[{\"type\":\"ColumnSet\",\"spacing\":\"None\",\"columns\":[{\"type\":\"Column\",\"style\":\"default\",\"items\":[{\"type\":\"FactSet\"}],\"width\":\"stretch\"}]}],\"version\":\"1.0\"}";
            var secondColumnSet = "{\"type\":\"AdaptiveCard\",\"body\":[{\"type\":\"ColumnSet\",\"spacing\":\"None\",\"columns\":[{\"type\":\"Column\",\"style\":\"default\",\"items\":[{\"type\":\"FactSet\"}],\"width\":\"stretch\"}]}],\"version\":\"1.0\"}";

            var adaptiveCard = AdaptiveCard.FromJson(problematicCard);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            throw;
        }

This is the card's JSON -> https://pastebin.com/Ud4BZ33g这是卡的 JSON -> https://pastebin.com/Ud4BZ33g

And this is how the card looks like in the adaptive cards designer这就是卡片在自适应卡片设计器中的样子

这就是卡片在自适应卡片设计器中的样子

It looks like it's throwing here.. https://github.com/JamesNK/Newtonsoft.Json/blob/master/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalBase.cs#L103它看起来像是扔在这里.. https://github.com/JamesNK/Newtonsoft.Json/blob/master/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalBase.cs#L103

The best way to diagnose serialization/deserialization issues is to populate the object yourself and serialize it to see what json content would it generate.诊断序列化/反序列化问题的最佳方法是自己填充 object 并对其进行序列化,以查看它会生成什么 json 内容。 We expect the same json to deserialize back, without errors.我们期望相同的 json 反序列化回来,没有错误。

In this case, we populate the exact object, and serialize it using the ToJson() method.在这种情况下,我们填充确切的 object,并使用ToJson()方法对其进行序列化。

We see that the only difference is我们看到唯一的区别是

Problematic:有问题的:

... \"items\":[{\"type\":\"FactSet\"}],\"width\":\"stretch\"}]} ...

Working:在职的:

... \"items\":[{\"type\":\"FactSet\", \"facts\": []}],\"width\":\"stretch\"}]} ...

And yes, when changed to contain the facts empty array, the deserialization works, as expected.是的,当更改为包含事实空数组时,反序列化按预期工作。

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM