Skip to content

add WriteBytesAsString() #393

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion extension_tests/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func Test_customize_byte_array_encoder(t *testing.T) {
should := require.New(t)
jsoniter.RegisterTypeEncoderFunc("[]uint8", func(ptr unsafe.Pointer, stream *jsoniter.Stream) {
t := *((*[]byte)(ptr))
stream.WriteString(string(t))
stream.WriteBytesAsString(t)
}, nil)
//defer jsoniter.ConfigDefault.(*frozenConfig).cleanEncoders()
val := []byte("abc")
Expand Down
2 changes: 1 addition & 1 deletion reflect_struct_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (encoder *stringModeStringEncoder) Encode(ptr unsafe.Pointer, stream *Strea
tempStream := encoder.cfg.BorrowStream(nil)
defer encoder.cfg.ReturnStream(tempStream)
encoder.elemEncoder.Encode(ptr, tempStream)
stream.WriteString(string(tempStream.Buffer()))
stream.WriteBytesAsString(tempStream.Buffer())
}

func (encoder *stringModeStringEncoder) IsEmpty(ptr unsafe.Pointer) bool {
Expand Down
8 changes: 8 additions & 0 deletions stream_str.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package jsoniter

import (
"unicode/utf8"
"unsafe"
)

// htmlSafeSet holds the value true if the ASCII character with the given
Expand Down Expand Up @@ -307,6 +308,13 @@ func writeStringSlowPathWithHTMLEscaped(stream *Stream, i int, s string, valLen
stream.writeByte('"')
}

// WriteBytesAsString writes b to the stream (without html escapes) as if it
// were a string. (If you start out with a []byte, calling this instead of
// WriteString avoids an allocation.)
func (stream *Stream) WriteBytesAsString(b []byte) {
stream.WriteString(*(*string)(unsafe.Pointer(&b)))
}

// WriteString write string to stream without html escape
func (stream *Stream) WriteString(s string) {
valLen := len(s)
Expand Down