1
0
mirror of https://github.com/json-iterator/go.git synced 2025-06-24 23:16:47 +02:00

#53 support escapeHtml

This commit is contained in:
Tao Wen
2017-06-15 23:54:43 +08:00
parent d867c8ba5c
commit 5f22e50c89
30 changed files with 590 additions and 249 deletions

View File

@ -11,6 +11,7 @@ type Config struct {
IndentionStep int
MarshalFloatWith6Digits bool
SupportUnexportedStructFields bool
EscapeHtml bool
}
type frozenConfig struct {
@ -20,7 +21,12 @@ type frozenConfig struct {
extensions []ExtensionFunc
}
var DEFAULT_CONFIG = Config{}.Froze()
var ConfigOfDefault = Config{}.Froze()
// Trying to be 100% compatible with standard library behavior
var ConfigCompatibleWithStandardLibrary = Config{
EscapeHtml: true,
}.Froze()
func (cfg Config) Froze() *frozenConfig {
frozenConfig := &frozenConfig{
@ -34,16 +40,19 @@ func (cfg Config) Froze() *frozenConfig {
if cfg.SupportUnexportedStructFields {
frozenConfig.supportUnexportedStructFields()
}
if cfg.EscapeHtml {
frozenConfig.escapeHtml()
}
return frozenConfig
}
// RegisterExtension can register a custom extension
func (cfg *frozenConfig) RegisterExtension(extension ExtensionFunc) {
func (cfg *frozenConfig) registerExtension(extension ExtensionFunc) {
cfg.extensions = append(cfg.extensions, extension)
}
func (cfg *frozenConfig) supportUnexportedStructFields() {
cfg.RegisterExtension(func(type_ reflect.Type, field *reflect.StructField) ([]string, EncoderFunc, DecoderFunc) {
cfg.registerExtension(func(type_ reflect.Type, field *reflect.StructField) ([]string, EncoderFunc, DecoderFunc) {
return []string{field.Name}, nil, nil
})
}
@ -62,6 +71,14 @@ func (cfg *frozenConfig) marshalFloatWith6Digits() {
}})
}
func (cfg *frozenConfig) escapeHtml() {
// for better performance
cfg.addEncoderToCache(reflect.TypeOf((*string)(nil)).Elem(), &funcEncoder{func(ptr unsafe.Pointer, stream *Stream) {
val := *((*string)(ptr))
stream.WriteStringWithHtmlEscaped(val)
}})
}
func (cfg *frozenConfig) addDecoderToCache(cacheKey reflect.Type, decoder Decoder) {
done := false
for !done {