From b9a2d9930914d5e0846584053b87376f585df44c Mon Sep 17 00:00:00 2001 From: a Date: Wed, 22 Nov 2023 20:34:22 -0600 Subject: [PATCH] Allow the disabling of the newline in the encoder (#558) --- api.go | 3 +++ encoder/encoder_amd64.go | 3 +++ encoder/encoder_compat.go | 13 +++++++++++++ internal/encoder/encoder.go | 14 ++++++++++++++ internal/encoder/stream.go | 8 ++++++-- sonic.go | 3 +++ 6 files changed, 42 insertions(+), 2 deletions(-) diff --git a/api.go b/api.go index 9525b7afd..ea73fe168 100644 --- a/api.go +++ b/api.go @@ -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 ( diff --git a/encoder/encoder_amd64.go b/encoder/encoder_amd64.go index e93b09a25..1cbef7b24 100644 --- a/encoder/encoder_amd64.go +++ b/encoder/encoder_amd64.go @@ -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 ) diff --git a/encoder/encoder_compat.go b/encoder/encoder_compat.go index 2e02b59cd..0727fee63 100644 --- a/encoder/encoder_compat.go +++ b/encoder/encoder_compat.go @@ -42,6 +42,7 @@ const ( bitNoNullSliceOrMap bitValidateString bitNoValidateJSONMarshaler + bitNoEncoderNewline // used for recursive compile bitPointerValue = 63 @@ -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 @@ -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 { diff --git a/internal/encoder/encoder.go b/internal/encoder/encoder.go index bd8bae357..0a46455eb 100644 --- a/internal/encoder/encoder.go +++ b/internal/encoder/encoder.go @@ -41,6 +41,7 @@ const ( bitNoNullSliceOrMap bitValidateString bitNoValidateJSONMarshaler + bitNoEncoderNewline // used for recursive compile bitPointerValue = 63 @@ -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 @@ -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 { diff --git a/internal/encoder/stream.go b/internal/encoder/stream.go index 8aa618c5a..d498f68fc 100644 --- a/internal/encoder/stream.go +++ b/internal/encoder/stream.go @@ -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) @@ -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: diff --git a/sonic.go b/sonic.go index 1da238895..91effec7e 100644 --- a/sonic.go +++ b/sonic.go @@ -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 {