Change how an IDictionary is serialized in ServiceContract

58 Views Asked by At
class Foo
{
    public string Name { get; set; }
    public IDictionary<string, object> Attributes { get; set; }
}

[ServiceContract]
interface IFooService
{
    [OperationContract]
    [WebInvoke(
        Method = "GET",
        UriTemplate = "testfoo",
        ResponseFormat = WebMessageFormat.Json)]
    Foo GetTestFoo();
}

class FooService : IFooService
{
    Foo GetTestFoo()
    {
        var foo = new Foo { Name = "name" };
        foo.Attributes.Add("foo", "bar");

        var serialize = new JavaScriptSerializer().Serialize(foo);
        Debug.WriteLine(serialize); // <- good format

        return foo;
    }
}

Making a call to /testfoo in a Chrome rest client the Debug.WriteLine prints:

{"Name":"name","Attributes":{"foo":"bar"}}

But the actual response is formatted as such:

{"Name":"name","Attributes":[{"Key":"foo","Value":"bar"}]}

I prefer the first variant. How do I configure or control the serialization to achieve this?

0

There are 0 best solutions below