summaryrefslogtreecommitdiff
path: root/compat/json/compat_test.go
blob: dac5e54f73833cf5e8a91c82948c6a7fe225f60c (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
// Copyright (C) 2022-2023  Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later

package json

import (
	"bytes"
	"io"

	"git.lukeshu.com/go/lowmemjson"
	"git.lukeshu.com/go/lowmemjson/internal"
)

var parseTag = internal.ParseTag

type scanner = lowmemjson.ReEncoder

func checkValid(in []byte, scan *lowmemjson.ReEncoder) error {
	scan.Out = io.Discard
	return reencode(in, scan)
}

func isValidNumber(s string) bool {
	var parser internal.Parser
	for _, r := range s {
		if t, _ := parser.HandleRune(r); !t.IsNumber() {
			return false
		}
	}
	if t, _ := parser.HandleEOF(); t == internal.RuneTypeError {
		return false
	}
	return true
}

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)
}