Skip to content

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

Description

@shanoaice

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"
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions