Skip to content
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

Support expanding the content of an object to the upper layer #56

Open
shanoaice opened this issue Oct 20, 2021 · 0 comments
Open

Support expanding the content of an object to the upper layer #56

shanoaice opened this issue Oct 20, 2021 · 0 comments

Comments

@shanoaice
Copy link

For example, I wanted to serialize the following records:

type Ex1 = { Val1: int }
type Ex2 = { Nested: Ex1; Val2: int }
let data = { Nested = { Val1 = 1 }; Val2 = 2 }
let json = Json.serialize data

into such form:

{
	"val1": 1,
	"val2": 2
}

It seemed like that this is impossible to achieve with FSharp.Json for now.
Is it possible to have a [<Expand>] attribute to achieve such task?

I got this idea because I wanted to expand the result of serializing a DU with union case key / value mode, like so:

[<JsonUnion(Mode = UnionMode.CaseKeyAsFieldValue, CaseKeyField="casekey", CaseValueField="casevalue")>]
type DU = | Example of string
type ExRec = { Val: int; Union: DU }
let data = { Val = 1; Union = DU.Example "str" }
let json = Json.serialize data

into such form:

{
	"val": 1,
	"casekey": "Example",
	"casevalue": "str"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant