MCPcopy
hub / github.com/protocolbuffers/protobuf-go / Read

Method Read

internal/encoding/json/decode.go:69–152  ·  view source on GitHub ↗

Read returns the next JSON token. It will return an error if there is no valid token.

()

Source from the content-addressed store, hash-verified

67// Read returns the next JSON token.
68// It will return an error if there is no valid token.
69func (d *Decoder) Read() (Token, error) {
70 const scalar = Null | Bool | Number | String
71
72 defer func() { d.lastCall = readCall }()
73 if d.lastCall == peekCall {
74 return d.lastToken, d.lastErr
75 }
76
77 tok, err := d.parseNext()
78 if err != nil {
79 return Token{}, err
80 }
81
82 switch tok.kind {
83 case EOF:
84 if len(d.openStack) != 0 ||
85 d.lastToken.kind&scalar|ObjectClose|ArrayClose == 0 {
86 return Token{}, ErrUnexpectedEOF
87 }
88
89 case Null:
90 if !d.isValueNext() {
91 return Token{}, d.newSyntaxError(tok.pos, unexpectedFmt, tok.RawString())
92 }
93
94 case Bool, Number:
95 if !d.isValueNext() {
96 return Token{}, d.newSyntaxError(tok.pos, unexpectedFmt, tok.RawString())
97 }
98
99 case String:
100 if d.isValueNext() {
101 break
102 }
103 // This string token should only be for a field name.
104 if d.lastToken.kind&(ObjectOpen|comma) == 0 {
105 return Token{}, d.newSyntaxError(tok.pos, unexpectedFmt, tok.RawString())
106 }
107 if len(d.in) == 0 {
108 return Token{}, ErrUnexpectedEOF
109 }
110 if c := d.in[0]; c != ':' {
111 return Token{}, d.newSyntaxError(d.currPos(), `unexpected character %s, missing ":" after field name`, string(c))
112 }
113 tok.kind = Name
114 d.consume(1)
115
116 case ObjectOpen, ArrayOpen:
117 if !d.isValueNext() {
118 return Token{}, d.newSyntaxError(tok.pos, unexpectedFmt, tok.RawString())
119 }
120 d.openStack = append(d.openStack, tok.kind)
121
122 case ObjectClose:
123 if len(d.openStack) == 0 ||
124 d.lastToken.kind&(Name|comma) != 0 ||
125 d.openStack[len(d.openStack)-1] != ObjectOpen {
126 return Token{}, d.newSyntaxError(tok.pos, unexpectedFmt, tok.RawString())

Callers 15

unmarshalIntFunction · 0.95
unmarshalUintFunction · 0.95
unmarshalFloatFunction · 0.95
TestDecoderFunction · 0.95
TestCloneFunction · 0.95
BenchmarkFloatFunction · 0.95
BenchmarkIntFunction · 0.95
BenchmarkStringFunction · 0.95
BenchmarkBoolFunction · 0.95
PeekMethod · 0.95
unmarshalMessageMethod · 0.45
unmarshalScalarMethod · 0.45

Calls 6

parseNextMethod · 0.95
isValueNextMethod · 0.95
newSyntaxErrorMethod · 0.95
currPosMethod · 0.95
consumeMethod · 0.95
RawStringMethod · 0.45

Tested by 7

TestDecoderFunction · 0.76
TestCloneFunction · 0.76
BenchmarkFloatFunction · 0.76
BenchmarkIntFunction · 0.76
BenchmarkStringFunction · 0.76
BenchmarkBoolFunction · 0.76
compareDecodersFunction · 0.36