tile38/controller/json.go

22 lines
377 B
Go
Raw Normal View History

2016-03-05 02:08:16 +03:00
package controller
2016-09-11 17:49:48 +03:00
import (
"encoding/json"
"github.com/tidwall/cast"
)
2016-03-05 02:08:16 +03:00
func jsonString(s string) string {
for i := 0; i < len(s); i++ {
2016-09-11 17:49:48 +03:00
if s[i] < ' ' || s[i] == '\\' || s[i] == '"' || s[i] > 126 {
2016-03-05 02:08:16 +03:00
d, _ := json.Marshal(s)
return string(d)
}
}
2016-09-11 17:49:48 +03:00
b := make([]byte, len(s)+2)
b[0] = '"'
copy(b[1:], cast.ToBytes(s))
b[len(b)-1] = '"'
return cast.ToString(b)
2016-03-05 02:08:16 +03:00
}