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 509689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:02:12+00:00 2026-05-13T07:02:12+00:00

I have a f# library with some values that I want to serialize in

  • 0

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.

For example, lets say this is the f# module and I want to serialize t1.

module Module1=

    type Tree = Leaf  | Branch of Tree * int * Tree

    let t1 = Leaf

In c# I do the following:

DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Module1.Tree));
StreamWriter writer = new StreamWriter(@"c:\test");
serializer.WriteObject(writer.BaseStream, Module1.t1);
writer.Close();

I’m having this error(in spanish because my visual studio is in spanish :S )

“No se espera el tipo ‘ns.Module1+Tree+_Leaf’ con el nombre de contrato de datos ‘Module1.Tree._Leaf:http://schemas.datacontract.org/2004/07/ns‘. 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).”

my translation:
“The type ‘ns.Module1+Tree+_Leaf’ was not expected with the data contract name ‘Module1.Tree._Leaf:http://schemas.datacontract.org/2004/07/ns‘. 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).”

Any ideas how to solve it?

  • 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-05-13T07:02:12+00:00Added an answer on May 13, 2026 at 7:02 am

    The problem is that, from the CLR perspective, the object referenced by t1 is not actually of type Module1.Tree but rather of an unrelated, nested type Module1.Tree+_Leaf. You need to inform the DataContractJsonSerializer that it may encounter objects of this type. Hopefully there is a helper method somewhere in the F# runtime to list such compiler-generated nested types; if not, you’ll have to use reflection, e.g.

    var serializer = new DataContractJsonSerializer (
        new List<Type> (typeof (Module1.Tree).GetNestedTypes ()) { // nested types
                        typeof (Module1.Tree),                     // root type
        }.ToArray ()) ;
    

    although I’d be a bit scared to write such code unless F# actually specifies exactly how it generates CLR types from algebraic types.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a library that I created with some business logic that includes writing
Suppose, I have an opensource project that depends on some library, that must be
I have a failure in some inner library function in Octave. I want to
I have a graph library that I use to plot some data. This data
I have a Python library that, in addition to regular Python modules, has some
I'd like to use the MSHTML library to parse some HTML that I have
I have read in some CSV values, line by line, that have the following
I have a query in Django that is resulting in unique rows but some
I have some code that is producing a Map where the values are Option
I have a library of objects that I want to load on the fly

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.