git.lukeshu.com/go/lowmemjson@v0.3.9-0.20230723050957-72f6d13f6fb2/compat/json/testcompat_test.go (about)

     1  // Copyright (C) 2022-2023  Luke Shumaker <lukeshu@lukeshu.com>
     2  //
     3  // SPDX-License-Identifier: GPL-2.0-or-later
     4  
     5  package json
     6  
     7  import (
     8  	"bytes"
     9  	"encoding/json"
    10  	"io"
    11  	"reflect"
    12  	_ "unsafe"
    13  
    14  	"git.lukeshu.com/go/lowmemjson"
    15  	"git.lukeshu.com/go/lowmemjson/internal/jsonparse"
    16  	"git.lukeshu.com/go/lowmemjson/internal/jsonstring"
    17  	"git.lukeshu.com/go/lowmemjson/internal/jsonstruct"
    18  )
    19  
    20  var (
    21  	parseTag         = jsonstruct.ParseTag
    22  	clearCache       = jsonstruct.ClearCache
    23  	cachedTypeFields = jsonstruct.IndexStruct
    24  )
    25  
    26  type scanner = lowmemjson.ReEncoderConfig
    27  
    28  func checkValid(in []byte, scan *lowmemjson.ReEncoderConfig) error {
    29  	return reencode(io.Discard, in, *scan)
    30  }
    31  
    32  func isValidNumber(s string) bool {
    33  	var parser jsonparse.Parser
    34  	for _, r := range s {
    35  		if t, _ := parser.HandleRune(r, true); !t.IsNumber() {
    36  			return false
    37  		}
    38  	}
    39  	if t, _ := parser.HandleEOF(); t == jsonparse.RuneTypeError {
    40  		return false
    41  	}
    42  	return true
    43  }
    44  
    45  const (
    46  	startDetectingCyclesAfter = 1000
    47  )
    48  
    49  type encodeState struct {
    50  	bytes.Buffer
    51  }
    52  
    53  func (es *encodeState) string(str string, _ bool) {
    54  	if err := jsonstring.EncodeStringFromString(&es.Buffer, lowmemjson.EscapeDefault, 0, reflect.Value{}, str); err != nil {
    55  		panic(err)
    56  	}
    57  }
    58  
    59  func (es *encodeState) stringBytes(str []byte, _ bool) {
    60  	if err := jsonstring.EncodeStringFromBytes(&es.Buffer, lowmemjson.EscapeDefault, 0, reflect.Value{}, str); err != nil {
    61  		panic(err)
    62  	}
    63  }
    64  
    65  type (
    66  	Token = json.Token
    67  	Delim = json.Delim
    68  )
    69  
    70  func (dec *Decoder) Token() (Token, error) {
    71  	panic("not implemented")
    72  }