【问题标题】:How do I serialize (NewtonSoft) a class that includes an interface with a property named Type?如何序列化 (NewtonSoft) 包含具有名为 Type 的属性的接口的类?
【发布时间】:2021-04-23 17:55:12
【问题描述】:

我需要序列化这个类的结果(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, 
                      GetType(PaymentResponse), 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”,
“行动”: { "paymentData": "XXX", "paymentMethodType": "方案", “令牌”:“YYY”},“身份验证”:{ “threeds2.fingerprintToken”:“ZZZ”},“详细信息”:[ { "key": "threeds2.fingerprint", “类型”:“文本” } ], "paymentData": "QQQ" }

【问题讨论】:

    标签: .net jsonconvert adyen


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-20
      • 2011-01-28
      • 2017-01-06
      • 2022-01-01
      • 1970-01-01
      相关资源
      最近更新 更多