I need a function to convert types from a third-party library to IDictionary
s so they can be easily serialized (to JSON). There are dependencies between the types so the dictionaries are sometimes nested.
Right now I have something hideous like this:
//Example type
type A(name) =
member __.Name = name
//Example type
type B(name, alist) =
member __.Name = name
member __.AList : A list = alist
let rec ToSerializable x =
match box x with
| :? A as a -> dict ["Name", box a.Name]
| :? B as b -> dict ["Name", box b.Name; "AList", box (List.map ToSerializable b.AList)]
| _ -> failwith "wrong type"
This would convert everything to a primitive type, an IEnumerable
of such a type, or a dictionary.
This function will keep growing as types are added (ugh). It's not type-safe (requiring the catch-all pattern). Figuring out which types are supported requires perusing the monolithic pattern match.
I'd love to be able to do this:
type ThirdPartyType with
member x.ToSerializable() = ...
let inline toSerializable x =
(^T : (member ToSerializable : unit -> IDictionary<string,obj>) x)
let x = ThirdPartyType() |> toSerializable //type extensions don't satisfy static member constraints
So, I'm looking for creativity here. Is there a better way to write this that addresses my complaints?
Here's one possible solution that addresses the type-safety question, though not necessarily your extensibility question:
// these types can appear in any assemblies
type A = { Name : string }
type B = { Name : string; AList : A list }
type C(name:string) =
member x.Name = name
static member Serialize(c:C) = dict ["Name", box c.Name]
// all of the following code goes in one place
open System.Collections.Generic
type SerializationFunctions = class end
let inline serializationHelper< ^s, ^t when (^s or ^t) : (static member Serialize : ^t -> IDictionary<string,obj>)> t =
((^s or ^t) : (static member Serialize : ^t -> IDictionary<string,obj>) t)
let inline serialize t = serializationHelper<SerializationFunctions,_> t
// overloads for each type that doesn't define its own Serialize method
type SerializationFunctions with
static member Serialize (a:A) = dict ["Name", box a.Name]
static member Serialize (b:B) = dict ["Name", box b.Name; "AList", box (List.map serialize b.AList)]
let d1 = serialize { A.Name = "a" }
let d2 = serialize { B.Name = "b"; AList = [{ A.Name = "a" }]}
let d3 = serialize (C "test")
x.Items |> Array.map (unbox >> serialize) |> box
. It seems there's no way to cast to ^t
where ^t requires member ToSerializable
- Daniel 2012-04-05 15:02
^t
must be statically known, so it doesn't make sense to do that at runtime. You could potentially use reflection instead - kvb 2012-04-05 15:30
as quick-n-obvious idea: use overloads
//Example type
type A(name) =
member __.Name = name
//Example type
type B(name, alist) =
member __.Name = name
member __.AList : A list = alist
type Converter =
static member ToSerializable(a : A) = dict ["Name", box a.Name]
static member ToSerializable(b : B) = dict ["Name", box b.Name; "AList", box (b.AList |> List.map Converter.ToSerializable)]