Skip to content

Commit

Permalink
feat: add JSON unmarshal support for StringBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
zishang520 committed Sep 29, 2024
1 parent 8eadb1b commit a30066b
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions types/buffer-ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a30066b

Please sign in to comment.