0

I'm trying to deserialize a Dictionary in a WCF Web Service.

The keys are unknown, and can not be hard typed.

This can be done with absolutely no problems using Newtonsoft Json Converter.

Can this be done using the default deserializer? or do I have to try and change the endpoint to use Newtonsoft?

The parameters is the part that doesn't deserialize properly. It results in an empty dictionary.

Example JSON

{
    "code":"test_code",
    "customer_id":123456,
    "parameters":{"param1":"test", "param2","test2"},
    "page":{"offset":0,"limit":0}
}

Thanks

Seige
  • 304
  • 5
  • 27
  • It's not easy. You want to set [`DataContractJsonSerializerSettings.UseSimpleDictionaryFormat`](https://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.usesimpledictionaryformat.aspx) but it's never exposed, so you'll need to replace the entire serializer. See https://stackoverflow.com/questions/6792785 or https://stackoverflow.com/questions/11003016. Possibly https://stackoverflow.com/questions/33554997 will be required also. If you just need to **return** a dictionary, see https://stackoverflow.com/questions/35490329 – dbc Aug 16 '16 at 22:26
  • So it would just be as easy/difficult to change the endpoint to use Newtonsoft? – Seige Aug 16 '16 at 22:47
  • It's no less difficult to switch to Json.NET than to enable `UseS‌impleDictionaryFormat‌​` since both involve completely replacing the serializer. The XML data contract serializer settings are exposed in WCF via `DataContractSerializerOperationBehavior`, see e.g. [Specifying Data Transfer in Service Contracts: Controlling the Serialization Process](https://msdn.microsoft.com/en-us/library/ms732038(v=vs.110).aspx#Anchor_8). But there seems to be no `IOperationBehavior` for the data contract **JSON** serializer and no way to set `UseSimpleDictionaryFormat` in the XML serializer behavior. – dbc Aug 16 '16 at 23:03

0 Answers0