Character Encoding, REST and Firemonkey (XE6)

Character Encoding, REST and Firemonkey (XE6)

Have a bit of a character encoding issue. I have  a webbroker application that provides a JSON array (built using XSuperObject) to a Firemonkey mobile application. The problem I am having is that extended characters such as in 'Coppélia' , fine when added to the array, looks ok when added to Response.Content (either with or without UTF8 encoding), but always shows as 'Copp�lia' or if UTF8
decoded 'Copp?lia'.

the encoding is set on the response:

  Response.ContentType := 'application/json';
  Response.ContentEncoding :=  + 'application/json;charset=utf-8';

Any ideas?

Comments

  1. How do you return the content? If you assign the string to Response.Content it is considered UTF16. You need to do the encoding with TEncoding and return the bytes...

    ReplyDelete
  2. Marco Cantù Hello, i have the same problem, when i bind data visually how i can decode by TEncoding ?

    ReplyDelete
  3. Marco Cantù thanks Marco. Sometimes blinded by doing something new - I actually do that for my standard webbroker web apps, no idea why I didn't do it 'just' because I am using it to provide REST json data.

    However, on the FMX client how should it be decoded? UTF8ToString still leads leading BOM (#$FEFF).

    ReplyDelete
  4. In this code ResponseContent is a Unicode UTF16 string you want to return as UTF8 from a WebBroker application:

    var
      aStream: TMemoryStream;
      aStreamWriter: TStreamWriter;
    begin
      aStream := TMemoryStream.Create;
      aStreamWriter := TStreamWriter.Create (aStream, TEncoding.UTF8);
      try
        aStreamWriter.Write(ResponseContent);
        aStream.Position := 0;
        Response.ContentType := 'text/html; charset=utf-8';
        Response.ContentStream := aStream;
      finally
        aStreamWriter.Free;
      end;

    ReplyDelete
  5. Martijn Coppoolse many thanks for that info. I hadn't ever done anything with Content-Encoding before, but was trying out everything and anything - except teh correct solution :-)

    ReplyDelete
  6. I can't understand what is a reason of this problem :(

    ReplyDelete
  7. Merab Chikvaidze Apparently, you have a completely different problem, because Russel has a problem with the encoding in a WebBroker Response object. That has nothing to do with binding data visually. So if you want to get an answer to your question, I suggest you ask it in a separate post, with more information, such as:
    What is it you want to do? What have you tried? What did you expect, and what actually happened?

    ReplyDelete
  8. Russell Weetch Content-Encoding isn't often necessary; Content-Type is something I'm confronted with more often. And charsets have been bitches since day one.
    I yearn for the day that every string will be in utf-8...

    ReplyDelete
  9. Marco Cantù That is the very code I use. The problem is in the FMX client application.  I have tried a myriad of ways to convert the RESTResponse (from Content and RawBytes) but it always leaves the BOM at the start and the extended characters as or ?. So any ideas would be much appreciated.

    ReplyDelete
  10. The issue than is you should not return the BOM from the server. The BOM is used for files, but should not be used in a stream returned by an HTTP server. So my code is probably wrong, and you need to fix the code to avoid returning the BOM from the server.

    ReplyDelete
  11. Marco Cantù I have been using that code in my webbroker apps for quite some time, but I guess a web browser is expecting a file. It is fairly easy to remove that, but I'm still not having much luck in the client getting it back to show the characters correctly. What was meant to be a quick project is now becoming mammoth. You guys at Embarcadero really like to challenge us. Perhaps I'll reinstall Delphi 1.0 - that truly transformed development

    ReplyDelete
  12. Just for Info - a little procedure in System.JSON called TJSONByteReader.ConsumeBOM shows us the way :-)

    ReplyDelete

Post a Comment