Deserializing C++ string in WCF service using Json converter
Deserializing C++ string in WCF service using Json converter
I am trying to communicate with C# wcf service which takes josn as an input where json contains image data, i wrote C# client where I am converting the object (InputData) which contains image as byte to json and sending it to server.
Client side:
InputData RequestData; //copied image data into RequestData obj
String jsonRequest = (new JavaScriptSerializer()).Serialize(RequestData); //converting obj to json
where :
public class InputData
{
private byte RawImage = null;
public byte FrontImage
{
get return this.FrontRawImage;
set if (null != value) { FrontRawImage = value.ToArray(); }
}
}
Server side:
//deserialize and get the object
InputData requestData = JsonConvert.DeserializeObject<InputData>(jsonData);
I am able to do this with C# client but the problem is, I have to communicate this WCF service from C++ client where I am converting image(jpg) data to string **(utf-8)**and then creating json and sending it to the service but on server side I am not able deserialze this json to InputData object (returning null with an exception unexpected characters).
//I am getting an exception on server side that says "unexpected character in the requested data"
InputData requestData = JsonConvert.DeserializeObject(jsonData);
I debug in the server side =>
json request data from c# client looks like
"{"Image":[92,34,92,34,36,73, 56.......huge image data here]}"
where [92,34,92,34,36,73, 56.......huge image data here] is actual image data in json.
but when i send same request data from c++ it looks like
"{"Image": ÉGÉìÉRÅ[ÉfÉBÉ.........data}"
so here I am getting an error while deserializing the json data using
InputData requestData = JsonConvert.DeserializeObject<InputData>(jsonData);
why am i not able to deserialize the data back to object here when request comes from c++ ?
could you please advise me a way to send this image data as string to the wcf service in C++ where the service could successfully deserialize the data back to the object.
is there a way that i can convert my image data something like this [92,34,92,34,........] in C++ so that that server can deserialize the data properly (I may be wrong).
Thanks in advance.
1 Answer
1
wcf use DataContract
in order to serialize. but you tried to serialize the input with newtonsoft
. if you want to serialize the input you can use this method:
DataContract
newtonsoft
public static string ToJSON<T>(this T obj) where T : class
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
using (MemoryStream stream = new MemoryStream())
{
serializer.WriteObject(stream, obj);
return Encoding.Default.GetString(stream.ToArray());
}
}
i strongly suggest in order to avoid this kind of problems use base64 instead of byte array.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Comments
Post a Comment