You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[<JsonFSharpConverter(UnionTagName ="tag")>]typeExUnion=| CaseOne ofstring| CaseTwo ofinttypeExRecord={ settings:ExUnion // and some other fields that are the same for all cases of settings, which will be unwise to pack them into ExUnion}letexVar={ settings = CaseOne "example"}
and I want the exVar to be serialized as follows, where the record field name indicates the field name of the case value, and another field that indicates the tag of the union:
{
"tag": "CaseOne",
"settings": "example"
}
with JsonUnionEncoding.AdjacentTag I was only able to get the follows:
and call JsonSerializer.Serialize() on instances of ExUnion instead of ExRecord.
But I still feel that such pattern is a bit counterintuitive, since I want to represent a field in a record whose type is annotated by another field in the same record, on the same level. Wrapping that with a DU under the record feels like a more natural way to do that. Even worse, this reverse-wrapping won't work if I want to achieve the follows:
[<JsonFSharpConverter(UnionTagName ="tag")>]typeExUnion1=| CaseOne ofstring| CaseTwo ofint[<JsonFSharpConverter(UnionTagName ="tag2")>]typeExUnion2=| A ofint| B ofstringtypeExRecord={ settings:ExUnion1
settings2:ExUnion2 }letexVar={ settings = CaseOne "example"; settings2 = A 8}
I have the following two types:
and I want the
exVar
to be serialized as follows, where the record field name indicates the field name of the case value, and another field that indicates the tag of the union:with
JsonUnionEncoding.AdjacentTag
I was only able to get the follows:And I can't figure out how to achieve what I want.
EDIT: I figured out how to do that:
and call
JsonSerializer.Serialize()
on instances of ExUnion instead of ExRecord.But I still feel that such pattern is a bit counterintuitive, since I want to represent a field in a record whose type is annotated by another field in the same record, on the same level. Wrapping that with a DU under the record feels like a more natural way to do that. Even worse, this reverse-wrapping won't work if I want to achieve the follows:
and serialize
exVar
into the follows:The text was updated successfully, but these errors were encountered: