未调用F#type字段上的JsonConverter
对于下面的代码未调用F#type字段上的JsonConverter,json,f#,system.text.json,Json,F#,System.text.json,对于下面的代码 type MyConverter = inherit JsonConverter<Datetime>() ..... type MyType = { [<JsonConverter(typeof<MyConverter>)>] date: DateTime } JsonSerializer.Deserialize("{\"date\": \"2020-10-20\"}", typeof<MyType&
type MyConverter =
inherit JsonConverter<Datetime>()
.....
type MyType = {
[<JsonConverter(typeof<MyConverter>)>]
date: DateTime
}
JsonSerializer.Deserialize("{\"date\": \"2020-10-20\"}", typeof<MyType>)
类型MyConverter=
继承JsonConverter()
.....
类型MyType={
[]
日期:日期时间
}
JsonSerializer.Deserialize(“{”日期\“:\”2020-10-20 \“}”,typeof)
未调用转换器。如果属性将被移动到那里,则会对整个类型调用它,但不会对字段调用
这是故意的吗?有没有可能为精确的字段指定转换器?似乎有两种方法可以定义记录类型以使其正常工作
[<CLIMutable>]
type MyType = {
[<JsonConverter(typeof<MyConverter>)>]
date: DateTime
}
[]
类型MyType={
[]
日期:日期时间
}
或
[]
类型MyType={
[]
可变日期:日期时间
}
经过所有讨论,我们决定使用以下几种解决方案的组合:
JsonConverter
属性的转换器,并使用它转换字段值System.Text.Json不允许反序列化记录类型。这对您有用吗?
System.Text.Json
不支持不可变类型的反序列化,请参阅。它也不处理字段,尽管我不记得f#记录是使用属性或字段实现的。请参阅:。@dbc它们是属性,但它们是只读的也许您可以尝试将[]属性添加到记录声明中。这改变了记录的编译方式——属性和默认构造函数上的getter和setter,因此记录的行为更像类。
[<Struct>]
type MyType = {
[<JsonConverter(typeof<MyConverter>)>]
mutable date: DateTime
}