0

This isn't a duplicate of JSON and escaping characters - That doesn't answer this, because those code samples are in JavaScript - I need C#.

What C# method/library converts a bullet point () into \u2022? The same converter would convert a newline char into \n. Those are just 2 examples, but the overall solution I'm looking for is to pass in a string (containing a combination of ASCII and special chars), and it converts all that to the same ASCII, but with the special chars escaped. For example, I need the following string:

• 3 Ply 330 3/16in x 1/16in(#77)
• 25 ft Long X 22 in Wide
• 2022 (2) Beltwall Blk Standard 4in (102mm)

...converted to this:

\u2022 3 Ply 330 3/16in x 1/16in(#77)\n\u2022 25 ft Long X 22 in Wide\n\u2022 (2) Beltwall Blk Standard 4in (102mm)

...so it can become a valid JSON string value.

I have been down a dozen rabbit holes trying to find the answer to this, though I have no doubt it's something ridiculously simple.

halfer
  • 19,824
  • 17
  • 99
  • 186
HerrimanCoder
  • 6,835
  • 24
  • 78
  • 158
  • 4
    `"• 3 Ply"` is a valid json value, and so is `"\u2022 3 Ply"`. Forcing one over the other is pointless because they are equally valid. – GSerg Jan 18 '23 at 00:36
  • Does this answer your question? [JSON and escaping characters](https://stackoverflow.com/questions/4901133/json-and-escaping-characters) – GSerg Jan 18 '23 at 00:40
  • GSerg, I suppose it may be valid JSON but it blows up the Microsoft Dataverse odata. It must be escaped. I have a successful example in front of me, escaping as shown. I just don't know how to perform the escape. – HerrimanCoder Jan 18 '23 at 00:41
  • Does Microsoft Dataverse odata specify [what encoding](https://www.joelonsoftware.com/articles/Unicode.html) it wants to use? Are you providing the string in that encoding? – GSerg Jan 18 '23 at 00:42
  • It's just a string. When I send it encoded as shown, in Postman, it works. When I send it decoded as shown, it blows up. – HerrimanCoder Jan 18 '23 at 00:43
  • GSerg, the link you provided seems very promising, but the code samples are in js. Is there a C# equivalent someplace? I have access to Newtonsoft but I don't know what method in there would do what I need, if any. – HerrimanCoder Jan 18 '23 at 00:44
  • 2
    There is [no such thing](https://www.joelonsoftware.com/articles/Unicode.html) as just a string. In what encoding is that string? – GSerg Jan 18 '23 at 00:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/251228/discussion-between-herrimancoder-and-gserg). – HerrimanCoder Jan 18 '23 at 00:45
  • Obviously, you can do [this](https://stackoverflow.com/q/1615559/11683), but that would be solving [the X](https://meta.stackexchange.com/q/66377/147640) rather than the Y. The Y is to provide the data in the encoding the program expects. – GSerg Jan 18 '23 at 00:47
  • @GSerg unfortunately OP already rejected that "manually replace characters with escaped versions" - your original link has the same (albeit written in JavaScript) code as the last one... I guess OP will have to come up with something brand new themselves. – Alexei Levenkov Jan 18 '23 at 01:39
  • `System.Text.Json.JsonSerializer` does exactly what you want out of the box, see [fiddle](https://dotnetfiddle.net/O1Ynpm) – Sir Rufo Jan 18 '23 at 05:23
  • Thank you GSerg, the link you gave (https://stackoverflow.com/questions/1615559/convert-a-unicode-string-to-an-escaped-ascii-string) mostly worked but I had to add some messy newline replacements. It was neraly perfect. The accepted answer was a bit tidier, and perfect. – HerrimanCoder Jan 18 '23 at 16:22

1 Answers1

1

You need to set which characters are escaped. If you are using Newtonsoft (comments indicate you are) then by default it will only escape control characters (newlines, etc).

You can pass the option StringEscapeHandling.EscapeNonAscii to have it escape all possible characters.

 public string EncodeNonAsciiCharacters(string value) {
     return JsonConvert.SerializeObject(value, Newtonsoft.Json.Formatting.None,
        new JsonSerializerSettings { StringEscapeHandling = StringEscapeHandling.EscapeNonAscii }
     );
  }
HerrimanCoder
  • 6,835
  • 24
  • 78
  • 158
Wh1t3rabbit
  • 348
  • 7