猿问

实现 IConvertible 的自定义数据类型的反序列化问题

我有一堂课ChargesDetail,正在尝试反序列化 json,如下所示。在这里,我使用的数据类型是Amount.


public class ChargesDetail

{

   public double DiscountRate { get; set; }

   public Amount DiscountAmount { get; set; }

}


public class Amount:IConvertible 

{

    private double _val = 0;

    private int _decimal = 5;


    public Amount()

    {

    }


    public Amount(double amount): this()            

    {

       // this.Value = amount;

        _val = Math.Round(amount, _decimal);

    }


    #region IConvertible Members


    // Implementation snipped


    #endregion

}

我的 JSON 看起来像:


{ "DiscountRate":0.0, "DiscountAmount":0.0 }

我试图像这样反序列化:


T result = JsonConvert.DeserializeObject<ChargesDetail>(json);

它给了我一个例外,例如:


从“System.Double”到“Amount”的无效转换。


在 System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) at System.Double.System.IConvertible.ToType(Type type, IFormatProvider provider) at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfoculture, JsonContract contract, Type targetType)


如何将 json 反序列化为此自定义数据类型?


我无法删除 IConvertible,因为它在某些计算中会引发 System.StackOverflowException。

我无法将 Amount 类型更改为 double 类型,bcz 有 100 多个相同类型的属性并在“Amount”类中进行计算。


犯罪嫌疑人X
浏览 188回答 4
4回答

BIG阳

正如您的问题表明您正在使用json.net,您可以使用自定义Amount将您序列化为单个十进制值:JsonConverterpublic class AmountConverter : JsonConverter{&nbsp; &nbsp; public override bool CanConvert(Type objectType)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return objectType == typeof(Amount);&nbsp; &nbsp; }&nbsp; &nbsp; public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // Would decimal be more appropriate than double?&nbsp; &nbsp; &nbsp; &nbsp; var value = serializer.Deserialize<double?>(reader);&nbsp; &nbsp; &nbsp; &nbsp; if (value == null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; return new Amount(value.Value);&nbsp; &nbsp; }&nbsp; &nbsp; public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; writer.WriteValue(((Amount)value).Value);&nbsp; &nbsp; }}然后,您可以将其应用于您的模型,如下所示:[JsonConverter(typeof(AmountConverter))]public class Amount : IConvertible{&nbsp; &nbsp; private double _val = 0;&nbsp; &nbsp; private int _decimal = 5;&nbsp; &nbsp; public double Value { get { return _val; } }&nbsp; &nbsp; public Amount()&nbsp; &nbsp; {&nbsp; &nbsp; }&nbsp; &nbsp; public Amount(double amount)&nbsp; &nbsp; &nbsp; &nbsp; : this()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // this.Value = amount;&nbsp; &nbsp; &nbsp; &nbsp; _val = Math.Round(amount, _decimal);&nbsp; &nbsp; }&nbsp; &nbsp; #region IConvertible Members&nbsp; &nbsp; #endregion}或者在JsonSerializerSettings.Converters序列化和反序列化的时候加入:var settings = new JsonSerializerSettings{&nbsp; &nbsp; Converters = { new AmountConverter() },};var = JsonConvert.DeserializeObject<T>(json, settings);

慕运维8079593

在浏览您的 JSON 时。{ "FileVersion":"11.03", "ChargesDetail":{ "DiscountRate":0.0, "DiscountAmount":0.0 } }&nbsp;public class ChargesDetail{&nbsp; &nbsp;public double DiscountRate { get; set; }&nbsp; &nbsp;public Amount DiscountAmount { get; set; }}public class Amount:IConvertible&nbsp;{&nbsp;}在这里,您需要将 DiscountAmount 的类型更改为 double,因为您映射的结构不正确。或者,如果您将 JSON 更改为{ "FileVersion":"11.03", "ChargesDetail":{ "DiscountRate":0.0, "DiscountAmount":{ "DiscountAmountVar":0.0 }} }和类public class ChargesDetail{&nbsp; &nbsp;public double DiscountRate { get; set; }&nbsp; &nbsp;public Amount DiscountAmount { get; set; }}public class Amount:IConvertible&nbsp;{&nbsp;&nbsp; &nbsp;public double DiscountAmountVar{get;set;}}然后您之前的映射将起作用。

炎炎设计

在您的班级 Amount 中,您可以删除IConvertible并执行为:public class Amount {&nbsp; &nbsp; public decimal DiscountAmountVar { get; set; }}有了这个和你的其他课程:&nbsp; &nbsp; public class ChargesDetail{&nbsp; &nbsp;public decimal DiscountRate { get; set; }&nbsp; &nbsp;public Amount DiscountAmount { get; set; }}而这个 JSON:****EDIT****string json =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @"{""DiscountRate"":12.0, ""DiscountAmount"":{ ""DiscountAmountVar"":13.0 } }";**编辑:刚刚意识到您需要删除一级 Json。您应该能够制作 JsonConvert。主要代码:class Program{&nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; string json =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @"{""DiscountRate"":12.0, ""DiscountAmount"":{ ""DiscountAmountVar"":13.0 } }";&nbsp; &nbsp; &nbsp; &nbsp; var converted = JsonConvert.DeserializeObject<ChargesDetail>(json);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(converted.DiscountAmount);&nbsp; &nbsp; }}public class ChargesDetail{&nbsp; &nbsp; public decimal DiscountRate { get; set; }&nbsp; &nbsp; public Amount DiscountAmount { get; set; }}public class Amount&nbsp;{&nbsp; &nbsp; public decimal DiscountAmountVar { get; set; }}

GCT1015

你的类Amount没有实现接口IConvertable。
随时随地看视频慕课网APP
我要回答