我如何序列化(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,行动。PaymentMethodType,行动。令牌,行动。[类型]和动作。Url如预期:汽车屏幕截图

但是当我序列化结果时,

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"
}

我没有得到动作。[类型]和"Action.Url">

jsonerror() 

永远不会被点击。

我添加了一个跟踪器:

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

一切看起来都很好,除了我没有动作。[型]序列化:

2021-01-19T14:25:11.852 Info开始序列化Adyen.Model.Checkout.PaymentResponse。路径"。2021 - 01 - 19 t14:25:11.854开始序列化Adyen.Model.Checkout。PaymentResponse+ResultCodeEnum与转换器Newtonsoft.Json.Converters.StringEnumConverter。"resultCode"道路。2021-01-19T14:25:11.854信息序列化完成Adyen.Model.Checkout。PaymentResponse+ResultCodeEnum与转换器Newtonsoft.Json.Converters.StringEnumConverter。"resultCode"道路。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 - 19 t14:25:11.860详细序列化JSON: {"resultCode"; "IdentifyShopper",
"action"; {"paymentData"XXX"paymentMethodType"scheme"token"YYY"}, "authentication": {"threeds2.fingerprintToken"ZZZ"}, "details": [{"key"threeds2.fingerprint"type"text"}], "paymentData";QQQ"}

这是Adyen . net库中的一个错误。IPaymentResponseAction的实现类缺少DataMember注释。有一个修复准备被合并,并将在不久的下一个补丁版本中可用。我建议你在发布时更新到补丁版本。

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

最新更新