Skip to content

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

Open

Description

@bartelink

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)

Activity

  1. deviousasti commented on Jul 24, 2023

    @deviousasti
    Member

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

  2. bartelink commented on Jul 24, 2023

    @bartelink
    ContributorAuthor

    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!

  3. changed the title [-]Feature: NewtonsoftJson: Upconvert from Case that should be a string[/-] [+]feat(NewtonsoftJson): Upconvert from Case that should be a string[/+] on Aug 8, 2023
  4. bartelink commented on Apr 25, 2024

    @bartelink
    ContributorAuthor

    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": [ "abc@def.com" ] }
                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

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