diff options
author | Luke Shumaker <lukeshu@datawire.io> | 2022-08-15 00:22:04 -0600 |
---|---|---|
committer | Luke Shumaker <lukeshu@datawire.io> | 2022-08-15 00:28:27 -0600 |
commit | 25d0b8365020ef4051dffd4f7704328402914613 (patch) | |
tree | c84662fd6431bd79559cfa9a7e3e2a7b75302867 /compat/json/adapter_test.go | |
parent | c11411d3591de78417f9ded2ca443c366f92367e (diff) |
mkdir -p compat/json && mv borrowed_*_test.go compat/json/ # [ci-skip]
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) +} |