2

I have the following JSON string that I read with the System.Text.Json library and I want to deserialize:

{
  "name": "\ud092\u00d0"
}

When I use the JsonSerializer.Deserialize method it of course decodes the \uXXXX to the appropriate characters.

What I want is to disable this and get the string as it is in the original JSON string: \ud092\u00d0. How can I accomplish that?

Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
  • [This](https://stackoverflow.com/questions/67189964/system-text-json-jsonserializer-serialize-adds-u0022)? – ProgrammingLlama Jun 29 '22 at 09:27
  • No, as you can see it only prevents the escaping of the ```\``` character. I am asking about prevention of escaping of unicode-encoded characters like `\uXXXX`. Thanks for trying to help! – Nikolay Kostov Jun 29 '22 at 09:30
  • Serializing an object containing such as `new { Test = "今日は水曜日です。" }` produces the following JSON: `{"Text":"\u4ECA\u65E5\u306F\u6C34\u66DC\u65E5\u3067\u3059\u3002"}`. Applying the suggested encoder in the solution from [this answer](https://stackoverflow.com/a/67191492/3181933) produces the following JSON: `{"Text":"今日は水曜日です。"}`. What evidence do you have of such a limitation that you've described? – ProgrammingLlama Jun 29 '22 at 09:34
  • 1
    Depending on what you're after, you might also be looking for `JsonElement.GetRawText()` (e.g. `JsonSerializer.Deserialize(@"{ ""name"": ""\ud092\u00d0"" }").GetProperty("name").GetRawText()`). The exact way a string is escaped is not semantically relevant, of course, and should not *generally* be interesting. – Jeroen Mostert Jun 29 '22 at 09:43
  • @DiplomacyNotWar The question is about deserializing not serializing, so the dupe is not the right one. – Charlieface Jun 29 '22 at 10:43
  • Closed, so can't make answer, but you need to write a custom `JsonConverter` class. – Poul Bak Jun 29 '22 at 11:12
  • @Charlie I think it's best if OP clarify their question before it be reopened. Two people other than me (evidenced by the deleted answers) have also interpreted this to be about not escaping characters on serialization. I didn't fully grasp the meaning of the last line in the question, which is the only thing that maybe suggests it might be about deserialization. If OP can clarify what the question is about then I'll happily vote to reopen it as necessary. – ProgrammingLlama Jun 29 '22 at 11:35
  • @PoulBak If you want you could always edit your original answer and undelete it, although given it has upvotes then making such a complete change might be not the [so] way – Charlieface Jun 29 '22 at 11:37
  • Well, how to write a custom JsonConverter would probably also be a duplicate. – Poul Bak Jun 29 '22 at 11:44
  • 1
    @DiplomacyNotWar I just edited the question so it is clarified that I am talking about deserializing – Nikolay Kostov Jun 30 '22 at 08:14

0 Answers0