summaryrefslogtreecommitdiff
path: root/adapter_test.go
blob: 6c57ebc6aa9ee2533448da344f9f695fac2d7040 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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)
}