Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7741553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:03:37+00:00 2026-06-01T09:03:37+00:00

I need a function to convert types from a third-party library to IDictionary s

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T09:03:38+00:00Added an answer on June 1, 2026 at 9:03 am

    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")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that will convert a type to a string, for example:
I need to convert the function path::combine(path1, path2). Please help me if you have
I need a function that can return the difference between the below two dates
When importing numbers from a csv file, I need to convert them to floats
I need help with translating some code from VB to C#. Public Function ToBase36(ByVal
I need to get data from JSON, transferred by Ajax from the client. Basically
what can I do... I need to pass a matrix into the function? error
I need to get Json data from a C# web service. I know there
I need a java script function that converts the document object of the current
in function need key to encrypt string without mcrypt libraly in php function encrypt($str,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.