UTF-8 Encoding trouble and no view in snapshot

551 views
Skip to first unread message

Olle Jacobsen

unread,
Mar 12, 2012, 10:52:07 AM3/12/12
to servic...@googlegroups.com
First of all thanks for all you effort on ServiceStack!

/api/catches <- display a list of all the latest catches (fishing releated)
When I browse the Snapshot view in my browser it isn't able to resolve åäö ÅÄÖ (swedish) in the list. 
If I here switch to ?format=json the encoding of the characters is correct. So no big deal but still faulty.

/api/catches/1234 - Working example. 
But snapshow view is empty, only able to see the data as ?format=json, only for this service. Another servies that displays another entity is working fine. Is it the "catch" keyword that get's messy?

var response = Mapper.Map<CatchView, CatchViewResponse>(model);
return response;

This returns the correct encoding of characters.


/api/ catches /1234 - Non working example 
var response = Mapper.Map<CatchView, CatchViewResponse>(model);
return RequestContext.ToOptimizedResultUsingCache(CacheClient, model.Id, new TimeSpan(0,5,0), () => response);

This returns incorrect encoding of the characters åäö ÅÄÖ.

Demis Bellot

unread,
Mar 12, 2012, 11:32:17 AM3/12/12
to servic...@googlegroups.com
What text encoding are you trying to use? UTF-8?
--
- Demis


Olle Jacobsen

unread,
Mar 13, 2012, 2:07:39 AM3/13/12
to servic...@googlegroups.com
Yes I'm using utf-8. Hosting in IIS in same webapp as an existing mvc3 application.

Web.config
<globalization culture="sv-SE" uiCulture="sv" fileEncoding="utf-8" requestEncoding="utf-8" responseEncoding="utf-8" />

Fails
public override object OnGet(Story request)
{
return new {Text = "Olle är en ÖL ål", Tags = new string[] {"öl", "ål", "mål"}};
}

view json datasource
{"text":"Olle är en ÖL ål","tags":["öl","ål","mål"]}

?format=json
{"text":"Olle är en ÖL ål","tags":["öl","ål","mål"]} 

?format=xml
throws an error since string[] cannot be serialized.

Demis Bellot

unread,
Mar 13, 2012, 4:46:26 AM3/13/12
to servic...@googlegroups.com
You're problem is that you're trying to serialize an anonymous type which is in general discouraged by ServiceStack, and not supported by .NET's Xml DataContractSerializer that ServiceStack uses.

Cheers,

Olle Jacobsen

unread,
Mar 13, 2012, 5:04:32 AM3/13/12
to servic...@googlegroups.com
Thanks, but that was only an example.

This also returns the wrong "encoding" characters, tha chars in code are correct the json is messed up:

public class TestResponse
{
public string Text { get; set; }
public string[] Tags { get; set; }
}

public override object OnGet(Story request)
{
var test = new TestResponse() { Text = "Olle är en ÖL ål", Tags = new string[] { "ål", "öl" }};
return test;
}

view json datasource 
{"text":"Olle är en ÖL ål","tags":["ål","öl"]}


Thanks in advance!

Demis Bellot

unread,
Mar 13, 2012, 5:25:07 AM3/13/12
to servic...@googlegroups.com
ok the JSON comes across the wire fine, the page just needs to specify UTF-8 encoding by specifying this in the HTML page:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

You can verify this by saving the HTML and adding the meta tag in the saved file.

I'll add the meta tag in the default HTML json report template, in the meantime you can get ServiceStack to append ;charset=utf-8 to the ContentType for HTML with:

SetConfig(new EndpointHostConfig {
AppendUtf8CharsetOnContentTypes = new HashSet<string> { ContentType.Html },
});

This should tell the browser to interpret the response as UTF8 as well.

Cheers,

Olle Jacobsen

unread,
Mar 13, 2012, 7:33:36 AM3/13/12
to servic...@googlegroups.com
Thanks. Now the html view look fine but the ?format=json is wrong instead (it's fine before you add the Append... on SetConfig).

So we need to specify this for the ContentType.Json also:

SetConfig(new EndpointHostConfig()
{
AppendUtf8CharsetOnContentTypes = new HashSet<string> { ContentType.Html, ContentType.Json }
});

The csv output is still messed up though with the wrong characters. The csv format is not needed by me in this case but maybe you'll like to know.

Thanks for your support!

Olle Jacobsen

unread,
Mar 13, 2012, 7:45:21 AM3/13/12
to servic...@googlegroups.com
The error still exists if I choose to cache the response:

return RequestContext.ToOptimizedResultUsingCache(CacheClient, model.Id, new TimeSpan(0,5,0), () => response);

Instead of just returning the result directly

return response;

Thanks, Olle.


On Tuesday, March 13, 2012 10:25:07 AM UTC+1, mythz wrote:

leong chung ern

unread,
Dec 18, 2013, 1:17:10 AM12/18/13
to servic...@googlegroups.com
I know this discussion is old. However I could not find a solution to this same problem which I am facing recently. Has it been resolved? Or is there a workaround?
Reply all
Reply to author
Forward
0 new messages