1
// Copyright 2019 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
13
// Kind represents a token kind expressible in the JSON format.
17
Invalid Kind = (1 << iota) / 2
29
// comma is only for parsing in between tokens and
30
// does not need to be exported.
34
func (k Kind) String() string {
62
// Token provides a parsed token kind and value.
64
// Values are provided by the difference accessor methods. The accessor methods
65
// Name, Bool, and ParsedString will panic if called on the wrong kind. There
66
// are different accessor methods for the Number kind for converting to the
67
// appropriate Go numeric type and those methods have the ok return value.
71
// pos provides the position of the token in the original input.
73
// raw bytes of the serialized token.
74
// This is a subslice into the original input.
76
// boo is parsed boolean value.
78
// str is parsed string value.
82
// Kind returns the token kind.
83
func (t Token) Kind() Kind {
87
// RawString returns the read value in string.
88
func (t Token) RawString() string {
92
// Pos returns the token position from the input.
93
func (t Token) Pos() int {
97
// Name returns the object name if token is Name, else it panics.
98
func (t Token) Name() string {
102
panic(fmt.Sprintf("Token is not a Name: %v", t.RawString()))
105
// Bool returns the bool value if token kind is Bool, else it panics.
106
func (t Token) Bool() bool {
110
panic(fmt.Sprintf("Token is not a Bool: %v", t.RawString()))
113
// ParsedString returns the string value for a JSON string token or the read
114
// value in string if token is not a string.
115
func (t Token) ParsedString() string {
116
if t.kind == String {
119
panic(fmt.Sprintf("Token is not a String: %v", t.RawString()))
122
// Float returns the floating-point number if token kind is Number.
124
// The floating-point precision is specified by the bitSize parameter: 32 for
125
// float32 or 64 for float64. If bitSize=32, the result still has type float64,
126
// but it will be convertible to float32 without changing its value. It will
127
// return false if the number exceeds the floating point limits for given
129
func (t Token) Float(bitSize int) (float64, bool) {
130
if t.kind != Number {
133
f, err := strconv.ParseFloat(t.RawString(), bitSize)
140
// Int returns the signed integer number if token is Number.
142
// The given bitSize specifies the integer type that the result must fit into.
143
// It returns false if the number is not an integer value or if the result
144
// exceeds the limits for given bitSize.
145
func (t Token) Int(bitSize int) (int64, bool) {
146
s, ok := t.getIntStr()
150
n, err := strconv.ParseInt(s, 10, bitSize)
157
// Uint returns the signed integer number if token is Number.
159
// The given bitSize specifies the unsigned integer type that the result must
160
// fit into. It returns false if the number is not an unsigned integer value
161
// or if the result exceeds the limits for given bitSize.
162
func (t Token) Uint(bitSize int) (uint64, bool) {
163
s, ok := t.getIntStr()
167
n, err := strconv.ParseUint(s, 10, bitSize)
174
func (t Token) getIntStr() (string, bool) {
175
if t.kind != Number {
178
parts, ok := parseNumberParts(t.raw)
182
return normalizeToIntString(parts)
185
// TokenEquals returns true if given Tokens are equal, else false.
186
func TokenEquals(x, y Token) bool {
187
return x.kind == y.kind &&
189
bytes.Equal(x.raw, y.raw) &&