JSON string serialization to match WCF service function parameter

I ran into the problem of serializing an object in JSON to match the name of the WCF function call parameter. The problem is to match the parameter name, i.e. the incoming JSON string must have the initial value as the same name as the parameter passed to the function, for example.

"{\"GetComplexDataResult\":{\"BoolValue\":true,\"StringValue\":\"Hello World!\"}}" 

This is my WCF function, which I call in my client, and, as you can see, the parameter name matches the name returned by "GetComplexDataResult"

 [OperationContract] [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)] String SaveData(CompositeType GetComplexDataResult); 

The problem that occurs when trying to serialize my object using Microsoft System.Web.Script.Serialization.JavaScriptSerializer or any other library (for example, Json.NET)

it returns only {\"BoolValue\":true,\"StringValue\":\"Hello World!\"} , even if I pass an object of the same class "CompositeType" (this is client-side code), for example.

 CompositeType GetComplexDataResult= new CompositeType(); GetComplexDataResult.BoolValue = true; GetComplexDataResult.StringValue = "Hello World"; JavaScriptSerializer serializer = new JavaScriptSerializer(); string json = serializer.Serialize(patchVersion); 

My question is: how can I get this JSON string

 "{\"GetComplexDataResult\":{\"BoolValue\":true,\"StringValue\":\"Hello World!\"}}" 

Instead

 {\"BoolValue\":true,\"StringValue\":\"Hello World!\"} 

just passing my object to a JSON parser. I can bind it manually after creating a JSON string, but that will be too much work. Is there any parser that solves this problem.

+6
source share
3 answers

If you serialize an anonymous object using the parameter name as the property name, it will include it in the json string. Try the following:

 string json = serializer.Serialize(new { GetComplexDataResult = patchVersion}); 

Also, if you don't care if the parameter name is included in JSON, you can set BodyStyle to BodyStyle = WebMessageBodyStyle.Bare .

+3
source

My question is, is there a significant reason why you need to have \"GetComplexDataResult\" at the beginning of json data?

I do not believe json serializers will do what you want from them, initially you have to deal with it yourself.

What we do in the company I work for, we create our own response wrapper, which makes all our answers the same for all our json services. So to a large extent, we are making a standard datacontract that returns a datacontract for one of the outter shell properties. You can do something similar to get the required nesting functionality.

Here is an example:

 [DataContract] public class ServiceResult<T> { [DataMember] public T GetComplexDataResult{ get; set; } } 

UPDATE . Changed it as universal. I also wanted to explain where I was going with my original answer. Again, the harsher answer is more concise, just keep in mind if you want to expand, for example add messaging, you will need to do something similar or manually do it yourself. I really like this approach because I know that my format will always be the same no matter which of our WCF services I call.

+2
source

For every JSON parser I've seen, an object type is never included in JSON. Serialization is an instance of this object. You would be better off adding the type attribute if you need to know the reference to the source type of the object. {\"BoolValue\":true,\"StringValue\":\"Hello World!\",\"type\":\"GetComplexDataResult\"} .

Your result clearly reminds me of a SOAP envelope. You do not need encapsulation - it just complicates the situation. JSON is simple - simple.

+1
source

Source: https://habr.com/ru/post/903593/


All Articles