I have a WCF REST POST that has JSON data as input,
IService.cs:
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
void ImportJSON(Stream request);
IService.svc:
public void ImportJSON(Stream request)
{
using (var reader = new StreamReader(request))
{
var resultStream = reader.ReadToEnd();
var model = JsonConvert.DeserializeObject<dynamic>(resultStream);
var resHeaders = model.Headers;
var resValues = model.Values;
string str = "";
string strValues = "";
foreach (var item in resHeaders)
{
str = str + item.Header + ";";
}
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine(str);
foreach (var item in resValues)
{
strValues = item.value + ";";
stringBuilder.AppendLine(strValues);
strValues = "";
}
System.Guid guid = Guid.NewGuid();
var path = HttpContext.Current.Server.MapPath("~/App_Data/tmp/" + guid.ToString() + ".csv");
using (StreamWriter writetext = new StreamWriter(path))
{
writetext.WriteLine(stringBuilder);
}
}
}
An exemple of JSON data:
{
"Headers": [{"Header":"CA"},{"Header":"PE"},{"Header":"AMOUNT"}],
"Values": [{"value":"toto;tata;titi"}, {"value":"A;2019;S100"}, {"value":"A;2019;S100"}, {"value":"A;2019;S300"},
{"value":"A;2013;S23"}, {"value":"A;2013;S200"}, {"value":"A;2013;S200"}, {"value":"A;2019;S100"}]
}
In fact, I don't use any classe : I use anonyme data directly, and I use dynamic on the Deserialization JSON,
I have my result as I wish : I get all the data from JSON then I saved all data in a txt/csv file, My result:
CA;PE;AMOUNT;
toto;tata;titi;
A;2019;S100;
A;2019;S100;
A;2019;S300;
A;2013;S23;
A;2013;S200;
A;2013;S200;
A;2019;S100;
Is my code is correct?
Are there any impact affect performance ?
And is there any problem if I have very long JSON data ?
And if there are any advise or feedback for my code.
Is my code is correct?
you'd better be confident it works as intended, lest it was off topic at CR@SE. Code/program/algorithm correctness is a topic of computer science - over at CS@SE they'd prompt you to try and prove your code correct. \$\endgroup\$