How to JSON serialize math vector type in F#?

Posted by The_Ghost on Stack Overflow See other posts from Stack Overflow or by The_Ghost
Published on 2010-05-20T21:14:44Z Indexed on 2010/05/21 0:40 UTC
Read the original article Hit count: 286

Filed under:
|
|
|

Hello!

I'm trying to serialize "vector" (Microsoft.FSharp.Math) type. And I get that error:

Exception Details: System.Runtime.Serialization.SerializationException: Type 'Microsoft.FSharp.Math.Instances+FloatNumerics@115' with data contract name 'Instances.FloatNumerics_x0040_115:http://schemas.datacontract.org/2004/07/Microsoft.FSharp.Math' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.

I have tried to put KnownType attribute and some other stuff, but nothing helps!

Could someone know the answer? This is the code I use:

// [< KnownType( typeof<vector> ) >]
type MyType = vector             

let public writeTest =
    let aaa = vector [1.1;2.2]
    let serializer = new DataContractJsonSerializer( typeof<MyType> )
    let writer = new StreamWriter( @"c:\test.txt" )
    serializer.WriteObject(writer.BaseStream, aaa)
    writer.Close()   

© Stack Overflow or respective owner

Related posts about F#

Related posts about serialization