diff options
Diffstat (limited to 'compat/json/adapter_test.go')
-rw-r--r-- | compat/json/adapter_test.go | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/compat/json/adapter_test.go b/compat/json/adapter_test.go new file mode 100644 index 0000000..6c57ebc --- /dev/null +++ b/compat/json/adapter_test.go @@ -0,0 +1,118 @@ +// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com> +// +// SPDX-License-Identifier: GPL-2.0-or-later + +package lowmemjson + +import ( + "bytes" + "encoding/json" + "io" +) + +func MarshalIndent(v any, prefix, indent string) ([]byte, error) { + var buf bytes.Buffer + formatter := &ReEncoder{ + Out: &buf, + Indent: indent, + prefix: prefix, + } + err := Encode(formatter, v) + return buf.Bytes(), err +} + +func Marshal(v any) ([]byte, error) { + var buf bytes.Buffer + formatter := &ReEncoder{ + Out: &buf, + Compact: true, + } + err := Encode(formatter, v) + return buf.Bytes(), err +} + +func HTMLEscape(dst *bytes.Buffer, src []byte) { + formatter := &ReEncoder{ + Out: dst, + } + _, _ = formatter.Write(src) +} + +func Compact(dst *bytes.Buffer, src []byte) error { + formatter := &ReEncoder{ + Out: dst, + Compact: true, + } + _, err := formatter.Write(src) + return err +} + +func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error { + formatter := &ReEncoder{ + Out: dst, + Indent: indent, + prefix: prefix, + } + _, err := formatter.Write(src) + return err +} + +func Valid(data []byte) bool { + formatter := &ReEncoder{ + Out: io.Discard, + Compact: true, + } + _, err := formatter.Write(data) + return err == nil +} + +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) +} + +//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 +) + +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) { + encodeString(&es.Buffer, str) +} +func (es *encodeState) stringBytes(str []byte, _ bool) { + encodeString(&es.Buffer, str) +} |