From a30066b08bce451cd61ee2368499bd5ba77e1558 Mon Sep 17 00:00:00 2001 From: luoyy Date: Sun, 29 Sep 2024 15:20:15 +0800 Subject: [PATCH] feat: add JSON unmarshal support for StringBuffer --- types/buffer-ext.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/types/buffer-ext.go b/types/buffer-ext.go index 6aaacec..cf4d3df 100644 --- a/types/buffer-ext.go +++ b/types/buffer-ext.go @@ -148,6 +148,22 @@ func (sb *StringBuffer) MarshalJSON() ([]byte, error) { return json.Marshal(sb.String()) } +// UnmarshalJSON decodes a JSON-encoded string into the StringBuffer. +func (sb *StringBuffer) UnmarshalJSON(data []byte) error { + if sb == nil { + return nil + } + + var str string + if err := json.Unmarshal(data, &str); err != nil { + return err + } + + // Clear and populate the buffer with the new string. + sb.Buffer = NewBufferString(str) + return nil +} + func NewStringBufferReader(r io.Reader) (BufferInterface, error) { b := NewStringBuffer(nil) _, err := b.ReadFrom(r)