views:

765

answers:

1

I have a class that's currently inheriting from Dictionary and then adds a few first class member properties to it. Roughly:

public class Foo : Dictionary<string, string>
{
   public string Bar { get; set; }
   public string Baz { get; set; }
}

Upon serializing an instance of this object to JSON however, it appears that the serializer only emits the key/value pairings that I have stored within the Dictionary. Even if I apply DataMember attributes to the new 1st class properties, the JSON serializer doesn't appears to know what to do with these and instead just ignores them.

I'm assuming there's something fundamentally elementary that I'm missing, but scouring through code samples and docs on .net's JSON serializer, I've only found trivial examples that don't quite match what I'm doing. All our other classes that derive from some other base class don't appear to exhibit this problem, it's this one that derives from the Generic Dictionary in particular that is giving us fits.

[Edit] Short of moving a dictionary into Foo as a first class property, is there anyway to make this work? I'm assuming the hang up is that the serializer doesn't know what to "name" the dictionary to distinguish it from the other members?

+1  A: 

Perhaps a composition-base solution would be better in this instance:

using System;
using System.Collections.Generic;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Text;

class Program
{
    static void Main()
    {
     Foo foo = new Foo { Bar = "bar", Baz = "baz" };
     foo.Items.Add("first", "first");

     DataContractJsonSerializer serializer 
            = new DataContractJsonSerializer(typeof(Foo));

     using (MemoryStream ms = new MemoryStream())
     {
      serializer.WriteObject(ms, foo);
      Console.WriteLine(Encoding.Default.GetString(ms.ToArray()));
     }
    }
}

public class Foo
{
    public Dictionary<string, string> Items { get; set; }
    public string Bar { get; set; }
    public string Baz { get; set; }

    public Foo()
    {
     this.Items = new Dictionary<string, string>();
    }
}

Produces this output:

{"Bar":"bar","Baz":"baz","Items":[{"Key":"first","Value":"first"}]}

Would this solve your issue as a workaround?

Andrew Hare
Andrew you beat me to the punch, I was editing my original as you posted.. I'm hoping to not have to alter the Foo class as it's a legacy piece and is touched a ton, but if I can't serialize this thing across the wire, my hand might be forced.
bakasan
@bakasan instead of altering Foo itself, can you create a Data Transfer Object that is easier to serialize (like Andrew's example), and initialize it with Foo's data?
Gabe Moothart
@Gabe ooh, that's a brilliant idea, and in fact I could create a quick and dirty extension method to inline the conversion right before I go to serialization...all without touching the thousands of places Foo is used. Thanks guys! (and thank god for extension methods)
bakasan
Marking the combo of Gabe and Andrew's ideas as an ideal/viable approach.
bakasan