Switch to faster json library go-json and sonic

Former-commit-id: ab9b4723f1dcf79fe5c073fff4d86f4f6969edfd
This commit is contained in:
Eric Coissac
2024-06-23 00:36:08 +02:00
parent 93f9dcb95f
commit c1f03cb1f6
9 changed files with 80 additions and 32 deletions

View File

@ -3,12 +3,13 @@ package obiutils
import (
"bufio"
"bytes"
"encoding/json"
"io"
"os"
"reflect"
"sync"
"github.com/bytedance/sonic"
"github.com/barkimedes/go-deepcopy"
)
@ -363,9 +364,12 @@ func AtomicCounter(initial ...int) func() int {
return nextCounter
}
// JsonMarshalByteBuffer marshals an interface into JSON format.
//
// It takes a bytes.Buffer as a buffer and an interface{} as i.
// Returns an error.
func JsonMarshalByteBuffer(buffer *bytes.Buffer, i interface{}) error {
encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(false)
encoder := sonic.ConfigDefault.NewEncoder(buffer)
err := encoder.Encode(i)
b := buffer.Bytes()
b = bytes.TrimRight(b, "\n")