summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@datawire.io>2022-08-14 20:52:06 -0600
committerLuke Shumaker <lukeshu@datawire.io>2022-08-15 00:28:47 -0600
commit58b7df5e9f1c0d4858528f326440599620a8c1fb (patch)
treedcbe104b42ff22debe4adf767ff1fe69785aa394
parent25d0b8365020ef4051dffd4f7704328402914613 (diff)
Create a compat/json compatibility library
-rw-r--r--compat/json/borrowed_decode_test.go8
-rw-r--r--compat/json/borrowed_encode_test.go2
-rw-r--r--compat/json/borrowed_fuzz_test.go2
-rw-r--r--compat/json/borrowed_scanner_test.go2
-rw-r--r--compat/json/borrowed_tagkey_test.go2
-rw-r--r--compat/json/borrowed_tags_test.go2
-rw-r--r--compat/json/compat.go (renamed from compat/json/adapter_test.go)103
-rw-r--r--compat/json/compat_test.go37
-rw-r--r--decode.go20
-rw-r--r--internal/borrowed_tags.go (renamed from borrowed_tags.go)2
-rw-r--r--internal/export.go16
-rw-r--r--reencode.go6
-rw-r--r--test_export.go16
13 files changed, 142 insertions, 76 deletions
diff --git a/compat/json/borrowed_decode_test.go b/compat/json/borrowed_decode_test.go
index ac8594b..306f85e 100644
--- a/compat/json/borrowed_decode_test.go
+++ b/compat/json/borrowed_decode_test.go
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package lowmemjson // MODIFIED
+package json
import (
"bytes"
@@ -19,6 +19,8 @@ import (
"strings"
"testing"
"time"
+
+ "git.lukeshu.com/go/lowmemjson" // MODIFIED
)
type T struct {
@@ -1093,9 +1095,7 @@ func equalError(a, b error) bool {
func TestUnmarshal(t *testing.T) {
t.Skip() // TODO
for i, tt := range unmarshalTests {
- scan := &ReEncoder{
- Out: io.Discard,
- }
+ scan := lowmemjson.ReEncoder{Out: io.Discard} // MODIFIED
in := []byte(tt.in)
if _, err := scan.Write(in); err != nil {
if !equalError(err, tt.err) {
diff --git a/compat/json/borrowed_encode_test.go b/compat/json/borrowed_encode_test.go
index 9659910..bb7c9dc 100644
--- a/compat/json/borrowed_encode_test.go
+++ b/compat/json/borrowed_encode_test.go
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package lowmemjson // MODIFIED
+package json
import (
"bytes"
diff --git a/compat/json/borrowed_fuzz_test.go b/compat/json/borrowed_fuzz_test.go
index 21756a7..82e6ada 100644
--- a/compat/json/borrowed_fuzz_test.go
+++ b/compat/json/borrowed_fuzz_test.go
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package lowmemjson // MODIFIED
+package json
import (
"testing"
diff --git a/compat/json/borrowed_scanner_test.go b/compat/json/borrowed_scanner_test.go
index c5d67e6..4955405 100644
--- a/compat/json/borrowed_scanner_test.go
+++ b/compat/json/borrowed_scanner_test.go
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package lowmemjson // MODIFIED
+package json
import (
"bytes"
diff --git a/compat/json/borrowed_tagkey_test.go b/compat/json/borrowed_tagkey_test.go
index 2d4d2c0..6a2d612 100644
--- a/compat/json/borrowed_tagkey_test.go
+++ b/compat/json/borrowed_tagkey_test.go
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package lowmemjson // MODIFIED
+package json
import (
"testing"
diff --git a/compat/json/borrowed_tags_test.go b/compat/json/borrowed_tags_test.go
index 28d76e9..8ba8ddd 100644
--- a/compat/json/borrowed_tags_test.go
+++ b/compat/json/borrowed_tags_test.go
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package lowmemjson // MODIFIED
+package json
import (
"testing"
diff --git a/compat/json/adapter_test.go b/compat/json/compat.go
index 6c57ebc..78a9d5f 100644
--- a/compat/json/adapter_test.go
+++ b/compat/json/compat.go
@@ -2,44 +2,69 @@
//
// SPDX-License-Identifier: GPL-2.0-or-later
-package lowmemjson
+package json
import (
+ "bufio"
"bytes"
"encoding/json"
"io"
+
+ "git.lukeshu.com/go/lowmemjson"
)
+type (
+ Number = json.Number
+ Marshaler = json.Marshaler
+ RawMessage = json.RawMessage
+
+ // low-level decode errors
+ SyntaxError = lowmemjson.SyntaxError
+ UnmarshalFieldError = json.UnmarshalFieldError
+ UnmarshalTypeError = json.UnmarshalTypeError // lowmemjson.DecodeTypeError
+
+ // high-level decode errors
+ InvalidUnmarshalError = json.InvalidUnmarshalError // lowmemjson.DecodeArgumentError
+
+ // marshal errors
+ InvalidUTF8Error = json.InvalidUTF8Error
+ MarshalerError = json.MarshalerError
+ UnsupportedTypeError = json.UnsupportedTypeError
+ UnsupportedValueError = json.UnsupportedValueError
+)
+
+/////////////////////////////////////////////////////////////////////
+
func MarshalIndent(v any, prefix, indent string) ([]byte, error) {
var buf bytes.Buffer
- formatter := &ReEncoder{
+ formatter := &lowmemjson.ReEncoder{
Out: &buf,
Indent: indent,
- prefix: prefix,
+ Prefix: prefix,
}
- err := Encode(formatter, v)
+ err := lowmemjson.Encode(formatter, v)
return buf.Bytes(), err
}
func Marshal(v any) ([]byte, error) {
var buf bytes.Buffer
- formatter := &ReEncoder{
+ formatter := &lowmemjson.ReEncoder{
Out: &buf,
Compact: true,
}
- err := Encode(formatter, v)
+ err := lowmemjson.Encode(formatter, v)
return buf.Bytes(), err
}
func HTMLEscape(dst *bytes.Buffer, src []byte) {
- formatter := &ReEncoder{
+ formatter := &lowmemjson.ReEncoder{
Out: dst,
}
_, _ = formatter.Write(src)
}
func Compact(dst *bytes.Buffer, src []byte) error {
- formatter := &ReEncoder{
+ formatter := &lowmemjson.ReEncoder{
Out: dst,
Compact: true,
}
@@ -48,17 +73,17 @@ func Compact(dst *bytes.Buffer, src []byte) error {
}
func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error {
- formatter := &ReEncoder{
+ formatter := &lowmemjson.ReEncoder{
Out: dst,
Indent: indent,
- prefix: prefix,
+ Prefix: prefix,
}
_, err := formatter.Write(src)
return err
}
func Valid(data []byte) bool {
- formatter := &ReEncoder{
+ formatter := &lowmemjson.ReEncoder{
Out: io.Discard,
Compact: true,
}
@@ -67,52 +92,30 @@ func Valid(data []byte) bool {
}
func Unmarshal(data []byte, ptr any) error {
- return Decode(bytes.NewReader(data), ptr)
-}
-
-func init() {
- forceBufio = true
-}
-
-func (dec *Decoder) Buffered() io.Reader {
- dat, _ := dec.buf.Peek(dec.buf.Buffered())
- return bytes.NewReader(dat)
+ return lowmemjson.Decode(bytes.NewReader(data), ptr)
}
-//func (dec *Decoder) Token() (Token, error)
-
/////////////////////////////////////////////////////////////////////
-type (
- Number = json.Number
- Marshaler = json.Marshaler
- RawMessage = json.RawMessage
-
- UnsupportedValueError = json.UnsupportedValueError
- MarshalerError = json.MarshalerError
- UnmarshalTypeError = json.UnmarshalTypeError
-)
-
-const (
- startDetectingCyclesAfter = 1000
-)
+type Decoder struct {
+ *lowmemjson.Decoder
+ buf *bufio.Reader
+}
-func isSpace(c byte) bool {
- switch c {
- case 0x0020, 0x000A, 0x000D, 0x0009:
- return true
- default:
- return false
+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,
}
}
-type encodeState struct {
- bytes.Buffer
+func (dec *Decoder) Buffered() io.Reader {
+ dat, _ := dec.buf.Peek(dec.buf.Buffered())
+ return bytes.NewReader(dat)
}
-func (es *encodeState) string(str string, _ bool) {
- encodeString(&es.Buffer, str)
-}
-func (es *encodeState) stringBytes(str []byte, _ bool) {
- encodeString(&es.Buffer, str)
-}
+//func (dec *Decoder) Token() (Token, error)
diff --git a/compat/json/compat_test.go b/compat/json/compat_test.go
new file mode 100644
index 0000000..399ff02
--- /dev/null
+++ b/compat/json/compat_test.go
@@ -0,0 +1,37 @@
+// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
+//
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+package json
+
+import (
+ "bytes"
+
+ "git.lukeshu.com/go/lowmemjson/internal"
+)
+
+var parseTag = internal.ParseTag
+
+const (
+ startDetectingCyclesAfter = 1000
+)
+
+func isSpace(c byte) bool {
+ switch c {
+ case 0x0020, 0x000A, 0x000D, 0x0009:
+ return true
+ default:
+ return false
+ }
+}
+
+type encodeState struct {
+ bytes.Buffer
+}
+
+func (es *encodeState) string(str string, _ bool) {
+ internal.EncodeStringFromString(&es.Buffer, str)
+}
+func (es *encodeState) stringBytes(str []byte, _ bool) {
+ internal.EncodeStringFromBytes(&es.Buffer, str)
+}
diff --git a/decode.go b/decode.go
index 0769bc7..8426526 100644
--- a/decode.go
+++ b/decode.go
@@ -27,8 +27,7 @@ type runeBuffer interface {
}
type Decoder struct {
- io runeTypeScanner
- buf *bufio.Reader
+ io runeTypeScanner
// config
disallowUnknownFields bool
@@ -39,23 +38,18 @@ type Decoder struct {
stack []any
}
-var forceBufio bool // used by the tests to implement .Buffered()
-
func NewDecoder(r io.Reader) *Decoder {
rr, ok := r.(io.RuneReader)
- var ret Decoder
- if forceBufio || !ok {
+ if !ok {
rr = bufio.NewReader(r)
- if forceBufio {
- ret.buf = rr.(*bufio.Reader)
- }
}
- ret.io = &noWSRuneTypeScanner{
- inner: &runeTypeScannerImpl{
- inner: rr,
+ return &Decoder{
+ io: &noWSRuneTypeScanner{
+ inner: &runeTypeScannerImpl{
+ inner: rr,
+ },
},
}
- return &ret
}
func (dec *Decoder) DisallowUnknownFields() { dec.disallowUnknownFields = true }
diff --git a/borrowed_tags.go b/internal/borrowed_tags.go
index 07292b1..e2bb54a 100644
--- a/borrowed_tags.go
+++ b/internal/borrowed_tags.go
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package lowmemjson
+package internal // MODIFIED
import (
"strings"
diff --git a/internal/export.go b/internal/export.go
new file mode 100644
index 0000000..d8cf622
--- /dev/null
+++ b/internal/export.go
@@ -0,0 +1,16 @@
+// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
+//
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+package internal
+
+import (
+ "io"
+)
+
+var ParseTag = parseTag
+
+var (
+ EncodeStringFromBytes func(io.Writer, []byte)
+ EncodeStringFromString func(io.Writer, string)
+)
diff --git a/reencode.go b/reencode.go
index 856de99..6bd1e48 100644
--- a/reencode.go
+++ b/reencode.go
@@ -22,7 +22,7 @@ type ReEncoder struct {
Indent string
// String to put before indents, for testing-compat with
// encoding/json only.
- prefix string
+ Prefix string
// Returns whether a given character in a string should be
// backslash-escaped. The bool argument is whether it was
// \u-escaped in the input. This does not affect characters
@@ -275,8 +275,8 @@ func (enc *ReEncoder) emitNlIndent() error {
if err := enc.emitByte('\n'); err != nil {
return err
}
- if enc.prefix != "" {
- if err := enc.emit(io.WriteString(enc.Out, enc.prefix)); err != nil {
+ if enc.Prefix != "" {
+ if err := enc.emit(io.WriteString(enc.Out, enc.Prefix)); err != nil {
return err
}
}
diff --git a/test_export.go b/test_export.go
new file mode 100644
index 0000000..1f97a7a
--- /dev/null
+++ b/test_export.go
@@ -0,0 +1,16 @@
+// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
+//
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+package lowmemjson
+
+import (
+ "git.lukeshu.com/go/lowmemjson/internal"
+)
+
+func init() {
+ internal.EncodeStringFromString = encodeString[string]
+ internal.EncodeStringFromBytes = encodeString[[]byte]
+}
+
+var parseTag = internal.ParseTag