mirror of
https://github.com/json-iterator/go.git
synced 2025-04-14 11:18:49 +02:00
fix html escape test and omit empty
This commit is contained in:
parent
8367a97ad8
commit
945fe53724
@ -113,12 +113,25 @@ func (cfg *frozenConfig) marshalFloatWith6Digits() {
|
|||||||
}})
|
}})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type htmlEscapedStringEncoder struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *htmlEscapedStringEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
|
str := *((*string)(ptr))
|
||||||
|
stream.WriteStringWithHtmlEscaped(str)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *htmlEscapedStringEncoder) encodeInterface(val interface{}, stream *Stream) {
|
||||||
|
writeToStream(val, stream, encoder)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *htmlEscapedStringEncoder) isEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
return *((*string)(ptr)) == ""
|
||||||
|
}
|
||||||
|
|
||||||
func (cfg *frozenConfig) escapeHtml() {
|
func (cfg *frozenConfig) escapeHtml() {
|
||||||
// for better performance
|
// for better performance
|
||||||
cfg.addEncoderToCache(reflect.TypeOf((*string)(nil)).Elem(), &funcEncoder{func(ptr unsafe.Pointer, stream *Stream) {
|
cfg.addEncoderToCache(reflect.TypeOf((*string)(nil)).Elem(), &htmlEscapedStringEncoder{})
|
||||||
val := *((*string)(ptr))
|
|
||||||
stream.WriteStringWithHtmlEscaped(val)
|
|
||||||
}})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cfg *frozenConfig) addDecoderToCache(cacheKey reflect.Type, decoder Decoder) {
|
func (cfg *frozenConfig) addDecoderToCache(cacheKey reflect.Type, decoder Decoder) {
|
||||||
|
@ -18,8 +18,8 @@ func (codec *stringCodec) encode(ptr unsafe.Pointer, stream *Stream) {
|
|||||||
stream.WriteString(str)
|
stream.WriteString(str)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (encoder *stringCodec) encodeInterface(val interface{}, stream *Stream) {
|
func (codec *stringCodec) encodeInterface(val interface{}, stream *Stream) {
|
||||||
writeToStream(val, stream, encoder)
|
writeToStream(val, stream, codec)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (codec *stringCodec) isEmpty(ptr unsafe.Pointer) bool {
|
func (codec *stringCodec) isEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
@ -118,6 +118,7 @@ func Test_string_encode_with_std(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_string_encode_with_std_without_html_escape(t *testing.T) {
|
func Test_string_encode_with_std_without_html_escape(t *testing.T) {
|
||||||
|
api := Config{EscapeHtml: false}.Froze()
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
for i := 0; i < utf8.RuneSelf; i++ {
|
for i := 0; i < utf8.RuneSelf; i++ {
|
||||||
input := string([]byte{byte(i)})
|
input := string([]byte{byte(i)})
|
||||||
@ -128,7 +129,7 @@ func Test_string_encode_with_std_without_html_escape(t *testing.T) {
|
|||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
stdOutput := buf.String()
|
stdOutput := buf.String()
|
||||||
stdOutput = stdOutput[:len(stdOutput)-1]
|
stdOutput = stdOutput[:len(stdOutput)-1]
|
||||||
jsoniterOutputBytes, err := Marshal(input)
|
jsoniterOutputBytes, err := api.Marshal(input)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
jsoniterOutput := string(jsoniterOutputBytes)
|
jsoniterOutput := string(jsoniterOutputBytes)
|
||||||
should.Equal(stdOutput, jsoniterOutput)
|
should.Equal(stdOutput, jsoniterOutput)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user