Search code examples
jsondelphiasciiutf-16xsuperobject

Delphi XE7: How can I make XSuperObject output accented characters?


uses
  ...
  XSuperObject;

procedure TForm1.Button1Click(Sender: TObject);
var
  Json: ISuperObject;
begin
  Memo1.Lines.Add('{"Evênements":"Noël"}');
  Json := SO('{"Evênements":"Noël"}');
  Memo1.Lines.Add(Json.AsJSON(True));
end;

This code produces the following output in Memo1:

{"Evênements":"Noël"}
{
  "Ev\u00eanements":"No\u00ebl"
}

Why do I get \u00ea and \u00eb in the output and what should I do to get the correct accented characters?


Solution

  • Based on my reading of the code, the library provides no way to do what you seek. Strings are generated with StrToUTF16 in XSuperJSON.pas, and that function is coded to convert all non-ASCII characters to the backslash-escaped format observed in your question.

    The function technically produces UTF-16, but only because ASCII, extended to 16 bits per character, is a subset of UTF-16. I think you should file an issue with the project to request more typical JSON serialization.

    You could omit the ChrToUTF16 function entirely and be fine for nearly all inputs.

    As far as JSON is concerned, the output you get is fine. It's perfectly valid JSON, and the strings will be read back to yield the same values you started with. If you don't expect a human to read or edit this JSON manually, then you shouldn't worry about it.