Skip to content

Weird results for Option, Result, anonymous types #28

@cmeeren

Description

@cmeeren

Consider this:

let data = {| Data = "This is a test"; Sub = {| Foo = Ok 1; Bar = [Some 2] |} |}
Log.Information("Data: {@Data}", data)

I get this:

{"Data": "This is a test", "Sub": {"Bar": [{"Value": 2, "$type": "Some"}], "Foo": {"ResultValue": 1, "$type": "Ok"}, "$type": "<>f__AnonymousType2950936943`2"}, "$type": "<>f__AnonymousType2576086083`2"}

There are several issues:

  • "$type": "Some" for Option seems weird
  • "$type": "Ok" for Result seems weird
  • "$type": "<>f__AnonymousType..." should not be there

Ideally, IMHO, for unions, you'd use a format like https://github.com/Tarmil/FSharp.SystemTextJson with union encoding set to

JsonUnionEncoding.Default
||| JsonUnionEncoding.InternalTag
||| JsonUnionEncoding.NamedFields
// And perhaps also this
||| JsonUnionEncoding.UnwrapFieldlessTags

I would therefore want something like this:

{"Data": "This is a test", "Sub": {"Bar": [2], "Foo": {"Case": "Ok", "ResultValue": 1}}}

I understand that this package is old and not actively maintained. I may end up writing my own library to destructure F# types.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions