Source File
encode.go
Belonging Package
google.golang.org/protobuf/internal/encoding/json
package json
import (
)
type kind uint8
const (
_ kind = (1 << iota) / 2
name
scalar
objectOpen
objectClose
arrayOpen
arrayClose
)
var errInvalidUTF8 = errors.New("invalid UTF-8")
func ( []byte, string) ([]byte, error) {
= append(, '"')
:= indexNeedEscapeInString()
, = [:], append(, [:]...)
for len() > 0 {
switch , := utf8.DecodeRuneInString(); {
case == utf8.RuneError && == 1:
return , errInvalidUTF8
case < ' ' || == '"' || == '\\':
= append(, '\\')
switch {
case '"', '\\':
= append(, byte())
case '\b':
= append(, 'b')
case '\f':
= append(, 'f')
case '\n':
= append(, 'n')
case '\r':
= append(, 'r')
case '\t':
= append(, 't')
default:
= append(, 'u')
= append(, "0000"[1+(bits.Len32(uint32())-1)/4:]...)
= strconv.AppendUint(, uint64(), 16)
}
= [:]
default:
:= indexNeedEscapeInString([:])
, = [+:], append(, [:+]...)
}
}
= append(, '"')
return , nil
}
func ( *Encoder) ( float64, int) {
.prepareNext(scalar)
.out = appendFloat(.out, , )
}
func ( *Encoder) ( uint64) {
.prepareNext(scalar)
.out = append(.out, strconv.FormatUint(, 10)...)
}
func ( *Encoder) () {
.prepareNext(objectOpen)
.out = append(.out, '{')
}
func ( *Encoder) () {
.prepareNext(objectClose)
.out = append(.out, '}')
}
func ( *Encoder) ( string) error {
.prepareNext(name)
func ( *Encoder) () {
.prepareNext(arrayClose)
.out = append(.out, ']')
}
.lastKind =
}()
if .lastKind&(scalar|objectClose|arrayClose) != 0 &&
&(name|scalar|objectOpen|arrayOpen) != 0 {
if &(objectClose|arrayClose) == 0 {
.indents = append(.indents, .indent...)
.out = append(.out, '\n')
.out = append(.out, .indents...)
}
case .lastKind&(scalar|objectClose|arrayClose) != 0:
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin 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 @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |