Note that there are some explanatory texts on larger screens.

plurals
  1. POSerialize f# algebraic data types
    text
    copied!<p>I have a f# library with some values that I want to serialize in c# with json. With registers I had no problems but I'm having errors when I try to serialize algebraic data types.</p> <p>For example, lets say this is the f# module and I want to serialize t1.</p> <pre><code>module Module1= type Tree = Leaf | Branch of Tree * int * Tree let t1 = Leaf </code></pre> <p>In c# I do the following:</p> <pre><code>DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Module1.Tree)); StreamWriter writer = new StreamWriter(@"c:\test"); serializer.WriteObject(writer.BaseStream, Module1.t1); writer.Close(); </code></pre> <p>I'm having this error(in spanish because my visual studio is in spanish :S )</p> <p>"No se espera el tipo 'ns.Module1+Tree+_Leaf' con el nombre de contrato de datos 'Module1.Tree._Leaf:<a href="http://schemas.datacontract.org/2004/07/ns" rel="nofollow noreferrer">http://schemas.datacontract.org/2004/07/ns</a>'. Agregue los tipos no conocidos estáticamente a la lista de tipos conocidos (por ejemplo, usando el atributo KnownTypeAttribute o agregándolos a la lista de tipos conocidos que se pasa a DataContractSerializer)."</p> <p>my translation: "The type 'ns.Module1+Tree+_Leaf' was not expected with the data contract name 'Module1.Tree._Leaf:<a href="http://schemas.datacontract.org/2004/07/ns" rel="nofollow noreferrer">http://schemas.datacontract.org/2004/07/ns</a>'. Add the unknown types statically to the list of known types (for example, using the attribute KnownTypeAttribute or adding them to the list of known types that are passed to DataContractSerializer)."</p> <p>Any ideas how to solve it?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload