c# - 如何为自定义 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.Decimal
JsonConverter?如何使它也适用MaxValue
?还有其他方法吗?
实际上我想让它序列化/反序列化就像一个decimal?
(可为空的十进制)
谢谢
解决方案
这似乎是不可能的。
因此,我完全删除了 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
}
推荐阅读
- java - 如何在java中裁剪图像?
- jquery - 导出模块中未触发 jquery 更改事件
- c# - GeoCoordinateWatcher 如何获取位置 C#
- angular - 如何通过在Angular4中将localStorage值作为参数传递来调用API url
- c++ - 确定用户是否可以使用 c++ 提升进程
- selenium - appium中的水平滚动
- java - 如何在 Java 中拆分和解析文本文件
- spring - 如何使用 Spring Mongo 模板转换数据类型
- python - 如何从不以数字开头的列表中返回所有项目?
- matplotlib - 如何在活动绘图期间删除 matplotlib 中不可见轴的空白?