mirror of
https://github.com/luxfi/rpc.git
synced 2026-10-30 15:19:52 +00:00
Modules declare go 1.27.1 and the build images follow. go fix applied the modernizers; omitzero is not applied, since it changes what the JSON says. Co-authored-by: Hanzo Dev <[email protected]>
47 lines
982 B
Go
47 lines
982 B
Go
// Copyright (C) 2019-2025, Lux Industries, Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package rpc
|
|
|
|
import (
|
|
"encoding/json"
|
|
)
|
|
|
|
// JSONCodec is a JSON-based codec
|
|
type JSONCodec struct{}
|
|
|
|
func (JSONCodec) Encode(v any) ([]byte, error) {
|
|
return json.Marshal(v)
|
|
}
|
|
|
|
func (JSONCodec) Decode(data []byte, v any) error {
|
|
return json.Unmarshal(data, v)
|
|
}
|
|
|
|
// defaultCodec is used when no codec is specified
|
|
var defaultCodec Codec = JSONCodec{}
|
|
|
|
// BinaryCodec passes bytes through unchanged (for pre-encoded data)
|
|
type BinaryCodec struct{}
|
|
|
|
func (BinaryCodec) Encode(v any) ([]byte, error) {
|
|
if b, ok := v.([]byte); ok {
|
|
return b, nil
|
|
}
|
|
if b, ok := v.(*[]byte); ok {
|
|
return *b, nil
|
|
}
|
|
return json.Marshal(v)
|
|
}
|
|
|
|
func (BinaryCodec) Decode(data []byte, v any) error {
|
|
if b, ok := v.(*[]byte); ok {
|
|
*b = data
|
|
return nil
|
|
}
|
|
return json.Unmarshal(data, v)
|
|
}
|
|
|
|
// Binary is a codec that passes bytes through unchanged
|
|
var Binary Codec = BinaryCodec{}
|