I have a hard time trying to customize the output of HttpClient
and JsonSerializer
when I send (or serialize) object with special characters. I get different size characters in the output.
Test case if very simple:
var test = new Test
{
A = "Zażółć gęślą jaźń"
};
var jsonString = JsonSerializer.Serialize(test);
public class Test
{
[JsonPropertyName("a")]
public string A { get; set; }
}
and PHP:
$data = array('a'=>'Zażółć gęślą jaźń');
$data = json_encode($data);
echo $data;
C# is returning:
{"a":"Za\u017C\u00F3\u0142\u0107 g\u0119\u015Bl\u0105 ja\u017A\u0144"}
PHP:
{"a":"Za\u017c\u00f3\u0142\u0107 g\u0119\u015bl\u0105 ja\u017a\u0144"}
In C# I get \u017C
, but in PHP I get \u017c
(capital C vs lower c).
I need to have the same output because I need to calculate checksum and hash from my request body.
I'm not even sure how to search for such an option and if this is possible to do (easily).
I've tried using JsonSerializerOptions
but without success.
EDIT
I've just done a quick test with Newtonsoft and the output is as desired:
string newtonsoftOutput = JsonConvert.SerializeObject(test , new JsonSerializerSettings
{
StringEscapeHandling = StringEscapeHandling.EscapeNonAscii,
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
So the question is: what options to change in Test.Json serializer and Httpclient to get the same result?