c++ - Producing JSON from C#: WebMessageBodyStyle.Wrapped or WebMessageBodyStyle.Bare? -
i trying write c++ application, using c++ rest sdk lib, process json data produced c# application. c# program can produce json in "wrapped" or "bare" style.
using bodystyle = webmessagebodystyle.wrapped
, c# produces json following:
{"echo":"{\"firstname\":\"an'",\"number\":21,\"secondname\":\"pn\"}"}
using bodystyle = webmessagebodystyle.bare
, c# produces json this:
"{\"firstname\":\"an'",\"number\":21,\"secondname\":\"pn\"}"
how can program recognize type produced: wrapped or bare?
json standard format representing, , exchanging, data. not define terms wrapped or bare. not familiar c# , libraries encoding data json, can make guess based on samples provided.
if have control on c# application, code use bare only. see no advantage, in general, wrapped style. perhaps designed other c# client libraries.
the difference see in produced output structure of data. there no way absolutely certain, 2 samples can @ deserialized object , check if has attribute echo
. if does, use value of attribute , if doesn't use object as-is.
since haven't worked in c++ in on decade , not know json library using, give example in javascript (though using style may closer c++). here how 2 objects handled:
var data = json.parse(...); // '...' represents ever text if (data["echo"] !== undefined) { data = data["echo"]; } console.log("the first name is:", data["firstname"]);
here psuedo-code example valid java may more recognized , translated c++:
map<string, object> data = json.parse(...); // '...' represents ever text if (data.containskey("echo")) { data = (map)data.get("echo"); } system.out.println("the first name is: " + data.get("firstname"));
Comments
Post a Comment