summaryrefslogtreecommitdiff
path: root/compat
diff options
context:
space:
mode:
Diffstat (limited to 'compat')
-rw-r--r--compat/json/borrowed_decode_test.go5
-rw-r--r--compat/json/compat.go30
-rw-r--r--compat/json/compat_test.go7
3 files changed, 22 insertions, 20 deletions
diff --git a/compat/json/borrowed_decode_test.go b/compat/json/borrowed_decode_test.go
index 4b84718..4b9d0cb 100644
--- a/compat/json/borrowed_decode_test.go
+++ b/compat/json/borrowed_decode_test.go
@@ -10,7 +10,6 @@ import (
"errors"
"fmt"
"image"
- "io" // MODIFIED
"math"
"math/big"
"net"
@@ -19,8 +18,6 @@ import (
"strings"
"testing"
"time"
-
- "git.lukeshu.com/go/lowmemjson" // MODIFIED
)
type T struct {
@@ -1093,7 +1090,7 @@ func equalError(a, b error) bool {
func TestUnmarshal(t *testing.T) {
for i, tt := range unmarshalTests {
- scan := lowmemjson.ReEncoder{Out: io.Discard} // MODIFIED
+ var scan scanner
in := []byte(tt.in)
if err := checkValid(in, &scan); err != nil {
if !equalError(err, tt.err) {
diff --git a/compat/json/compat.go b/compat/json/compat.go
index 7145a86..570afc9 100644
--- a/compat/json/compat.go
+++ b/compat/json/compat.go
@@ -75,31 +75,32 @@ func HTMLEscape(dst *bytes.Buffer, src []byte) {
_, _ = formatter.Write(src)
}
+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
+}
+
func Compact(dst *bytes.Buffer, src []byte) error {
- formatter := &lowmemjson.ReEncoder{
+ return reencode(src, &lowmemjson.ReEncoder{
Out: dst,
Compact: true,
BackslashEscape: lowmemjson.EscapePreserve,
- }
- _, err := formatter.Write(src)
- return err
+ })
}
func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error {
- formatter := &lowmemjson.ReEncoder{
+ return reencode(src, &lowmemjson.ReEncoder{
Out: dst,
Indent: indent,
Prefix: prefix,
BackslashEscape: lowmemjson.EscapePreserve,
- }
- _, err := formatter.Write(src)
- if se, ok := err.(*lowmemjson.ReEncodeSyntaxError); ok {
- err = &SyntaxError{
- msg: se.Err.Error(),
- Offset: se.Offset + 1,
- }
- }
- return err
+ })
}
func Valid(data []byte) bool {
@@ -153,6 +154,7 @@ func (dec *Decoder) Decode(ptr any) error {
}
} else if _, isArgErr := terr.Err.(*lowmemjson.DecodeArgumentError); terr.Err != nil &&
!isArgErr &&
+ !errors.Is(terr.Err, lowmemjson.ErrDecodeNonEmptyInterface) &&
!errors.Is(terr.Err, strconv.ErrSyntax) &&
!errors.Is(terr.Err, strconv.ErrRange) {
err = terr.Err
diff --git a/compat/json/compat_test.go b/compat/json/compat_test.go
index 5a34d22..2cb1e87 100644
--- a/compat/json/compat_test.go
+++ b/compat/json/compat_test.go
@@ -6,6 +6,7 @@ package json
import (
"bytes"
+ "io"
"git.lukeshu.com/go/lowmemjson"
"git.lukeshu.com/go/lowmemjson/internal"
@@ -13,9 +14,11 @@ import (
var parseTag = internal.ParseTag
+type scanner = lowmemjson.ReEncoder
+
func checkValid(in []byte, scan *lowmemjson.ReEncoder) error {
- _, err := scan.Write(in)
- return err
+ scan.Out = io.Discard
+ return reencode(in, scan)
}
const (