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.
Well, I resolved to sidestep the DataContract serialization for Dictionary objects.
I create an two virtual methods in my root object.
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.
Then, when my code calls serialize, it additionally first calls prepareForSerialization.
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.