2025-12-31 15:45:25 +00:00
|
|
|
package mining
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"sync"
|
|
|
|
|
)
|
|
|
|
|
|
2026-04-04 05:33:08 +00:00
|
|
|
// buffer := jsonBufferPool.Get().(*bytes.Buffer)
|
|
|
|
|
// buffer.Reset()
|
|
|
|
|
// defer jsonBufferPool.Put(buffer)
|
|
|
|
|
var jsonBufferPool = sync.Pool{
|
|
|
|
|
New: func() any {
|
2025-12-31 15:45:25 +00:00
|
|
|
return bytes.NewBuffer(make([]byte, 0, 1024))
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-04 05:33:08 +00:00
|
|
|
// buffer := acquireJSONBuffer()
|
|
|
|
|
// defer releaseJSONBuffer(buffer)
|
|
|
|
|
func acquireJSONBuffer() *bytes.Buffer {
|
|
|
|
|
buffer := jsonBufferPool.Get().(*bytes.Buffer)
|
|
|
|
|
buffer.Reset()
|
|
|
|
|
return buffer
|
2025-12-31 15:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
2026-04-04 05:33:08 +00:00
|
|
|
// releaseJSONBuffer(buffer) returns the buffer to the pool when it stays under 64 KB.
|
|
|
|
|
func releaseJSONBuffer(buffer *bytes.Buffer) {
|
|
|
|
|
if buffer.Cap() <= 65536 {
|
|
|
|
|
jsonBufferPool.Put(buffer)
|
2025-12-31 15:45:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-04 05:33:08 +00:00
|
|
|
// UnmarshalJSON(data, &message)
|
ax(batch): expand abbreviated parameter and local variable names across all packages
Applies AX principle 1 (Predictable Names Over Short Names) to function
signatures and local variables: s->input/raw, v->target/value, d->duration,
a,b->left,right, w->writer, r->reader, l->logger, p->part/databasePoint,
fn parameter names left as-is where they are callback conventions.
Co-Authored-By: Charon <charon@lethean.io>
2026-04-02 18:27:21 +01:00
|
|
|
func UnmarshalJSON(data []byte, target interface{}) error {
|
|
|
|
|
return json.Unmarshal(data, target)
|
2026-04-02 09:44:40 +01:00
|
|
|
}
|
|
|
|
|
|
2026-04-04 05:33:08 +00:00
|
|
|
// data, err := MarshalJSON(stats) // safe to keep after the call returns.
|
ax(batch): expand abbreviated parameter and local variable names across all packages
Applies AX principle 1 (Predictable Names Over Short Names) to function
signatures and local variables: s->input/raw, v->target/value, d->duration,
a,b->left,right, w->writer, r->reader, l->logger, p->part/databasePoint,
fn parameter names left as-is where they are callback conventions.
Co-Authored-By: Charon <charon@lethean.io>
2026-04-02 18:27:21 +01:00
|
|
|
func MarshalJSON(value interface{}) ([]byte, error) {
|
2026-04-04 05:33:08 +00:00
|
|
|
buffer := acquireJSONBuffer()
|
|
|
|
|
defer releaseJSONBuffer(buffer)
|
2025-12-31 15:45:25 +00:00
|
|
|
|
2026-04-04 05:33:08 +00:00
|
|
|
encoder := json.NewEncoder(buffer)
|
|
|
|
|
// Keep characters like < and > unchanged so API responses match json.Marshal.
|
2026-04-02 08:06:07 +01:00
|
|
|
encoder.SetEscapeHTML(false)
|
ax(batch): expand abbreviated parameter and local variable names across all packages
Applies AX principle 1 (Predictable Names Over Short Names) to function
signatures and local variables: s->input/raw, v->target/value, d->duration,
a,b->left,right, w->writer, r->reader, l->logger, p->part/databasePoint,
fn parameter names left as-is where they are callback conventions.
Co-Authored-By: Charon <charon@lethean.io>
2026-04-02 18:27:21 +01:00
|
|
|
if err := encoder.Encode(value); err != nil {
|
2025-12-31 15:45:25 +00:00
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-04 05:33:08 +00:00
|
|
|
// json.Encoder.Encode adds a newline; trim it so callers get compact JSON.
|
|
|
|
|
data := buffer.Bytes()
|
2025-12-31 15:45:25 +00:00
|
|
|
if len(data) > 0 && data[len(data)-1] == '\n' {
|
|
|
|
|
data = data[:len(data)-1]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result := make([]byte, len(data))
|
|
|
|
|
copy(result, data)
|
|
|
|
|
return result, nil
|
|
|
|
|
}
|