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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:12:23+00:00 2026-05-14T03:12:23+00:00

I would expect a Dictionary object of the form: var dict = new Dictionary<string,string>()

  • 0

I would expect a Dictionary object of the form:

var dict = new Dictionary<string,string>()
{
    {"blah", "bob"},
    {"blahagain", "bob"}
};

to serialize into JSON in the form of:

{ "blah": "bob", "blahagain": "bob" }

NOT

[ { "key": "blah", "value": "bob" }, { "key": "blahagain", "value": "bob"}]

What is the reason for what appears to be a monstrosity of a generic attempt at serializing collections?

The DataContractJsonSerializer uses the ISerializable interface to produce this thing. It seems to me as though somebody has taken the XML output from ISerializable and mangled this thing out of it.

Is there a way to override the default serialization used by .Net here? Could I just derive from Dictionary and override the Serialization methods?

Posting to hear of any caveats or suggestions people might have.

  • 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-14T03:12:24+00:00Added an answer on May 14, 2026 at 3:12 am

    Well, I resolved to sidestep the DataContract serialization for Dictionary objects.

    I create an two virtual methods in my root object.

    public virtual void prepareForSerialization();
    public virtual void postDeserialize();
    

    Then specify string DataMember class properties for each dictionary attribute of my classes. The strings become serialized and the Dictionary is no longer serialized directly.

    [DataMember]
    public string dictionaryString;
    public Dictionary<int, string> dict;
    

    Then, when my code calls serialize, it additionally first calls prepareForSerialization.

    public override void prepareForSerialization() { 
        base.prepareForSerialization();
    }
    
    public override void postDeSerialize() {
        base.postDeSerialize();
    }
    

    Derived classes with Dictionary members will then call my own serializer for the Dictionary.

    Note: this is a simple serialization that suits my purposes. YMMV.
    Javascript ticks credit to another stackoverfow post. Forget which one.

    /// <summary>
    /// Extension methods needed to implement Javascript dates for C#
    /// </summary>
    public static class MyExtensions{
        public static double JavascriptTicks(this DateTime dt) {
            DateTime d1 = new DateTime(1970, 1, 1);
            DateTime d2 = dt.ToUniversalTime();
            TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks);
            return ts.TotalMilliseconds;
        }        
    }
    /// <summary>
    /// Serialize a single value
    /// </summary>
    /// <param name="o">An object to serialize</param>
    /// <returns>A JSON string of the value</returns>
    if (o is string) {
        return string.Format("\"{0}\"", o);
    } else if (o is DateTime) {
        return string.Format("new Date({0})", ((DateTime)o).JavascriptTicks()); ;
    } else if(o.GetType().IsValueType) {
        return o.ToString();
    } else {
        //Here you want a check of the form if is IMySerializer, then call your prepare before
        //using the .Net one.
        DataContractJsonSerializer json = new DataContractJsonSerializer(o.GetType());
        using(MemoryStream ms = new MemoryStream())
        using (StreamReader sr = new StreamReader(ms)) {
            json.WriteObject(ms, o);
            ms.Position = 0;
            return sr.ReadToEnd();
        }
    }
    
    /// <summary>
    /// Serializes a List object into JSON
    /// </summary>
    /// <param name="list">The IList interface into the List</param>
    /// <returns>A JSON string of the list</returns>
    public string SerializeList(IList list) {
        string result = null;
        if (list != null) {
            IEnumerator it = list.GetEnumerator();
            StringBuilder sb = new StringBuilder();
            long len = list.Count;
            sb.Append("[");
            while (it.MoveNext()) {
                if (it.Current is IList) {
                    sb.Append(SerializeList((IList)it.Current));
                } else if (it.Current is IDictionary) {
                    sb.Append(SerializeDictionary((IDictionary)it.Current));
                } else {
                    sb.Append(SerializeValue(it.Current));
                }
                --len;
                if (len > 0) sb.Append(",");
            }
            sb.Append("]");
            result = sb.ToString();
        }
        return result;
    }
    /// <summary>
    /// Returns a stringified key of the object
    /// </summary>
    /// <param name="o">The key value</param>
    /// <returns></returns>
    public string SerializeKey(object o) {
        return string.Format("\"{0}\"", o); 
    }
    /// <summary>
    /// Serializes a dictionary into JSON
    /// </summary>
    /// <param name="dict">The IDictionary interface into the Dictionary</param>
    /// <returns>A JSON string of the Dictionary</returns>
    public string SerializeDictionary(IDictionary dict) {
        string result = null;
        if (dict != null) {
            IDictionaryEnumerator it = dict.GetEnumerator();
            StringBuilder sb = new StringBuilder();
            long len = dict.Count;
            sb.Append("{");
            while (it.MoveNext()) {
                sb.Append(SerializeKey(it.Key));
                sb.Append(":");
                if (it.Value is IList) {
                    sb.Append(SerializeList((IList)it.Value));
                } else if (it.Value is IDictionary) {
                    sb.Append(SerializeDictionary((IDictionary)it.Value));
                } else {
                    sb.Append(SerializeValue(it.Value));
                }
                --len;
                if (len > 0) sb.Append(",");
            }
            sb.Append("}");
            result = sb.ToString();
        }
        return result;
    }   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a Dictionary<string,object> like so: var foo = new Dictionary<string, object>();
I've got a form that is posting what is effectively an object dictionary to
Using any tools which you would expect to find on a nix system (in
For example, if I make a query like between(1,4,X)? I would expect something like
Would I expect to see any performance gain by building my native C++ Client
Using groovy, would you expect better performance in terms of speed and memory overhead
Haven't fired up reflector to look at the difference but would one expect to
I have a generic dictonary which is templated in the following manner: Dictionary<object, IList<ISomeInterface>>
Consider this short python list of dictionaries (first dictionary item is a string, second
I would expect this line of JavaScript: foo bar baz.match(/^(\s*\w+)+$/) to return something like:

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.