Extending types in third-party library for serialization

让人想犯罪 __ 提交于 2019-12-11 08:27:12

问题


I need a function to convert types from a third-party library to IDictionarys 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?


回答1:


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")



回答2:


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)]


来源:https://stackoverflow.com/questions/10020242/extending-types-in-third-party-library-for-serialization

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!