Google Groups

Re: [ServiceStack] UTF-8 Encoding trouble and no view in snapshot


Olle Jacobsen Mar 13, 2012 4:33 AM
Posted in group: ServiceStack .NET Open Source REST Web Services Framework
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!

On Tuesday, March 13, 2012 10:25:07 AM UTC+1, mythz wrote:
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,


On Tue, Mar 13, 2012 at 5:04 AM, Olle Jacobsen <ol...@brightcabin.se> wrote:
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!

On Tuesday, March 13, 2012 9:46:26 AM UTC+1, mythz wrote:
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,


On Tue, Mar 13, 2012 at 2:07 AM, Olle Jacobsen <ol...@brightcabin.se> wrote:
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.


On Monday, March 12, 2012 4:32:17 PM UTC+1, mythz wrote:
What text encoding are you trying to use? UTF-8?

On Mon, Mar 12, 2012 at 10:52 AM, Olle Jacobsen <ol...@brightcabin.se> wrote:
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





--
- Demis





--
- Demis



On Tuesday, March 13, 2012 10:25:07 AM UTC+1, mythz wrote:
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,


On Tue, Mar 13, 2012 at 5:04 AM, Olle Jacobsen <ol...@brightcabin.se> wrote:
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!

On Tuesday, March 13, 2012 9:46:26 AM UTC+1, mythz wrote:
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,


On Tue, Mar 13, 2012 at 2:07 AM, Olle Jacobsen <ol...@brightcabin.se> wrote:
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.


On Monday, March 12, 2012 4:32:17 PM UTC+1, mythz wrote:
What text encoding are you trying to use? UTF-8?

On Mon, Mar 12, 2012 at 10:52 AM, Olle Jacobsen <ol...@brightcabin.se> wrote:
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





--
- Demis





--
- Demis