4

I am new to ServiceStack. It has feature to provide csv file for the data but I need to download it in UTF8 format because I am getting some special characters. I have tried this config setting.

SetConfig(new HostConfig
        {
            AppendUtf8CharsetOnContentTypes = new HashSet<string> { MimeTypes.Csv } 
        });

but no luck.. Please suggest what I am doing wrong.

JSHunjan
  • 387
  • 1
  • 5
  • 17

2 Answers2

3

ServiceStack Text serializers already serialize to UTF8 by default. The configuration below only appends the UTF8 suffix so the full Content-Type returned is text/csv; charset=utf-8:

SetConfig(new HostConfig {
    AppendUtf8CharsetOnContentTypes = new HashSet<string> { MimeTypes.Csv } 
});

It doesn't change what it's serialized to which is already UTF-8.

I've made a change that let you modify the UTF8Encoding used for the different Text Serializers in ServiceStack.Text in this commit.

This will let you specify to emit a UT8 BOM for the CSV Serializer with:

CsvSerializer.UTF8Encoding = UTF8Encoding(true);

This change is available from v4.0.37+ that's now available on MyGet.

mythz
  • 134,801
  • 25
  • 234
  • 373
  • Thanks for your reply mythz. I checked the file downloaded in Notepad++ and when I checked the encoding, it showed UTF-8 without BOM. When I modified that to UTF-8 only then the special characters were gone. Please let me know which is the setting I can change so that I can get the file in UTF-8 encoding only. – JSHunjan Feb 04 '15 at 05:04
  • Sounds like it is utf-8 without BOM/byte-order-mark. BOM is optional - http://stackoverflow.com/questions/2223882/whats-different-between-utf-8-and-utf-8-without-bom . You probably would have to customize the serializer to send the BOM sequence in the stream. – Raul Nohea Goodness Feb 11 '15 at 00:26
  • @RaulNoheaGoodness yes you're right but we can add BOM char in client side as described in below link https://stackoverflow.com/questions/17879198/adding-utf-8-bom-to-string-blob – M_Farahmand Sep 02 '19 at 15:45
1

For ServiceStack versions > 5.x.x you must change your code like this

CsvSerializer.UseEncoding = PclExport.Instance.GetUTF8Encoding(true);

By default it is using CsvSerializer.UseEncoding = PclExport.Instance.GetUTF8Encoding(false); // without BOM

labilbe
  • 3,218
  • 2
  • 24
  • 34