我如何序列化 (NewtonSoft) 一个类,该类包含一个名为 Type 的属性的接口?

问题描述

我需要序列化此类的结果(Assembly Adyen,Version=6.0.0.0):

<Runtime.Serialization.DataContractAttribute>
Public Class PaymentResponse
    Implements IEquatable(Of PaymentResponse),IValidatableObject

    Public Sub New(...)

    ...
   <Runtime.Serialization.DataMemberAttribute(Name:="action",EmitDefaultValue:=False)>
    Public Property Action As IPaymentResponseAction

    ...        

End Class

查看结果,我按预期有 Action.PaymentData、Action.PaymentMethodType、Action.Token、Action.[Type] 和 Action.Url: Screenshot of Autos

但是当我序列化结果时,

Dim tmp As String = Newtonsoft.Json.JsonConvert.SerializeObject(ret,GetType(PaymentResponse),New 
                     Newtonsoft.Json.JsonSerializerSettings() With {.[Error] = AddressOf jsonerror})

我只得到:

{
  "resultCode": "IdentifyShopper","action": {
  "paymentData": "XXX","paymentMethodType": "scheme","token": "YYY"
},"authentication": {
"threeds2.fingerprintToken": "ZZZ"
},"details": [
 {
  "key": "threeds2.fingerprint","type": "text"
 }
],"paymentData": "QQQ"
}

我的 Json 字符串中没有 Action.[Type] 和“Action.Url”。

jsonerror() 

永远不会被击中。

编辑:我添加了一个跟踪器:

Dim tracewriter As Newtonsoft.Json.Serialization.ITraceWriter = New 
                      Newtonsoft.Json.Serialization.MemoryTraceWriter
Dim tmp As String = Newtonsoft.Json.JsonConvert.SerializeObject(ret,New 
                      Newtonsoft.Json.JsonSerializerSettings() With 
                     {.TraceWriter = tracewriter,.[Error] = AddressOf 
                        jsonerror})

一切看起来都很好,只是我没有得到操作。[type] 序列化:

2021-01-19T14:25:11.852 信息开始序列化 Adyen.Model.Checkout.PaymentResponse。小路 ''。 2021-01-19T14:25:11.854 信息开始序列化 Adyen.Model.Checkout.PaymentResponse+ResultCodeEnum 带转换器 Newtonsoft.Json.Converters.StringEnumConverter。路径“结果代码”。 2021-01-19T14:25:11.854 信息已完成序列化 Adyen.Model.Checkout.PaymentResponse+ResultCodeEnum 带转换器 Newtonsoft.Json.Converters.StringEnumConverter。路径“结果代码”。 2021-01-19T14:25:11.856 信息开始序列化 Adyen.Model.Checkout.Action.CheckoutThreeDS2FingerPrintAction。小路 '行动'。 2021-01-19T14:25:11.857 信息已完成序列化 Adyen.Model.Checkout.Action.CheckoutThreeDS2FingerPrintAction。小路 '行动'。 2021-01-19T14:25:11.857 信息开始序列化 System.Collections.Generic.Dictionary2[System.String,System.String]. Path 'authentication'. 2021-01-19T14:25:11.857 Info Finished serializing System.Collections.Generic.Dictionary2[System.String,System.String]。 路径“认证”。 2021-01-19T14:25:11.857 信息开始 连载 System.Collections.Generic.List1[Adyen.Model.Checkout.InputDetail]. Path 'details'. 2021-01-19T14:25:11.857 Info Started serializing Adyen.Model.Checkout.InputDetail. Path 'details'. 2021-01-19T14:25:11.857 Info Finished serializing Adyen.Model.Checkout.InputDetail. Path 'details[0]'. 2021-01-19T14:25:11.857 Info Finished serializing System.Collections.Generic.List1[Adyen.Model.Checkout.InputDetail]。 路径“详细信息”。 2021-01-19T14:25:11.860 信息已完成序列化 Adyen.Model.Checkout.PaymentResponse。小路 ''。 2021-01-19T14:25:11.860 详细序列化 JSON: { "resultCode": "IdentifyShopper",
“行动”: { "支付数据": "XXX","paymentMethodType": "方案",“令牌”:“YYY”},“身份验证”:{ “threeds2.fingerprintToken”:“ZZZ”},“详细信息”:[ { "key": "threeds2.fingerprint",“类型”:“文本” } ],"paymentData": "QQQ" }

解决方法

这是 Adyen .NET 库中的一个错误。 IPaymentResponseAction 的实现类缺少 DataMember 注释。有一个 fix 可以合并,很快就会在下一个补丁版本中提供。建议您在发布时更新到补丁版本。

对于 URL 字段,它仅可用于某些操作,例如重定向,例如理想的付款方式。对于方案,如您的情况,URL 不适用。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...