Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a thin wrapper for encoding/json's Decoder #85

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions decoder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package simplejson

import (
"encoding/json"
"io"
)

// Decoder is a simple wrapper of encoding/json's Decoder.
type Decoder json.Decoder

// Decode reads the next JSON-encoded value from its input and returns it.
func (dec *Decoder) Decode() (*Json, error) {
j := new(Json)
err := (*json.Decoder)(dec).Decode(&j.data)
return j, err
}

// Buffered returns a reader of the data remaining in the Decoder's buffer.
// The reader is valid until the next call to Decode.
func (dec *Decoder) Buffered() io.Reader {
return (*json.Decoder)(dec).Buffered()
}

// More reports whether there is another element in the
// current array or object being parsed.
func (dec *Decoder) More() bool {
return (*json.Decoder)(dec).More()
}
13 changes: 13 additions & 0 deletions decoder_go10.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// +build !go1.1

package simplejson

import (
"encoding/json"
"io"
)

func NewDecoder(r io.Reader) *Decoder {
dec := json.NewDecoder(r)
return (*Decoder)(dec)
}
15 changes: 15 additions & 0 deletions decoder_go11.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// +build go1.1

package simplejson

import (
"encoding/json"
"io"
)

// NewDecoder returns a new decoder that reads from r.
func NewDecoder(r io.Reader) *Decoder {
dec := json.NewDecoder(r)
dec.UseNumber()
return (*Decoder)(dec)
}
142 changes: 142 additions & 0 deletions decoder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package simplejson

import (
"io"
"reflect"
"strings"
"testing"
)

// This test case was copied from encoding/json/stream_test.go
var streamEncoded = `0.1
"hello"
null
true
false
["a","b","c"]
{"ß":"long s","K":"Kelvin"}
3.14
`

func TestDecoder(t *testing.T) {
r := strings.NewReader(streamEncoded)
dec := NewDecoder(r)

if !dec.More() {
t.Error("More must returns true")
}
j, err := dec.Decode()
if err != nil {
t.Fatal(err)
}
f, err := j.Float64()
if err != nil {
t.Error(err)
} else if f != 0.1 {
t.Errorf(`want %v, but got %v`, 0.1, f)
}

if !dec.More() {
t.Error("More must returns true")
}
j, err = dec.Decode()
if err != nil {
t.Fatal(err)
}
s, err := j.String()
if err != nil {
t.Error(err)
} else if s != "hello" {
t.Errorf(`want %v, but got %v`, "hello", s)
}

if !dec.More() {
t.Error("More must returns true")
}
j, err = dec.Decode()
if err != nil {
t.Fatal(err)
}
i := j.Interface()
if i != nil {
t.Errorf(`want %v, but got %v`, nil, i)
}

if !dec.More() {
t.Error("More must returns true")
}
j, err = dec.Decode()
if err != nil {
t.Fatal(err)
}
b, err := j.Bool()
if err != nil {
t.Error(err)
} else if b != true {
t.Errorf(`want %v, but got %v`, true, b)
}

if !dec.More() {
t.Error("More must returns true")
}
j, err = dec.Decode()
if err != nil {
t.Fatal(err)
}
b, err = j.Bool()
if err != nil {
t.Error(err)
} else if b != false {
t.Errorf(`want %v, but got %v`, false, b)
}

if !dec.More() {
t.Error("More must returns true")
}
j, err = dec.Decode()
if err != nil {
t.Fatal(err)
}
a, err := j.Array()
if err != nil {
t.Error(err)
} else if !reflect.DeepEqual(a, []interface{}{"a", "b", "c"}) {
t.Errorf(`want %v, but got %v`, true, a)
}

if !dec.More() {
t.Error("More must returns true")
}
j, err = dec.Decode()
if err != nil {
t.Fatal(err)
}
m, err := j.Map()
if err != nil {
t.Error(err)
} else if !reflect.DeepEqual(m, map[string]interface{}{"ß": "long s", "K": "Kelvin"}) {
t.Errorf(`want %v, but got %v`, true, a)
}

if !dec.More() {
t.Error("More must returns true")
}
j, err = dec.Decode()
if err != nil {
t.Fatal(err)
}
f, err = j.Float64()
if err != nil {
t.Error(err)
} else if f != 3.14 {
t.Errorf(`want %v, but got %v`, 3.14, f)
}

if dec.More() {
t.Error("More must returns false")
}
_, err = dec.Decode()
if err != io.EOF {
t.Errorf(`want %v, but got %v`, io.EOF, err)
}
}