Skip to content

Commit

Permalink
Allow the disabling of the newline in the encoder (#558)
Browse files Browse the repository at this point in the history
  • Loading branch information
elee1766 authored Nov 23, 2023
1 parent 3d13f33 commit b9a2d99
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 2 deletions.
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

0 comments on commit b9a2d99

Please sign in to comment.