Serialization/De-Serialization of Fsharp types

173 views
Skip to first unread message

federico....@gmail.com

unread,
Aug 26, 2015, 1:00:41 AM8/26/15
to RavenDB - 2nd generation document database
Hi,

I am trying to use RavenDB from F#. I can successfully serialize and store F# Map and List types but not deserialize them correctly. Lists are deserialized to Arrays (which would be fine) but I cannot deserialize Maps. The latter only works if I use a .NET Dictionary. I can serialize and deserialize things correctly with Newtonsoft Json.NET, which is what I thought RavenDB was using for this, so I am a bit puzzled as to way it doesn't work.


Thanks in advance for your help,

Federico


Oren Eini (Ayende Rahien)

unread,
Aug 26, 2015, 1:03:01 AM8/26/15
to ravendb

Hibernating Rhinos Ltd  

Oren Eini l CEO Mobile: + 972-52-548-6969

Office: +972-4-622-7811 l Fax: +972-153-4-622-7811

 


--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

federico....@gmail.com

unread,
Aug 26, 2015, 1:53:42 PM8/26/15
to RavenDB - 2nd generation document database
This does not seem to work. I have tried to add the converters in Newtonsoft.Json.FSharp bu the following code


    let customiseSerialiser (s: Raven.Imports.Newtonsoft.Json.JsonSerializer) =
        s.Converters.Add(new Newtonsoft.Json.FSharp.MapConverter())

fails because "The type MapConverter is not compatible with the type Raven.Imports.Newtonsoft.Json.JsonConverter". I tried upcasting it but to no avail. Is it possible that the problem is that RavenDB and Newtonsoft.Json.FSharp are referring to two different, incompatible, versions of Newtonsoft.Json?

As a side not I am observing this weird behavior (RavenDB build is 3690). I have the following code


[<CLIMutable>]
type WithMap = {
    Name: string
    Address: Map<string, string> 
}

let testQuery1 (store: Document.DocumentStore) = 
    use session = store.OpenSession("Test")
    let temp = {Name = "foo"; Address = Map.ofList [ ("1", "one"); ("2", "two"); ("3", "three") ]}
    session.Store(temp)
    session.SaveChanges()
    query {
        for record in session.Query<WithMap>() do
        select record
    }
    |> Seq.toArray

let testQuery2 (store: Document.DocumentStore) = 
    use session = store.OpenSession("Test")
    query {
        for record in session.Query<WithMap>() do
        select record
    }
    |> Seq.toArray
    
Now testQuery1 succeeds (provided that there are no other documents of WithMap type in the collection), I can insert the record in the db, fetch it back and it deserializes correctly. Howerver if I try testQuery2 it fails with the exception 


System.InvalidOperationException was unhandled
Message: An unhandled exception of type 'System.InvalidOperationException' occurred in Raven.Client.Lightweight.dll
Additional information: Could not convert document WithMaps/193 to entity of type Program+WithMap

The only way to make testQuery2 work is to Change the Map in the WithMap type to be a Dictionary. Similarly, If the are other instances of WithMap in the database testQuery1 correctly inserts a new instance in the database but then the query expression fails with the same exception.

Thanks in advance,

Federico

Oren Eini (Ayende Rahien)

unread,
Aug 27, 2015, 1:43:03 AM8/27/15
to ravendb
We use an embedded version of json.net, you need to recompile the converter against our version

federico....@gmail.com

unread,
Aug 27, 2015, 6:03:16 PM8/27/15
to RavenDB - 2nd generation document database
Thank you very much for the help I'll try that. I still don't understand the weird behavior in my example though. Also which version of Json.NET does your embedded version correspond to? Starting from Json.NET 6.0.3 Fsharp types should be serialized/deserialized correctly out of the box.

Thanks again,

Federico

Oren Eini (Ayende Rahien)

unread,
Aug 28, 2015, 5:34:48 AM8/28/15
to ravendb
I think we are using 5.0 or something like that.
We'll update that in 3.5
Reply all
Reply to author
Forward
0 new messages