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

feat(NewtonsoftJson): Upconvert from Case that should be a string #97

Open
bartelink opened this issue Jul 23, 2023 · 3 comments
Open

Comments

@bartelink
Copy link
Collaborator

bartelink commented Jul 23, 2023

Provide an upconversion that lets you write the updated form per TypeSafeEnumConverter, but also accept mangled versions that predate impl of #96

i.e. read:

"items": [
        {
          "serviceId": "5c8795be52e34e82883d61babed19513",
          "serviceKind": {
            "Case": "ProductA"
          }
        },
        {
          "serviceId": "8a55ebbf0d404485b50da95bdb53f7b3",
          "serviceKind": {
            "Case": "ProductB"
          }
        }
      ]
    },

but write:

"items": [
                            {
                                "serviceId": "5c8795be52e34e82883d61babed19513",
                                "serviceKind": "ProductB"
                            },
                            {
                                "serviceId": "8a55ebbf0d404485b50da95bdb53f7b3",
                                "serviceKind": "ProductA"
                            }
                        ]

related: JamesNK/Newtonsoft.Json#1662 (comment)

@deviousasti
Copy link
Member

What's the use-case? Someone might have persisted this to their DB without checking what it actually serializes as?

@bartelink
Copy link
Collaborator Author

Exactly - DUs in particular are notorious; the standard encoding is sensitive to reordering of fields etc, and more, as mentioned in the linked json.net issue.

The most common thing IME is that people use random SCDUs and typesafe enums declared in common files in serialised structs. And then, once it's in the database, you're tied to that representation forever unless you're prepared to roundtrip e.g. all the events in your event store to 'fix' them, and life is short!

@bartelink bartelink changed the title Feature: NewtonsoftJson: Upconvert from Case that should be a string feat(NewtonsoftJson): Upconvert from Case that should be a string Aug 8, 2023
@bartelink
Copy link
Collaborator Author

Poor man's impl:

and ScduProxy = { Case: string; Fields: string[] }
and private EmailAddressNsjConverter() =
    inherit FsCodec.NewtonsoftJson.JsonIsomorphism<EmailAddress, string>()
    override this.Pickle(v) = v.ToString()
    override this.UnPickle(input) = input |> EmailAddress
    override this.Read(reader, serializer) =
        if reader.TokenType = Newtonsoft.Json.JsonToken.String then base.Read(reader, serializer)
        elif reader.TokenType = Newtonsoft.Json.JsonToken.StartObject then
            // UNPACK { "Case": "EmailAddress", "Fields": [ "[email protected]" ] }
            let p = serializer.Deserialize<ScduProxy>(reader)
            match p.Case, p.Fields with
            | "EmailAddress", [| x |] -> this.UnPickle x
            | _ -> failwith $"unexpected %A{p}"
        else failwith $"unexpected {reader.TokenType}"

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

2 participants