question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Serializing and deserializing f# functions is not supported

See original GitHub issue

Today i tried to do make a model something like this

[<CLIMutable>]
type Product =
    {
        Id: int
        Name: string
        //PrinterKey: Expression<Func<Item,obj>>
        ImposerKey: obj -> obj
    }

When i deserialized it from database, below exception was thrown

Newtonsoft.Json.JsonSerializationException: 'Could not create an instance of type Microsoft.FSharp.Core.FSharpFunc`2[System.Object,System.Object]. Type is an interface or abstract class and cannot be instantiated. Path 'Products[0].ImposerKey', line 1, position 133.'

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:15 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
Zaid-Ajajcommented, Jul 20, 2018

I am assuming you solved your issue, if it happened to be the case that you still need to serialize or deserialize functions, then just use a byte[] as the data format to store the function with FsPickler for converting between the bytes and functions. This way you don’t need to integrate FsPickler with this library internally

1reaction
Zaid-Ajajcommented, Jul 13, 2018

Your business logic should be in your code, not in your storage system. The only reason you would store arbitrary filters inside the database, is to be able to update them by hand (without the application) which I don’t think you actually want.

You would put this kind of logic in place where you are reading the values from the database in a where query:

// order that contains any item with "CROSS DOCK" is invalid 
let orderHasInvalidItems (order : Order) = 
  Seq.forall (fun item -> item.Art.Contains("CROSS DOCK")) order.Items) 

// get all products of which the orders are valid (their items do not contain "CROSS DOCK") 
let getActiveProducts (db: ILiteDatabase) = 
  let products = db.GetCollection<Product> "products"
  products.where 
      <@ fun prod -> prod.Orders @> 
      (fun orders -> Seq.forall (fun order -> not (orderHasInvalidItems  order) orders) 

otherwise if you really needed to store F# filters, I suggest you use a simple embedded DSL that you can serialize to the database (then you will be doing what LiteDB is doing for you now):

type FilterExpr = 
  | NoFilter 
  | Equals of propName:string * value:obj 
  | LessThan of propName:string * value:float 
  | GreaterThan of propName:string * value:float 
  | Contains of propName:string * value:string 
  | And of left:FilterExpr * right:FilterExpr 
  | Or of left:FilterExpr * right:FilterExpr  
  | Not of expr:FilterExpr 
  | NestedFilter of propName:string * filter:FilterExpr 

You will also need to evaulate the expr yourself against dynamic record values from the database

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does System.Text.Json throw a ` ...
Your code is failing during serialization not deserialization because you are catching some inner exception and trying to serialize it with ...
Read more >
Serde serialize to not supported type
I'm implementing a serde Serializer Serializer in serde - Rust for a binary format that do not distinguish between signed and unsigned integer ......
Read more >
Asp.net identity System.NotSupportedException
Hi so im using the method FindUserById tfrom asp.net identity and i get this error ill attach the code ** Controller method ...
Read more >
BinaryFormatter serialization methods are obsolete and ...
Serialize and Deserialize methods on BinaryFormatter, Formatter, and IFormatter are now obsolete as warning. Additionally, BinaryFormatter  ...
Read more >
net 5.0 BinaryFormatter not supported - Help
I tried to upgrade my web api to .net 5.0 and get the exception 'BinaryFormatter serialization and deserialization are disabled within this application....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found