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

  • Home
  • SEARCH
  • 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 7875389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:58:27+00:00 2026-06-03T02:58:27+00:00

I’m planning to build a WCFservice that returns generic dictionary objects serialized to JSON.

  • 0

I’m planning to build a WCFservice that returns generic dictionary objects serialized to JSON. Unfortunately, serialization fails, as object is potentially always different. KnownTypes can’t help, because property type is Dictionary, and I can’t say KnownType, as the class will potentially be always different.

Any ideas if it’s possible to serialize an ‘unknown type’?

I don’t mind to specify DataContract/DataMember for each of my classes, but (at least for prototype version) I don’t want to have strong types for each and every response. Javascript client just doesn’t care.

How about anonymous classes?

  • 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-06-03T02:58:28+00:00Added an answer on June 3, 2026 at 2:58 am

    NOTE: I’ve gone into a lot of details about JavaScriptSerializer at the start of my answer, if you just want to read about the resolution to the known type problem mentioned in the original question, jump to the end of the answer.

    Performance

    Based on the benchmarks I ran, the JavaScriptSerializer is the far slower than the other alternatives and can take 2x as long to serialize/deserialize an object compared to DataContractSerializer.

    No need for Known Type

    That said, the JavascriptSerializer is more flexible in that it doesn’t require you to specify ‘known types’ ahead of time, and the serialized JSON is cleaner at least in the case of dictionaries (see examples here).

    The flip side of that flexibility around known types is that it won’t be able to deserialize that same JSON string back to the original type. For instance, suppose I have a simple Person class:

    public class Person
    {
        public string Name { get; set; }
    
        public int Age { get; set; }
    }
    

    And if I create an instance of Dictinoary<string, object> and add an instance of the Person class to it before serializing it:

    var dictionary = new Dictionary<string, object>();
    dictionary.Add("me", new Person { Name = "Yan", Age = 30 });
    var serializer = new new JavaScriptSerializer();
    var json = serializer .Serialize(dictionary);
    

    I’ll get the following JSON {"me":{"Name":"Yan","Age":30}} which is very clean but devoid of any type information. So supposed if you have two classes with the same member definitions or if Person is subclassed without introducing any additional members:

    public class Employee : Person
    {
    }
    

    then there’s simply no way for the serializer to be able to guarantee that the JSON {"Name":"Yan","Age":30} can be deserialized to the correct type.

    If you deserialize {"me":{"Name":"Yan","Age":30}} using the JavaScriptSerializer, in the dictionary you get back the value associated with “me” is NOT an instance of Person but a Dictionary<string, object> instead, a simple property bag.

    If you want to get a Person instance back, you could (though you most probably would never want to!) convert that Dictionary<string, object> using the ConvertToType helper method:

    var clone = serializer.Deserialize<Dictionary<string, object>>(json);
    var personClone = serializer.ConverToType<Person>(clone["me"]);
    

    On the other hand, if you don’t need to worry about deserializing those JSON into the correct type and JSON serailization is not a performance bottleneck (profile your code and find out how much CPU time’s spent on serialization if you haven’t done so already) then I’d say just use JavaScriptSerializer.

    Injecting Known Type

    IF, at the end of the day, you do still need to use DataContractSerializer and need to inject those KnownTypes, here are two things you can try.

    1) Pass the array of known types to the DataContractSerializer constructor.

    2) Pass a subclass of DataContractResolver (with the means to locate the types of interest to you) to the DataContractSerializer constructor

    You can create a ‘known type registry’ of sorts that keeps track of the types that can be added to the dictionary, and if you control all the types that you’ll need to inject to the DataContractSerializer, you can try the simplest thing:

    1. Create a KnownTypeRegister class with static methods to add a type to the list of known types:

      public static class KnownTypeRegister
      {
          private static readonly ConcurrentBag _knownTypes = new ConcurrentBag();
          public static void Add(Type type)
          {
              _knownTypes.Add(type);
          }
          public static IEnumerable Get()
          {
              return _knownTypes.ToArray();
          }
      }
    2. Add a static constructor that registers the types with the register:

      [DataContract]
      public class Person
      {
          static Person()
          {
              KnownTypeRegister.Add(typeof(Person));
          }
          [DataMember]
          public string Name { get; set; }
          [DataMember]
          public int Age { get; set; }
      }
    3. Get the array of known types from the register when you construct the serializer:

    var serializer = new DataContractSerializer(typeof(Dictionary<string, object>), KnownTypeRegister.Get());

    More dynamic/better options are possible but they’re also more difficult to implement, if you want to read more about dynamic known type resolution, have a look at Juval Lowy’s MSDN article on the topic here.
    Also, this blog post by Carlos Figueira also goes into details on more advance techniques such as dynamically generating the types, well worth a read whilst you’re on the topic!

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
We're building an app, our first using Rails 3, and we're having to build
i got an object with contents of html markup in it, for example: string
I need a function that will clean a strings' special characters. I do NOT

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.