summaryrefslogtreecommitdiff
path: root/compat
diff options
context:
space:
mode:
Diffstat (limited to 'compat')
-rw-r--r--compat/json/compat.go85
1 files changed, 48 insertions, 37 deletions
diff --git a/compat/json/compat.go b/compat/json/compat.go
index 570afc9..8d6ec35 100644
--- a/compat/json/compat.go
+++ b/compat/json/compat.go
@@ -37,17 +37,21 @@ type (
// Encode wrappers ///////////////////////////////////////////////////
+func convertEncodeError(err error) error {
+ if me, ok := err.(*lowmemjson.EncodeMethodError); ok {
+ err = &MarshalerError{
+ Type: me.Type,
+ Err: me.Err,
+ sourceFunc: me.SourceFunc,
+ }
+ }
+ return err
+}
+
func marshal(v any, formatter *lowmemjson.ReEncoder) ([]byte, error) {
var buf bytes.Buffer
formatter.Out = &buf
- if err := lowmemjson.Encode(formatter, v); err != nil {
- if me, ok := err.(*lowmemjson.EncodeMethodError); ok {
- err = &MarshalerError{
- Type: me.Type,
- Err: me.Err,
- sourceFunc: me.SourceFunc,
- }
- }
+ if err := convertEncodeError(lowmemjson.Encode(formatter, v)); err != nil {
return nil, err
}
return buf.Bytes(), nil
@@ -68,6 +72,16 @@ func Marshal(v any) ([]byte, error) {
// ReEncode wrappers /////////////////////////////////////////////////
+func convertReEncodeError(err error) error {
+ if se, ok := err.(*lowmemjson.ReEncodeSyntaxError); ok {
+ err = &SyntaxError{
+ msg: se.Err.Error(),
+ Offset: se.Offset + 1,
+ }
+ }
+ return err
+}
+
func HTMLEscape(dst *bytes.Buffer, src []byte) {
formatter := &lowmemjson.ReEncoder{
Out: dst,
@@ -77,13 +91,7 @@ func HTMLEscape(dst *bytes.Buffer, src []byte) {
func reencode(src []byte, formatter *lowmemjson.ReEncoder) error {
_, err := formatter.Write(src)
- if se, ok := err.(*lowmemjson.ReEncodeSyntaxError); ok {
- err = &SyntaxError{
- msg: se.Err.Error(),
- Offset: se.Offset + 1,
- }
- }
- return err
+ return convertReEncodeError(err)
}
func Compact(dst *bytes.Buffer, src []byte) error {
@@ -114,28 +122,7 @@ func Valid(data []byte) bool {
// Decode wrappers ///////////////////////////////////////////////////
-func Unmarshal(data []byte, ptr any) error {
- return NewDecoder(bytes.NewReader(data)).Decode(ptr)
-}
-
-type Decoder struct {
- *lowmemjson.Decoder
- buf *bufio.Reader
-}
-
-func NewDecoder(r io.Reader) *Decoder {
- br, ok := r.(*bufio.Reader)
- if !ok {
- br = bufio.NewReader(r)
- }
- return &Decoder{
- Decoder: lowmemjson.NewDecoder(br),
- buf: br,
- }
-}
-
-func (dec *Decoder) Decode(ptr any) error {
- err := dec.Decoder.Decode(ptr)
+func convertDecodeError(err error) error {
if derr, ok := err.(*lowmemjson.DecodeError); ok {
switch terr := derr.Err.(type) {
case *lowmemjson.DecodeSyntaxError:
@@ -172,6 +159,30 @@ func (dec *Decoder) Decode(ptr any) error {
return err
}
+func Unmarshal(data []byte, ptr any) error {
+ return convertDecodeError(lowmemjson.Decode(bytes.NewReader(data), ptr))
+}
+
+type Decoder struct {
+ *lowmemjson.Decoder
+ buf *bufio.Reader
+}
+
+func NewDecoder(r io.Reader) *Decoder {
+ br, ok := r.(*bufio.Reader)
+ if !ok {
+ br = bufio.NewReader(r)
+ }
+ return &Decoder{
+ Decoder: lowmemjson.NewDecoder(br),
+ buf: br,
+ }
+}
+
+func (dec *Decoder) Decode(ptr any) error {
+ return convertDecodeError(dec.Decoder.Decode(ptr))
+}
+
func (dec *Decoder) Buffered() io.Reader {
dat, _ := dec.buf.Peek(dec.buf.Buffered())
return bytes.NewReader(dat)