fsprojects / Fleece

Json mapper for F#
http://fsprojects.github.io/Fleece
Apache License 2.0
199 stars 31 forks source link

Feature request, add parameters for conditional ser/de #93

Open Swoorup opened 4 years ago

Swoorup commented 4 years ago

Is there a way to pass parameters to OfJson or ToJson properties?

I would like to conditionally switch between different versions of underlying JSON model based on a parameter for example revision, specially for DU types, where revisions are not part of the main type being serialized/deserialized.

wallymathieu commented 4 years ago

That's an interesting question. One way to do it would perhaps be to have ofJson and toJson separated from the types in order to be able to invoke the correct format for the different revisions. Another way would perhaps be to do some conditional compilation to generate two different types for the different revisions.

wallymathieu commented 4 years ago

Or if you can pass the revision as a type parameter?

Swoorup commented 4 years ago

The need mostly came from ES, you might have old events which are not updated to the latest revision. But still need to be upconvered for parsing. So possible I am thinking as a parameter to parseJson <string> (event.SchemaRevision, event.EventType)?

type EventWrite<'payload, 'metadata> =
  { Id: Guid                     // ID of the event
    StreamId: StreamId           // Id of the stream this event belongs to
    VersionNumber: VersionNumber // Sequence number of the event in a single stream
    SchemaRevision: int.         // Schema revision of the payload and metadata
    EventType: string            // Event Type
    Payload: 'payload            // Event data
    Timestamp: DateTime }        // Timestamp the event was generated