Hexilee/roa

write_json should have a utf-8 charset attached

c5soft opened this issue · 7 comments

to support worldwide unicode charactor json uncoding/parsing
//body.rs
lazy_static! {
static ref APPLICATION_JSON: HeaderValue =
HeaderValue::from_static("application/json");
...
}

should be changed to:

lazy_static! {
static ref APPLICATION_JSON: HeaderValue =
HeaderValue::from_static("application/json; charset=utf-8");
...
}

I think it's unnecessary as write_json is just a lump of sugar. Maybe adding a method write_json_utf8 is better, you are welcome to create a PR to do it.

thank u for your response so quickly, this makes roa on the way to the best rust web crate. i don't think write_json_utf8 is a good ideal, it is boring to write ..._utf8 every time. May be the best solution, is introducing a way to set default charset.

Sounds reasonable, as the string in rust is encoded in UTF-8, we should set it as the default encoding.
So just change

lazy_static! {
	static ref APPLICATION_JSON: HeaderValue =
	HeaderValue::from_static("application/json");
	...
}

To

lazy_static! {
	static ref APPLICATION_JSON: HeaderValue =
	HeaderValue::from_static("application/json; charset=utf-8");
	...
}

I will do it in several days, may you do it by yourself and create a PR.

write_json now has a utf-8 charset attached, but there is something wrong with roa-tokio. I will fix it and implement #12 in July, then a new release will be published.

You can use roa = { git="https://github.com/Hexilee/roa" } to apply this update right now.

不错!
不过最近我正在琢磨warp。您可能注意到了我在rustcc.cn上发表的一些浅见。
我是从nodejs转过来的rust菜鸟,是koa的重度使用者,对express不感冒。非常喜欢roa中许多独到的设计,比如state,通过Deref/DerefMut Trait的实现让ctx能够直接操作state的内容,这相当高明。我对roa的担忧主要在于roa依赖hyper,而hyper又依赖tokio, 而roa的用例是async-std写的,我对这两个异步库如何协作心里没底。

事实上 hyper 0.13 并不依赖 tokio 的任何实际组件,必须的只有两个 trait (tokio::io::{AsyncRead, AsyncWrite})。

另外,async-std 的所有组件保证能运行在任何异步运行时上(通过自己的事件循环或同步线程池),开销也没有太大问题。

roa v0.5.2 published, this issue will be closed