Skip to content

Commit f048771

Browse files
committed
document public symbols
1 parent 46574e7 commit f048771

22 files changed

+227
-176
lines changed

extra/fuzzy_decoder.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ type tolerateEmptyArrayDecoder struct {
160160
func (decoder *tolerateEmptyArrayDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
161161
if iter.WhatIsNext() == jsoniter.Array {
162162
iter.Skip()
163-
newIter := iter.Config().BorrowIterator([]byte("{}"))
164-
defer iter.Config().ReturnIterator(newIter)
163+
newIter := iter.Pool().BorrowIterator([]byte("{}"))
164+
defer iter.Pool().ReturnIterator(newIter)
165165
decoder.valDecoder.Decode(ptr, newIter)
166166
} else {
167167
decoder.valDecoder.Decode(ptr, iter)
@@ -202,8 +202,8 @@ func (decoder *fuzzyIntegerDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.It
202202
default:
203203
iter.ReportError("fuzzyIntegerDecoder", "not number or string")
204204
}
205-
newIter := iter.Config().BorrowIterator([]byte(str))
206-
defer iter.Config().ReturnIterator(newIter)
205+
newIter := iter.Pool().BorrowIterator([]byte(str))
206+
defer iter.Pool().ReturnIterator(newIter)
207207
isFloat := strings.IndexByte(str, '.') != -1
208208
decoder.fun(isFloat, ptr, newIter)
209209
if newIter.Error != nil {
@@ -222,8 +222,8 @@ func (decoder *fuzzyFloat32Decoder) Decode(ptr unsafe.Pointer, iter *jsoniter.It
222222
*((*float32)(ptr)) = iter.ReadFloat32()
223223
case jsoniter.String:
224224
str = iter.ReadString()
225-
newIter := iter.Config().BorrowIterator([]byte(str))
226-
defer iter.Config().ReturnIterator(newIter)
225+
newIter := iter.Pool().BorrowIterator([]byte(str))
226+
defer iter.Pool().ReturnIterator(newIter)
227227
*((*float32)(ptr)) = newIter.ReadFloat32()
228228
if newIter.Error != nil {
229229
iter.Error = newIter.Error
@@ -244,8 +244,8 @@ func (decoder *fuzzyFloat64Decoder) Decode(ptr unsafe.Pointer, iter *jsoniter.It
244244
*((*float64)(ptr)) = iter.ReadFloat64()
245245
case jsoniter.String:
246246
str = iter.ReadString()
247-
newIter := iter.Config().BorrowIterator([]byte(str))
248-
defer iter.Config().ReturnIterator(newIter)
247+
newIter := iter.Pool().BorrowIterator([]byte(str))
248+
defer iter.Pool().ReturnIterator(newIter)
249249
*((*float64)(ptr)) = newIter.ReadFloat64()
250250
if newIter.Error != nil {
251251
iter.Error = newIter.Error

feature_adapter.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
// Package jsoniter implements encoding and decoding of JSON as defined in
2-
// RFC 4627 and provides interfaces with identical syntax of standard lib encoding/json.
3-
// Converting from encoding/json to jsoniter is no more than replacing the package with jsoniter
4-
// and variable type declarations (if any).
5-
// jsoniter interfaces gives 100% compatibility with code using standard lib.
6-
//
7-
// "JSON and Go"
8-
// (https://golang.org/doc/articles/json_and_go.html)
9-
// gives a description of how Marshal/Unmarshal operate
10-
// between arbitrary or predefined json objects and bytes,
11-
// and it applies to jsoniter.Marshal/Unmarshal as well.
121
package jsoniter
132

143
import (
@@ -96,7 +85,7 @@ func (adapter *Decoder) Buffered() io.Reader {
9685
func (decoder *Decoder) UseNumber() {
9786
origCfg := decoder.iter.cfg.configBeforeFrozen
9887
origCfg.UseNumber = true
99-
decoder.iter.cfg = origCfg.Froze()
88+
decoder.iter.cfg = origCfg.Froze().(*frozenConfig)
10089
}
10190

10291
func NewEncoder(writer io.Writer) *Encoder {
@@ -119,6 +108,6 @@ func (adapter *Encoder) SetIndent(prefix, indent string) {
119108

120109
func (adapter *Encoder) SetEscapeHTML(escapeHtml bool) {
121110
config := adapter.stream.cfg.configBeforeFrozen
122-
config.EscapeHtml = escapeHtml
123-
adapter.stream.cfg = config.Froze()
111+
config.EscapeHTML = escapeHtml
112+
adapter.stream.cfg = config.Froze().(*frozenConfig)
124113
}

feature_config.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ import (
99
"unsafe"
1010
)
1111

12+
// Config customize how the API should behave.
13+
// The API is created from Config by Froze.
1214
type Config struct {
1315
IndentionStep int
1416
MarshalFloatWith6Digits bool
15-
EscapeHtml bool
17+
EscapeHTML bool
1618
SortMapKeys bool
1719
UseNumber bool
1820
}
@@ -28,7 +30,11 @@ type frozenConfig struct {
2830
iteratorPool chan *Iterator
2931
}
3032

31-
type Api interface {
33+
// API the public interface of this package.
34+
// Primary Marshal and Unmarshal.
35+
type API interface {
36+
IteratorPool
37+
StreamPool
3238
MarshalToString(v interface{}) (string, error)
3339
Marshal(v interface{}) ([]byte, error)
3440
MarshalIndent(v interface{}, prefix, indent string) ([]byte, error)
@@ -39,22 +45,25 @@ type Api interface {
3945
NewDecoder(reader io.Reader) *Decoder
4046
}
4147

48+
// ConfigDefault the default API
4249
var ConfigDefault = Config{
43-
EscapeHtml: true,
50+
EscapeHTML: true,
4451
}.Froze()
4552

46-
// Trying to be 100% compatible with standard library behavior
53+
// ConfigCompatibleWithStandardLibrary tries to be 100% compatible with standard library behavior
4754
var ConfigCompatibleWithStandardLibrary = Config{
48-
EscapeHtml: true,
55+
EscapeHTML: true,
4956
SortMapKeys: true,
5057
}.Froze()
5158

59+
// ConfigFastest marshals float with only 6 digits precision
5260
var ConfigFastest = Config{
53-
EscapeHtml: false,
61+
EscapeHTML: false,
5462
MarshalFloatWith6Digits: true,
5563
}.Froze()
5664

57-
func (cfg Config) Froze() *frozenConfig {
65+
// Froze forge API from config
66+
func (cfg Config) Froze() API {
5867
// TODO: cache frozen config
5968
frozenConfig := &frozenConfig{
6069
sortMapKeys: cfg.SortMapKeys,
@@ -67,8 +76,8 @@ func (cfg Config) Froze() *frozenConfig {
6776
if cfg.MarshalFloatWith6Digits {
6877
frozenConfig.marshalFloatWith6Digits()
6978
}
70-
if cfg.EscapeHtml {
71-
frozenConfig.escapeHtml()
79+
if cfg.EscapeHTML {
80+
frozenConfig.escapeHTML()
7281
}
7382
if cfg.UseNumber {
7483
frozenConfig.useNumber()
@@ -145,7 +154,7 @@ func (encoder *htmlEscapedStringEncoder) IsEmpty(ptr unsafe.Pointer) bool {
145154
return *((*string)(ptr)) == ""
146155
}
147156

148-
func (cfg *frozenConfig) escapeHtml() {
157+
func (cfg *frozenConfig) escapeHTML() {
149158
cfg.addEncoderToCache(reflect.TypeOf((*string)(nil)).Elem(), &htmlEscapedStringEncoder{})
150159
}
151160

@@ -189,18 +198,16 @@ func (cfg *frozenConfig) getEncoderFromCache(cacheKey reflect.Type) ValEncoder {
189198
return cache[cacheKey]
190199
}
191200

192-
// cleanDecoders cleans decoders registered or cached
193201
func (cfg *frozenConfig) cleanDecoders() {
194202
typeDecoders = map[string]ValDecoder{}
195203
fieldDecoders = map[string]ValDecoder{}
196-
*cfg = *cfg.configBeforeFrozen.Froze()
204+
*cfg = *(cfg.configBeforeFrozen.Froze().(*frozenConfig))
197205
}
198206

199-
// cleanEncoders cleans encoders registered or cached
200207
func (cfg *frozenConfig) cleanEncoders() {
201208
typeEncoders = map[string]ValEncoder{}
202209
fieldEncoders = map[string]ValEncoder{}
203-
*cfg = *cfg.configBeforeFrozen.Froze()
210+
*cfg = *(cfg.configBeforeFrozen.Froze().(*frozenConfig))
204211
}
205212

206213
func (cfg *frozenConfig) MarshalToString(v interface{}) (string, error) {

feature_iter.go

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
1-
//
2-
// Besides, jsoniter.Iterator provides a different set of interfaces
3-
// iterating given bytes/string/reader
4-
// and yielding parsed elements one by one.
5-
// This set of interfaces reads input as required and gives
6-
// better performance.
71
package jsoniter
82

93
import (
104
"fmt"
115
"io"
126
)
137

8+
// ValueType the type for JSON element
149
type ValueType int
1510

1611
const (
12+
// Invalid invalid JSON element
1713
Invalid ValueType = iota
14+
// String JSON element "string"
1815
String
16+
// Number JSON element 100 or 0.10
1917
Number
18+
// Nil JSON element null
2019
Nil
20+
// Bool JSON element true or false
2121
Bool
22+
// Array JSON element []
2223
Array
24+
// Object JSON element {}
2325
Object
2426
)
2527

@@ -63,7 +65,8 @@ func init() {
6365
valueTypes['{'] = Object
6466
}
6567

66-
// Iterator is a fast and flexible JSON parser
68+
// Iterator is a io.Reader like object, with JSON specific read functions.
69+
// Error is not returned as return value, but stored as Error member on this iterator instance.
6770
type Iterator struct {
6871
cfg *frozenConfig
6972
reader io.Reader
@@ -75,57 +78,58 @@ type Iterator struct {
7578
Error error
7679
}
7780

78-
// Create creates an empty Iterator instance
79-
func NewIterator(cfg *frozenConfig) *Iterator {
81+
// NewIterator creates an empty Iterator instance
82+
func NewIterator(cfg API) *Iterator {
8083
return &Iterator{
81-
cfg: cfg,
84+
cfg: cfg.(*frozenConfig),
8285
reader: nil,
8386
buf: nil,
8487
head: 0,
8588
tail: 0,
8689
}
8790
}
8891

89-
// Parse parses a json buffer in io.Reader into an Iterator instance
90-
func Parse(cfg *frozenConfig, reader io.Reader, bufSize int) *Iterator {
92+
// Parse creates an Iterator instance from io.Reader
93+
func Parse(cfg API, reader io.Reader, bufSize int) *Iterator {
9194
return &Iterator{
92-
cfg: cfg,
95+
cfg: cfg.(*frozenConfig),
9396
reader: reader,
9497
buf: make([]byte, bufSize),
9598
head: 0,
9699
tail: 0,
97100
}
98101
}
99102

100-
// ParseBytes parses a json byte slice into an Iterator instance
101-
func ParseBytes(cfg *frozenConfig, input []byte) *Iterator {
103+
// ParseBytes creates an Iterator instance from byte array
104+
func ParseBytes(cfg API, input []byte) *Iterator {
102105
return &Iterator{
103-
cfg: cfg,
106+
cfg: cfg.(*frozenConfig),
104107
reader: nil,
105108
buf: input,
106109
head: 0,
107110
tail: len(input),
108111
}
109112
}
110113

111-
// ParseString parses a json string into an Iterator instance
112-
func ParseString(cfg *frozenConfig, input string) *Iterator {
114+
// ParseString creates an Iterator instance from string
115+
func ParseString(cfg API, input string) *Iterator {
113116
return ParseBytes(cfg, []byte(input))
114117
}
115118

116-
func (iter *Iterator) Config() *frozenConfig {
119+
// Pool returns a pool can provide more iterator with same configuration
120+
func (iter *Iterator) Pool() IteratorPool {
117121
return iter.cfg
118122
}
119123

120-
// Reset can reset an Iterator instance for another json buffer in io.Reader
124+
// Reset reuse iterator instance by specifying another reader
121125
func (iter *Iterator) Reset(reader io.Reader) *Iterator {
122126
iter.reader = reader
123127
iter.head = 0
124128
iter.tail = 0
125129
return iter
126130
}
127131

128-
// ResetBytes can reset an Iterator instance for another json byte slice
132+
// ResetBytes reuse iterator instance by specifying another byte array as input
129133
func (iter *Iterator) ResetBytes(input []byte) *Iterator {
130134
iter.reader = nil
131135
iter.buf = input
@@ -134,7 +138,7 @@ func (iter *Iterator) ResetBytes(input []byte) *Iterator {
134138
return iter
135139
}
136140

137-
// WhatIsNext gets ValueType of relatively next json object
141+
// WhatIsNext gets ValueType of relatively next json element
138142
func (iter *Iterator) WhatIsNext() ValueType {
139143
valueType := valueTypes[iter.nextToken()]
140144
iter.unreadByte()
@@ -184,6 +188,7 @@ func (iter *Iterator) nextToken() byte {
184188
}
185189
}
186190

191+
// ReportError record a error in iterator instance with current position.
187192
func (iter *Iterator) ReportError(operation string, msg string) {
188193
if iter.Error != nil {
189194
if iter.Error != io.EOF {
@@ -198,7 +203,7 @@ func (iter *Iterator) ReportError(operation string, msg string) {
198203
string(iter.buf[peekStart:iter.head]), string(iter.buf[0:iter.tail]))
199204
}
200205

201-
// CurrentBuffer gets current buffer as string
206+
// CurrentBuffer gets current buffer as string for debugging purpose
202207
func (iter *Iterator) CurrentBuffer() string {
203208
peekStart := iter.head - 10
204209
if peekStart < 0 {
@@ -261,6 +266,7 @@ func (iter *Iterator) unreadByte() {
261266
return
262267
}
263268

269+
// Read read the next JSON element as generic interface{}.
264270
func (iter *Iterator) Read() interface{} {
265271
valueType := iter.WhatIsNext()
266272
switch valueType {

feature_iter_array.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package jsoniter
22

3+
// ReadArray read array element, tells if the array has more element to read.
34
func (iter *Iterator) ReadArray() (ret bool) {
45
c := iter.nextToken()
56
switch c {
@@ -23,6 +24,7 @@ func (iter *Iterator) ReadArray() (ret bool) {
2324
}
2425
}
2526

27+
// ReadArrayCB read array with callback
2628
func (iter *Iterator) ReadArrayCB(callback func(*Iterator) bool) (ret bool) {
2729
c := iter.nextToken()
2830
if c == '[' {

feature_iter_float.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func init() {
3030
floatDigits['.'] = dotInNumber
3131
}
3232

33+
// ReadBigFloat read big.Float
3334
func (iter *Iterator) ReadBigFloat() (ret *big.Float) {
3435
str := iter.readNumberAsString()
3536
if iter.Error != nil && iter.Error != io.EOF {
@@ -47,6 +48,7 @@ func (iter *Iterator) ReadBigFloat() (ret *big.Float) {
4748
return val
4849
}
4950

51+
// ReadBigInt read big.Int
5052
func (iter *Iterator) ReadBigInt() (ret *big.Int) {
5153
str := iter.readNumberAsString()
5254
if iter.Error != nil && iter.Error != io.EOF {
@@ -62,14 +64,14 @@ func (iter *Iterator) ReadBigInt() (ret *big.Int) {
6264
return ret
6365
}
6466

67+
//ReadFloat32 read float32
6568
func (iter *Iterator) ReadFloat32() (ret float32) {
6669
c := iter.nextToken()
6770
if c == '-' {
6871
return -iter.readPositiveFloat32()
69-
} else {
70-
iter.unreadByte()
71-
return iter.readPositiveFloat32()
7272
}
73+
iter.unreadByte()
74+
return iter.readPositiveFloat32()
7375
}
7476

7577
func (iter *Iterator) readPositiveFloat32() (ret float32) {
@@ -165,14 +167,14 @@ func (iter *Iterator) readFloat32SlowPath() (ret float32) {
165167
return float32(val)
166168
}
167169

170+
// ReadFloat64 read float64
168171
func (iter *Iterator) ReadFloat64() (ret float64) {
169172
c := iter.nextToken()
170173
if c == '-' {
171174
return -iter.readPositiveFloat64()
172-
} else {
173-
iter.unreadByte()
174-
return iter.readPositiveFloat64()
175175
}
176+
iter.unreadByte()
177+
return iter.readPositiveFloat64()
176178
}
177179

178180
func (iter *Iterator) readPositiveFloat64() (ret float64) {

0 commit comments

Comments
 (0)