Source File
decode_token.go
Belonging Package
google.golang.org/protobuf/internal/encoding/json
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package json
import (
)
// Kind represents a token kind expressible in the JSON format.
type Kind uint16
const (
Invalid Kind = (1 << iota) / 2
EOF
Null
Bool
Number
String
Name
ObjectOpen
ObjectClose
ArrayOpen
ArrayClose
// comma is only for parsing in between tokens and
// does not need to be exported.
comma
)
func ( Kind) () string {
switch {
case EOF:
return "eof"
case Null:
return "null"
case Bool:
return "bool"
case Number:
return "number"
case String:
return "string"
case ObjectOpen:
return "{"
case ObjectClose:
return "}"
case Name:
return "name"
case ArrayOpen:
return "["
case ArrayClose:
return "]"
case comma:
return ","
}
return "<invalid>"
}
// Token provides a parsed token kind and value.
//
// Values are provided by the difference accessor methods. The accessor methods
// Name, Bool, and ParsedString will panic if called on the wrong kind. There
// are different accessor methods for the Number kind for converting to the
// appropriate Go numeric type and those methods have the ok return value.
type Token struct {
// Token kind.
kind Kind
// pos provides the position of the token in the original input.
pos int
// raw bytes of the serialized token.
// This is a subslice into the original input.
raw []byte
// boo is parsed boolean value.
boo bool
// str is parsed string value.
str string
}
// Kind returns the token kind.
func ( Token) () Kind {
return .kind
}
// RawString returns the read value in string.
func ( Token) () string {
return string(.raw)
}
// Pos returns the token position from the input.
func ( Token) () int {
return .pos
}
// Name returns the object name if token is Name, else it panics.
func ( Token) () string {
if .kind == Name {
return .str
}
panic(fmt.Sprintf("Token is not a Name: %v", .RawString()))
}
// Bool returns the bool value if token kind is Bool, else it panics.
func ( Token) () bool {
if .kind == Bool {
return .boo
}
panic(fmt.Sprintf("Token is not a Bool: %v", .RawString()))
}
// ParsedString returns the string value for a JSON string token or the read
// value in string if token is not a string.
func ( Token) () string {
if .kind == String {
return .str
}
panic(fmt.Sprintf("Token is not a String: %v", .RawString()))
}
// Float returns the floating-point number if token kind is Number.
//
// The floating-point precision is specified by the bitSize parameter: 32 for
// float32 or 64 for float64. If bitSize=32, the result still has type float64,
// but it will be convertible to float32 without changing its value. It will
// return false if the number exceeds the floating point limits for given
// bitSize.
func ( Token) ( int) (float64, bool) {
if .kind != Number {
return 0, false
}
, := strconv.ParseFloat(.RawString(), )
if != nil {
return 0, false
}
return , true
}
// Int returns the signed integer number if token is Number.
//
// The given bitSize specifies the integer type that the result must fit into.
// It returns false if the number is not an integer value or if the result
// exceeds the limits for given bitSize.
func ( Token) ( int) (int64, bool) {
, := .getIntStr()
if ! {
return 0, false
}
, := strconv.ParseInt(, 10, )
if != nil {
return 0, false
}
return , true
}
// Uint returns the signed integer number if token is Number.
//
// The given bitSize specifies the unsigned integer type that the result must
// fit into. It returns false if the number is not an unsigned integer value
// or if the result exceeds the limits for given bitSize.
func ( Token) ( int) (uint64, bool) {
, := .getIntStr()
if ! {
return 0, false
}
, := strconv.ParseUint(, 10, )
if != nil {
return 0, false
}
return , true
}
func ( Token) () (string, bool) {
if .kind != Number {
return "", false
}
, := parseNumberParts(.raw)
if ! {
return "", false
}
return normalizeToIntString()
}
// TokenEquals returns true if given Tokens are equal, else false.
func (, Token) bool {
return .kind == .kind &&
.pos == .pos &&
bytes.Equal(.raw, .raw) &&
.boo == .boo &&
.str == .str
}
The pages are generated with Golds v0.4.9. (GOOS=linux GOARCH=amd64)