首页 > 解决方案 > 如何为自定义 JsonConverter 设置 FloatParseHandling.Decimal?

问题描述

如何为自定义 JsonConverter 设置 FloatParseHandling.Decimal?

我们有一个结构DecimalDbValue,它在内部只包含一个我想要对其所有类型进行反序列化的十进制字段。

它使用幻数 (decimal.MinValue) 来指示“空”值。它是在具有可为空值类型的 .net 2.0 之前创建的!

这是我们 struct:: 的简化版本

    [Serializable]
    [JsonConverter(typeof(DecimalDbValueJsonConverter))]
    public struct DecimalDbValue : ISerializable
    {
        private readonly Decimal _decValue;

        public DecimalDbValue(
            decimal init)
        {
            _decValue = init;
        }

        [JsonConstructor]
        public DecimalDbValue(
            decimal? init)
        {
            if (init.HasValue)
                _decValue = init.Value;
            else
                _decValue = decimal.MinValue;
        }

        private DecimalDbValue(
            SerializationInfo objSerializationInfo,
            StreamingContext objStreamingContext)
        {
            _decValue = objSerializationInfo.GetDecimal("value");
        }

        public bool IsNotNull
        {
            get
            {
                return !IsNull;
            }
        }

        public bool IsNull
        {
            get
            {
                return _decValue.Equals(Decimal.MinValue);
            }
        }

        public Decimal Value
        {
            get
            {
                return _decValue;
            }
        }

        public void GetObjectData(
            SerializationInfo info,
            StreamingContext context)
        {
            info.AddValue("value", _decValue);
        }
}

我创建了一个 JsonConverter:

    class DecimalDbValueJsonConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return typeof(DecimalDbValue) == objectType;
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var value = reader.Value == null ? (decimal?)null : Convert.ToDecimal(reader.Value);
            return new DecimalDbValue(value);
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            var dbValue = (DecimalDbValue)value;
            if (dbValue.IsNull)
                writer.WriteNull();
            else
                writer.WriteValue(dbValue.Value);
        }
    }

[JsonConverter(typeof(DecimalDbValueJsonConverter))]并在 DecimalDbValue 结构上设置属性

我添加了一个测试:

        [Test]
        public void TestMaxDecimalDbValue()
        {
            var s = new DecimalDbValue(decimal.MaxValue);
            var json = JsonConvert.SerializeObject(s, Formatting.Indented);
            var x = JsonConvert.DeserializeObject<DecimalDbValue>(json);

            Assert.AreEqual(s, x);
        }

但它抛出: System.OverflowException : Value was either too large or too small for a Decimal.

如何设置FloatParseHandling.DecimalJsonConverter?如何使它也适用MaxValue?还有其他方法吗?

实际上我想让它序列化/反序列化就像一个decimal?(可为空的十进制)

谢谢

标签: c#.netjson.netjsonconverter

解决方案


这似乎是不可能的。

因此,我完全删除了 JsonConverter。

在结构上设置此属性:

[JsonObject(MemberSerialization.OptIn)]

以及构造函数上的this:

 [JsonConstructor]
    public DecimalDbValue(
        decimal? init) //IMPORTANT: if you change the name of init - rename the JsonProperty below too - you might break all existing json out there thou!

以及 get 属性上的这个:

[JsonProperty("init")]
    public Decimal Value
    {
        get
        {
            return _decValue;
        }
    }

不幸的是,这使 json 膨胀:

{
    "init": 79228162514264337593543950335.0
}

推荐阅读