Skip to content

Commit

Permalink
feat(enc): non-pointer receiver for String, Bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
ernado committed Nov 2, 2021
1 parent 91cfa36 commit e1ffda4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ fmt.Println(values)
// Output: [4 8 15 16 23 42]
```

### Encoder
```go
var e jx.Encoder // zero value is ok
e.ObjStart()
e.ObjField("values") // "values":
e.ArrStart()
for i, v := range []int{4, 8, 15, 16, 23, 42} {
if i != 0 {
e.More() // ,
}
e.Int(v)
}
e.ArrEnd()
e.ObjEnd()
fmt.Println(e)
// Output: {"values":[4,8,15,16,23,42]}
```

## Capture
The `Decoder.Capture` method allows to unread everything is read in callback.
This is useful for multi-pass parsing.
Expand Down
4 changes: 2 additions & 2 deletions enc.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (e *Encoder) SetIdent(n int) {
}

// String returns string of underlying buffer.
func (e *Encoder) String() string {
func (e Encoder) String() string {
return string(e.Bytes())
}

Expand All @@ -41,7 +41,7 @@ func (e *Encoder) Reset() {
}

// Bytes returns underlying buffer.
func (e *Encoder) Bytes() []byte { return e.buf }
func (e Encoder) Bytes() []byte { return e.buf }

// SetBytes sets underlying buffer.
func (e *Encoder) SetBytes(buf []byte) { e.buf = buf }
Expand Down
17 changes: 17 additions & 0 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,20 @@ func ExampleDecodeStr() {
fmt.Println(values)
// Output: [4 8 15 16 23 42]
}

func ExampleEncoder_String() {
var e jx.Encoder
e.ObjStart()
e.ObjField("values")
e.ArrStart()
for i, v := range []int{4, 8, 15, 16, 23, 42} {
if i != 0 {
e.More()
}
e.Int(v)
}
e.ArrEnd()
e.ObjEnd()
fmt.Println(e)
// Output: {"values":[4,8,15,16,23,42]}
}

0 comments on commit e1ffda4

Please sign in to comment.