-
Notifications
You must be signed in to change notification settings - Fork 43
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
Unwrapping single-field record cases #56
Comments
I don't think it's possible with the current options. But this would be a useful feature! It's implemented in WebSharper's JSON library and I've definitely made use of it in the past. |
I think the implementing the writing might be something like: /// Extensions (hopefully) coming with .NET 5
/// (https://github.com/dotnet/runtime/issues/31274)
/// from https://github.com/jet/FsCodec/blob/aab8dce0240469351172c43e962aaaa8b57f111f/src/FsCodec.SystemTextJson/JsonSerializerElementExtensions.fs#L21
[<AutoOpen>]
module private JsonSerializerExtensions =
open System.Runtime.InteropServices
type private JsonSerializer with
static member SerializeToElement(value: 'T, [<Optional; DefaultParameterValue(null)>] ?options: JsonSerializerOptions) =
JsonSerializer.Deserialize<JsonElement>(ReadOnlySpan.op_Implicit(JsonSerializer.SerializeToUtf8Bytes(value, defaultArg options null)))
let writeExpandedSingleField (writer: Utf8JsonWriter) (case: Case) (value: obj) (options: JsonSerializerOptions) =
writer.WriteStartObject()
writer.WriteString(fsOptions.UnionTagName, case.Name)
let element = JsonSerializer.SerializeToElement((case.Dector value).[0], options)
match element.ValueKind with
| JsonValueKind.Object ->
for property in element.EnumerateObject() do property.WriteTo(writer)
| _ ->
writer.WritePropertyName(case.Fields.[0].Name)
element.WriteTo(writer)
writer.WriteEndObject() The trickiest part about implementing this enhancement for me is understanding how it should compose with the other options using the I'm not quite familiar enough with this library to suggest a design here, so if you can recommend how to integrate this with the (Alternatively, and maybe a different piece of work, perhaps the options could be modelled as a bunch of DU types where only the valid combinations are representable. And/or the |
I've started an implementation that reuses a chunk of the code for writing records, will create a draft PR soon.
As far as I can tell, it should be possible to use this option any time you have |
[#56] Unwrap single-field record cases
I'm looking to 'unwrap' a record so that it's properties are at the same level as the case field.
Is there some kinda combination I could set for
myOptions
which would give me the behaviour I'm testing for here?The text was updated successfully, but these errors were encountered: