我如何序列化 (NewtonSoft) class,其中包含一个带有 属性 命名类型的接口?

How do I serialize (NewtonSoft) a class that includes an interface with a property named Type?

我需要序列化这个class(Assembly Adyen,版本=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 字符串中输入 [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, 
                      GetType(PaymentResponse), New 
                      Newtonsoft.Json.JsonSerializerSettings() With 
                     {.TraceWriter = tracewriter, .[Error] = AddressOf 
                        jsonerror})

一切看起来都很好,除了我没有采取行动。[type] 序列化:

2021-01-19T14:25:11.852 Info Started serializing Adyen.Model.Checkout.PaymentResponse. Path ''. 2021-01-19T14:25:11.854 Info Started serializing Adyen.Model.Checkout.PaymentResponse+ResultCodeEnum with converter Newtonsoft.Json.Converters.StringEnumConverter. Path 'resultCode'. 2021-01-19T14:25:11.854 Info Finished serializing Adyen.Model.Checkout.PaymentResponse+ResultCodeEnum with converter Newtonsoft.Json.Converters.StringEnumConverter. Path 'resultCode'. 2021-01-19T14:25:11.856 Info Started serializing Adyen.Model.Checkout.Action.CheckoutThreeDS2FingerPrintAction. Path 'action'. 2021-01-19T14:25:11.857 Info Finished serializing Adyen.Model.Checkout.Action.CheckoutThreeDS2FingerPrintAction. Path 'action'. 2021-01-19T14:25:11.857 Info Started serializing 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]. Path 'authentication'. 2021-01-19T14:25:11.857 Info Started serializing 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]. Path 'details'. 2021-01-19T14:25:11.860 Info Finished serializing Adyen.Model.Checkout.PaymentResponse. Path ''. 2021-01-19T14:25:11.860 Verbose Serialized 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 注释。有一个 fix 准备合并,将很快在下一个补丁版本中提供。建议发布时更新补丁版本

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