I'm new to WCF services with HTTP Routing, but my new project uses WCF. The services return objects marked with with [DataContract] and [DataMember] attributes returned as part of services marked with [ServiceContract] and [OperationContract] attributes
I want to return a class with a Dictionary<string, string> like this:
var myObject = new Dictionary<string, string> { { "aKey", "aValue" } };
I want it to JSON serialize like this:
{
"aKey": "aValue"
}
But it serializes like this:
[{
"Key": "aKey",
"Value": "aValue"
}]
How can I do that with WCF + HTTP?
- I can see this question/answer showing how to change JsonSerializeSettings with MVC / ASP.NET Core, but WCF is different, right?
I found many helpful answers, but they don't show me how to do it in the WCF/HTTP context so REST responses are automatically JSON serialized
- I see this question/answer uses
DataContractJsonSerializerSettings, specifically withUseSimpleDictionaryFormat = true- but this example writes to a
MemoryStream; where would I specify that in a WCF project sot he REST response is returned? in the Web.config? In Global.asax.cs?
- but this example writes to a
- This question/answer uses a custom
[JsonConverter]attribute- Let me try that; I assume I can try that using the attribute directly on my property
- There's also this question/answer which uses the
[JsonExtensionData]attribute- I can also try that; I assume I can try that using the attribute directly on my property
- What does the
[JsonDictionary]attribute do? It seems like it might be helpful here - And maybe this question/answer is exactly what I need...
- (Also for reference just linking this question on WCF services with HTTP routing more generally, because I'm new to WCF...)
- And other similar questions
EDIT
- Here is a duplicate question
- And see discussions here
- I have similar issues when I try to specify how to deserialize a C# Date object
- There may be some hope in the WCF Message Formatters post here
ISerializableand the second link shows how I can do it by writing to the response manually usingWebOperationContext.Current.CreateTextResponseISerializablein the way it would need for hte first suggestion. Thanks, I'm OK to close this question as duplicate or something, or if you answer I'll give you credit! Thanks,