I'm generating a JSON string from a class input. I don't know why but....It's generating different outputs (different projects), but with the same input (generated classes from Web Service Reference). i.e.:
1st project generates:
"{\"authToken\":{\"Token\":\"4f49f29e951d8d4f7e5b1f26aaf924771c9ed5fdfe6a23021d6720f2f5deead7==\",\"UserInfo\":{\"Email\":\"YS00982@mail.com\",\"Locked\":false,\"Phone\":null,\"UserLogin\":\"YS00982\",\"UserName\":\"YS00982 \"}},\"interactionModel\":{\"Description\":\"description\",\"Solicitor\":\"ry13578\",\"Title\":\"title\",\"Urgency\":\"3\"}}"
2nd project generates:
"{\r\n \"authToken\": {\r\n \"token\": \"4f49f29e951d8d4f7e5b1f26aaf924771c9ed5fdfe6a23021d6720f2f5deead7==\",\r\n \"userInfo\": {\r\n \"email\": \"YS00982@mail.com\",\r\n \"locked\": false,\r\n \"userLogin\": \"YS00982\",\r\n \"userName\": \"YS00982 \"\r\n }\r\n },\r\n \"interactionModel\": {\r\n \"description\": \"description\",\r\n \"solicitor\": \"ry13578\",\r\n \"title\": \"title\",\r\n \"urgency\": \"3\"\r\n }\r\n}"
There are two main differences: uppercase in the first character (token vs Token) and also "\r\n" added to my json.
AFAIK I didn't configure anything in the Newtonsoft, so I don't know why does it behave in different way in each project. Does anyone know the reason?
Sample code:
JsonConvert.DefaultSettings = () => new JsonSerializerSettings()
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
Formatting = Formatting.Indented,
NullValueHandling = NullValueHandling.Ignore,
};
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://ws.domain.com/BackEndService/");
StringContent c = new StringContent($"{{ \"userLogin\": \"YS00982\", \"password\": \"pass\"}}", Encoding.UTF8, "application/json");
HttpResponseMessage r = await client.PostAsync("service.svc/Authenticate", c);
string d = await r.Content.ReadAsStringAsync();
string token = string.Empty;
D authres = JsonConvert.DeserializeObject<D>(d);
if (authres.d.success)
{
AuthToken authToken = new AuthToken();
authToken = authres.d.Data;
var model = new InteractionModel
{
Description = "description",
Title = "title",
Solicitor = "ry13578",
Urgency = "3"
};
object obj = new
{
authToken = authToken,
interactionModel = model
};
string json = JsonConvert.SerializeObject(obj, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.None, ContractResolver = null });
Console.WriteLine(json);
StringContent cc = new StringContent(json, Encoding.UTF8, "application/json");
r = await client.PostAsync("CAUMobileService.svc/CreateInteraction", cc);
d = await r.Content.ReadAsStringAsync();
Console.WriteLine(d);
}
Best regards