JsonConvert.SerializeObject 到 class 具有不可为 null 的 DateTime 属性?
JsonConvert.SerializeObject to class with non-nullable DateTime properties?
背景
我有一些 JSON 被反序列化为具有 DateTime
属性的 Class。
有时JSON
对应的元素是null
.
当您尝试将 JSON
反序列化为 class 时会抛出错误,因为普通的旧 DateTime
无法接受 null
.
简单但删除了功能
所以最简单的解决方案是使 class 的接受属性成为可为 null 的 DateTime
(DateTime?
),但如果你这样做,那么会有很多 DateTime
您不能再对这些属性使用的方法。
有效但是...很奇怪?
所以在寻找替代方案时,我考虑了以下几点:
public class FooRelaxed
{
[Required(ErrorMessage = "Please enter the id.")]
public int? Id { get; set; }
[Required(ErrorMessage = "Please enter the Start Date.")]
public DateTime? StartDate { get; set; }
[Required(ErrorMessage = "Please enter the End Date.")]
public DateTime? EndDate { get; set; }
public FooRelaxed() { }
public FooRelaxed(
int? id,
DateTime? startdate,
DateTime? enddate)
{
this.Id = id;
this.EndDate = enddate;
this.StartDate = startdate;
}
}
public class FooStrict
[Required(ErrorMessage = "Please enter the id.")]
public int Id { get; set; }
[Required(ErrorMessage = "Please enter the Start Date.")]
public DateTime StartDate { get; set; }
[Required(ErrorMessage = "Please enter the End Date.")]
public DateTime EndDate { get; set; }
public FooStrict() { }
public FooStrict(FooRelaxed obj)
{
this.Id = Convert.ToInt32(obj.Id);
this.EndDate = Convert.ToDateTime(obj.EndDate);
this.StartDate = Convert.ToDateTime(obj.StartDate);
}
}
然后我使用这些 classes 来 :
- 将
JSON
反序列化为 class FooRelaxed,它具有可为空的 DateTime
属性
- 通过调用
Validator.TryValidateObject
验证结果对象的属性。
- 假设没有错误,然后实例化 'shadow' class,FooStrict,它具有不可为空的
DateTime
属性,使用 FooRexlaxed 实例作为构造函数的参数
- 对所有后续处理使用 FooStrict
我确信一定有比这更好的方法,但我不知道它是什么。谁能提出更好的解决方案?
使用适当的 JsonProperty
属性装饰:
[JsonProperty(NullValueHandling=NullValueHandling.Ignore)]
或者
[JsonProperty("<NameOfProperty>", NullValueHandling=NullValueHandling.Ignore)]
最终代码为:
[JsonProperty("EndDate", NullValueHandling=NullValueHandling.Ignore)]
public DateTime EndDate { get; set; }
[JsonProperty("StartDate", NullValueHandling=NullValueHandling.Ignore)]
public DateTime StartDate { get; set; }
背景
我有一些 JSON 被反序列化为具有 DateTime
属性的 Class。
有时JSON
对应的元素是null
.
当您尝试将 JSON
反序列化为 class 时会抛出错误,因为普通的旧 DateTime
无法接受 null
.
简单但删除了功能
所以最简单的解决方案是使 class 的接受属性成为可为 null 的 DateTime
(DateTime?
),但如果你这样做,那么会有很多 DateTime
您不能再对这些属性使用的方法。
有效但是...很奇怪?
所以在寻找替代方案时,我考虑了以下几点:
public class FooRelaxed
{
[Required(ErrorMessage = "Please enter the id.")]
public int? Id { get; set; }
[Required(ErrorMessage = "Please enter the Start Date.")]
public DateTime? StartDate { get; set; }
[Required(ErrorMessage = "Please enter the End Date.")]
public DateTime? EndDate { get; set; }
public FooRelaxed() { }
public FooRelaxed(
int? id,
DateTime? startdate,
DateTime? enddate)
{
this.Id = id;
this.EndDate = enddate;
this.StartDate = startdate;
}
}
public class FooStrict
[Required(ErrorMessage = "Please enter the id.")]
public int Id { get; set; }
[Required(ErrorMessage = "Please enter the Start Date.")]
public DateTime StartDate { get; set; }
[Required(ErrorMessage = "Please enter the End Date.")]
public DateTime EndDate { get; set; }
public FooStrict() { }
public FooStrict(FooRelaxed obj)
{
this.Id = Convert.ToInt32(obj.Id);
this.EndDate = Convert.ToDateTime(obj.EndDate);
this.StartDate = Convert.ToDateTime(obj.StartDate);
}
}
然后我使用这些 classes 来 :
- 将
JSON
反序列化为 class FooRelaxed,它具有可为空的DateTime
属性 - 通过调用
Validator.TryValidateObject
验证结果对象的属性。 - 假设没有错误,然后实例化 'shadow' class,FooStrict,它具有不可为空的
DateTime
属性,使用 FooRexlaxed 实例作为构造函数的参数 - 对所有后续处理使用 FooStrict
我确信一定有比这更好的方法,但我不知道它是什么。谁能提出更好的解决方案?
使用适当的 JsonProperty
属性装饰:
[JsonProperty(NullValueHandling=NullValueHandling.Ignore)]
或者
[JsonProperty("<NameOfProperty>", NullValueHandling=NullValueHandling.Ignore)]
最终代码为:
[JsonProperty("EndDate", NullValueHandling=NullValueHandling.Ignore)]
public DateTime EndDate { get; set; }
[JsonProperty("StartDate", NullValueHandling=NullValueHandling.Ignore)]
public DateTime StartDate { get; set; }