Coder Social home page Coder Social logo

Comments (3)

EdwardCooke avatar EdwardCooke commented on June 3, 2024 1

The Deserializer isn't meant to be instantiated directly. You should use the DeserializerBuilder so you can set all the options for it. It also adds all of the necessary objects and other required classes so it will work correctly.

I'm not an F# developer and don't know what the implications of removing option from the definition will do with other code.

Using the DeserializerBuilder and a couple of options and removing option from the subThing property made your code work:

[<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:

{ subThing = { value = "a" } }

from yamldotnet.

tymokvo avatar tymokvo commented on June 3, 2024

I'm not an F# developer and don't know what the implications of removing option from the definition will do with other code.

It's basically equivalent to Nullable<T> in C# land. So, removing it makes the field required.

I don't know enough about how YamlDotNet works to know what's going on, but serializing the values of F# types also results in somewhat unexpected behavior which might hint at what's happening? It may be related to the implementation of Option.

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"
serialize None:
"subThing@: 
subThing: 
"
deserialize None:
{ subThing = None }
serialize Some:
"subThing@: &o0
  Value:
    value@: a
    value: a
subThing: *o0
"
(Line: 2, Col: 3, Idx: 17) - (Line: 2, Col: 8, Idx: 22): Property 'Value' not found on type 'Microsoft.FSharp.Core.FSharpOption`1[[FSI_0002+SubThing, FSI-ASSEMBLY2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]'.
Stopped due to error

from yamldotnet.

EdwardCooke avatar EdwardCooke commented on June 3, 2024

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 FSharpOption generic. I just don't have time to work on that right now.

If you search the codebase for Nullable you can see where we do the checks for it. It would be a bit tricky with FSharp since that base generic class isn't included in the csharp and I don't want to include any dependencies, like fsharp. We currently don't impose non .net default libraries to use yamldotnet and I'd like to keep it that way.

To accomplish this we'd probably have to do type checking using string comparisons and more reflection to get/set the correct fields.

from yamldotnet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.