Skip to content
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

Allow the disabling of the newline in the encoder #558

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ type Config struct {
// NoValidateJSONMarshaler indicates that the encoder should not validate the output string
// after encoding the JSONMarshaler to JSON.
NoValidateJSONMarshaler bool

// NoEncoderNewline indicates that the encoder should not add a newline after every message
NoEncoderNewline bool
}

var (
Expand Down
3 changes: 3 additions & 0 deletions encoder/encoder_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ const (
// after encoding the JSONMarshaler to JSON.
NoValidateJSONMarshaler Options = encoder.NoValidateJSONMarshaler

// NoEncoderNewline indicates that the encoder should not add a newline after every message
NoEncoderNewline Options = encoder.NoEncoderNewline

// CompatibleWithStd is used to be compatible with std encoder.
CompatibleWithStd Options = encoder.CompatibleWithStd
)
Expand Down
13 changes: 13 additions & 0 deletions encoder/encoder_compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const (
bitNoNullSliceOrMap
bitValidateString
bitNoValidateJSONMarshaler
bitNoEncoderNewline

// used for recursive compile
bitPointerValue = 63
Expand Down Expand Up @@ -77,6 +78,9 @@ const (
// NoValidateJSONMarshaler indicates that the encoder should not validate the output string
// after encoding the JSONMarshaler to JSON.
NoValidateJSONMarshaler Options = 1 << bitNoValidateJSONMarshaler

// NoEncoderNewline indicates that the encoder should not add a newline after every message
NoEncoderNewline Options = 1 << bitNoEncoderNewline

// CompatibleWithStd is used to be compatible with std encoder.
CompatibleWithStd Options = SortMapKeys | EscapeHTML | CompactMarshaler
Expand Down Expand Up @@ -130,6 +134,15 @@ func (self *Encoder) SetNoValidateJSONMarshaler(f bool) {
}
}

// SetNoEncoderNewline specifies if option NoEncoderNewline opens
func (self *Encoder) SetNoEncoderNewline(f bool) {
if f {
self.Opts |= NoEncoderNewline
} else {
self.Opts &= ^NoEncoderNewline
}
}

// SetCompactMarshaler specifies if option CompactMarshaler opens
func (self *Encoder) SetCompactMarshaler(f bool) {
if f {
Expand Down
14 changes: 14 additions & 0 deletions internal/encoder/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const (
bitNoNullSliceOrMap
bitValidateString
bitNoValidateJSONMarshaler
bitNoEncoderNewline

// used for recursive compile
bitPointerValue = 63
Expand Down Expand Up @@ -76,6 +77,9 @@ const (
// NoValidateJSONMarshaler indicates that the encoder should not validate the output string
// after encoding the JSONMarshaler to JSON.
NoValidateJSONMarshaler Options = 1 << bitNoValidateJSONMarshaler

// NoEncoderNewline indicates that the encoder should not add a newline after every message
NoEncoderNewline Options = 1 << bitNoEncoderNewline

// CompatibleWithStd is used to be compatible with std encoder.
CompatibleWithStd Options = SortMapKeys | EscapeHTML | CompactMarshaler
Expand Down Expand Up @@ -129,6 +133,16 @@ func (self *Encoder) SetNoValidateJSONMarshaler(f bool) {
}
}

// SetNoEncoderNewline specifies if option NoEncoderNewline opens
func (self *Encoder) SetNoEncoderNewline(f bool) {
if f {
self.Opts |= NoEncoderNewline
} else {
self.Opts &= ^NoEncoderNewline
}
}


// SetCompactMarshaler specifies if option CompactMarshaler opens
func (self *Encoder) SetCompactMarshaler(f bool) {
if f {
Expand Down
8 changes: 6 additions & 2 deletions internal/encoder/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ func (enc *StreamEncoder) Encode(val interface{}) (err error) {
}

// according to standard library, terminate each value with a newline...
buf.WriteByte('\n')
if enc.Opts & NoEncoderNewline == 0 {
buf.WriteByte('\n')
}

/* copy into io.Writer */
_, err = io.Copy(enc.w, buf)
Expand All @@ -76,7 +78,9 @@ func (enc *StreamEncoder) Encode(val interface{}) (err error) {
}

// according to standard library, terminate each value with a newline...
enc.w.Write([]byte{'\n'})
if enc.Opts & NoEncoderNewline == 0 {
enc.w.Write([]byte{'\n'})
}
}

free_bytes:
Expand Down
3 changes: 3 additions & 0 deletions sonic.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ func (cfg Config) Froze() API {
if cfg.NoValidateJSONMarshaler {
api.encoderOpts |= encoder.NoValidateJSONMarshaler
}
if cfg.NoEncoderNewline {
api.encoderOpts |= encoder.NoEncoderNewline
}

// configure decoder options:
if cfg.UseInt64 {
Expand Down