I have a Web API method returning a List<AttributeCollection>. AttributeCollection is kind of a list of, obviously, attributes with values for each attributes (in this case fetched by the CRM 2011 SDK).
Here is the JSON that the method returns:
[
[
{
"Key": "mobilephone",
"Value": "(430) 565-1212"
},
{
"Key": "firstname",
"Value": "John"
}
],
[
{
"Key": "mobilephone",
"Value": "(430) 565-1313"
},
{
"Key": "firstname",
"Value": "Mark"
}
]
]
Now, the first pair of brackets are a visual representation of the List, and then you have many pairs of brackets ([]) for each AttributeCollection.
I want to get rid of the first pair of brackets, replace it with a top level element name (ie: allAttributes) and then all items following.
I am overidding the WriteToStreamAsync method of JsonMediaTypeFormatter
public override System.Threading.Tasks.Task WriteToStreamAsync(Type type, object value, System.IO.Stream writeStream, HttpContent content, TransportContext transportContext)
{
if ((typeof(IEnumerable<AttributeCollection>).IsAssignableFrom(type)))
{
//anything I could do here ?
}
return base.WriteToStreamAsync(type, value, writeStream, content, transportContext);
}
I thought of manipulating the JSON string itself, but it doesn't seem accessible from there.
Any ideas ?
Thanks.