-
-
Notifications
You must be signed in to change notification settings - Fork 494
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FSharp deserialization of records with optional record fields #913
Comments
The I'm not an F# developer and don't know what the implications of removing Using the [<CLIMutable>]
type SubThing = { value: string }
[<CLIMutable>]
type Thing = { subThing: SubThing }
open YamlDotNet.Serialization
let d = DeserializerBuilder().EnablePrivateConstructors().IncludeNonPublicProperties().Build()
"""
subThing:
value: "a"
"""
|> d.Deserialize<Thing>
|> printfn "%A"
Results:
|
It's basically equivalent to I don't know enough about how Using this script, for example, the second value doesn't successfully round-trip to and from YAML. #r "nuget: YamlDotNet"
[<CLIMutable>]
type SubThing = { value: string }
[<CLIMutable>]
type Thing = { subThing: SubThing option }
open YamlDotNet.Serialization
let d = DeserializerBuilder().EnablePrivateConstructors().IncludeNonPublicProperties().Build()
let s = SerializerBuilder().Build()
{
subThing = None
}
|> s.Serialize
|> (fun v ->
printfn "serialize None:\n%A" v
v
)
|> d.Deserialize<Thing>
|> printfn "deserialize None:\n%A"
{
subThing = Some { value = "a" }
}
|> s.Serialize
|> (fun v ->
printfn "serialize Some:\n%A" v
v
)
|> d.Deserialize<Thing>
|> printfn "deserialize Some:\n%A"
|
YamlDotNet uses reflection by default to set and get values, there's some special handling for the nullable type because of the value field. We may be able to do the same thing for the If you search the codebase for To accomplish this we'd probably have to do type checking using string comparisons and more reflection to get/set the correct fields. |
I'm trying to figure out the canonical way to deserialize a record with an optional field that is not a scalar value.
Given the script:
Running this fails with the cryptic error:
I would expect this to result in
{ subThing = Some { value = "a" } }
.A simple omission of the field succeeds in the way I would expect:
However, if the type of the
subThing
field is a scalar (e.g.string
), the behavior is as I expect; omitting it results inNone
and supplying it results inSome <value>
.Is this behavior configurable in some way?
The text was updated successfully, but these errors were encountered: