// 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 jsonimport ()// Kind represents a token kind expressible in the JSON format.typeKinduint16const (InvalidKind = (1 << iota) / 2EOFNullBoolNumberStringNameObjectOpenObjectCloseArrayOpenArrayClose// comma is only for parsing in between tokens and // does not need to be exported. comma)func ( Kind) () string {switch {caseEOF:return"eof"caseNull:return"null"caseBool:return"bool"caseNumber:return"number"caseString:return"string"caseObjectOpen:return"{"caseObjectClose:return"}"caseName:return"name"caseArrayOpen:return"["caseArrayClose:return"]"casecomma: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.typeTokenstruct {// 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 {returnstring(.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 {return0, false } , := strconv.ParseFloat(.RawString(), )if != nil {return0, 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 ! {return0, false } , := strconv.ParseInt(, 10, )if != nil {return0, 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 ! {return0, false } , := strconv.ParseUint(, 10, )if != nil {return0, false }return , true}func ( Token) () (string, bool) {if .kind != Number {return"", false } , := parseNumberParts(.raw)if ! {return"", false }returnnormalizeToIntString()}// 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 Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.