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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:17:18+00:00 2026-05-13T09:17:18+00:00

I am using the WCF JSON serializer to produce JSON for use as return

  • 0

I am using the WCF JSON serializer to produce JSON for use as return data for the ASP.NET MVC framework. I am doing this because the built-in JsonAction does not provide any way to control the naming of public properties in the serialized JSON.

public override void ExecuteResult(ControllerContext context)
{
    ...

    if (Data != null)
    {
        var serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(Data.GetType());
        System.IO.MemoryStream ms = new System.IO.MemoryStream();

        serializer.WriteObject(ms, this.Data);

        response.Write(Encoding.Default.GetString(ms.ToArray()));
    }
}

In this example, I am using this with OpenFlashChart, so I set this.Data to a PieChart instance. It worked fine. Then, I set this.Data to an instance of Chart, and I got the following exception:

Type 'OpenFlashChart.Pie' with data contract name 'Pie:http://schemas.datacontract.org/2004/07/OpenFlashChart' is not expected. Consider using a DataContractResolver or 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.

Obviously, when I gave the serializer a PieChart element, it was able to infer that I needed the Pie class as well. Why when I provide it Chart<PieChart> is it no longer looking at the classes used by PieChart? Is there a way to get around this issue without having to annotate everything with KnownTypeAttributes?

  • 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-13T09:17:18+00:00Added an answer on May 13, 2026 at 9:17 am

    I’ve edited my previous answer because I’ve just realised the problem here.

    I think problem is because the type being serialized is generic, you have to add any generic parameter types to the DataContractJsonSerializer’s known types.

    If I’m right, then you can still do this without annotating.

    Firstly, call the constructor on DataContractJsonSerializer which accepts a Type and an IEnumerable<Type>.

    Then change how you construct the serializer to something like this:

    DataContractJsonSerializer serializer = null;
    Type dataType = Data.GetType();
    if(dataType.IsGenericType)
        serializer = new DataContractJsonSerializer(dataType, dataType.GetGenericArguments());
    else
        serializer = new DataContractJsonSerializer(dataType);
    

    That would be my next step.

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

Sidebar

Related Questions

I created a new wcf rest service using .net 4. I have created a
I have a WCF service hosted and I'm trying to use it within an
I'm developing some CRUD screens for retrieving/updating various items using .NET 3.5 and aspx
I configured traditional WCF Services using SOAP end points. In my client project I
i want to send a json string from a html webpage using javascript to
I have a WCF service implemented using the WebServiceHostFactory (REST). I'm calling a service
I am trying to host a WCF service that responds to incoming requests by
I have a complex JSON object that I want represent as C# class. I
Hi Lords of the Interwebs, I've got a Silverlight project that I'm just beginning
Sorry for the strange title. Here is my situation. I have a table of

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.