Coder Social home page Coder Social logo

json's People

Contributors

davecheney avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

json's Issues

BenchmarkParseNumber in scanner_test.go

func BenchmarkParseNumber(b *testing.B) {

Hi Dave
I think a small change should be done for this func()
To see an issue quickly just run this line

go test -bench=.Parse.

`

panic: runtime error: index out of range [0] with length 0

goroutine 290 [running]:
github.com/pkg/json.BenchmarkParseNumber.func1(0xc000194240)
C:/Users/VE/go/dev/src/davecheney/pkg/json-master/scanner_test.go:157 +0x51
testing.(*B).runN(0xc000194240, 0x1)
C:/Go/src/testing/benchmark.go:191 +0xf2
testing.(*B).run1.func1(0xc000194240)
C:/Go/src/testing/benchmark.go:231 +0x5e
created by testing.(*B).run1
C:/Go/src/testing/benchmark.go:224 +0x85
exit status 2
`

// --------------------------------------------------------------------------------------------------------------

`

		for i := 0; i < b.N; i++ {
			r.Seek(0, 0)
			scanner := &Scanner{
				br: byteReader{
					data: buf[:0],
					r:    r,
				},
			}
			scanner.Next()                                       // add scanner.Next() here to escape panic
			n := scanner.parseNumber(scanner.br.window(0)[0])    // panic was here

`

Unexpected behaviour of internal type `stack'

func TestBoolStack(t *testing.T) {
	var bs stack
	bs.push(true)
	bs.push(false)
	bs.push(true)
	for i := len(bs); i > 0; i-- {
		t.Log(bs.len(), bs.pop())
	}
}

Output:

=== RUN   TestBoolStack
    decoder_test.go:229: 3 false
    decoder_test.go:229: 2 true
    decoder_test.go:229: 1 false
--- PASS: TestBoolStack (0.00s)

The output doesn't match the reversed input. This is not what I expect from a stack with methods push and pop. Is it on purpose? I guess not, but there is no comment on the stack type to tell otherwise.

support for decoding struct types

It seems the current version of pkg/json does not yet support decoding into struct tyoes. As pkg/josn is still in development, the issue is probably known. This issue is meant to track the progress of implementing support for decoding into struct types.

$ go test
--- FAIL: TestDecodeIntoStruct (0.00s)
    foo_test.go:29: unable to decode into struct; decodeValue: unhandled type: struct
FAIL
exit status 1
FAIL	github.com/pkg/json	0.276s

The following test case passes when using encoding/json but fails with unable to decode into struct; decodeValue: unhandled type: struct when using github.com/pkg/json.

package json

import (
	//"encoding/json"
	"reflect"
	"strings"
	"testing"
)

func TestDecodeIntoStruct(t *testing.T) {
	type Foo struct {
		X int
		Y int
	}
	golden := []struct {
		want Foo
		in   string
	}{
		{
			want: Foo{X: 1, Y: 2},
			in:   `{"X": 1, "Y": 2}`,
		},
	}
	for _, g := range golden {
		r := strings.NewReader(g.in)
		dec := NewDecoder(r)
		got := Foo{}
		if err := dec.Decode(&got); err != nil {
			t.Errorf("unable to decode into struct; %v", err)
			continue
		}
		if g.want != got {
			t.Errorf("decoded value mismatch; expected %#v, got %#v", g.want, got)
		}
	}
}

func TestDecodeIntoMap(t *testing.T) {
	type Foo map[string]int
	golden := []struct {
		want Foo
		in   string
	}{
		{
			want: Foo{"X": 1, "Y": 2},
			in:   `{"X": 1, "Y": 2}`,
		},
	}
	for _, g := range golden {
		r := strings.NewReader(g.in)
		dec := NewDecoder(r)
		got := Foo{}
		if err := dec.Decode(&got); err != nil {
			t.Errorf("unable to decode into struct; %v", err)
			continue
		}
		if !reflect.DeepEqual(g.want, got) {
			t.Errorf("decoded value mismatch; expected %#v, got %#v", g.want, got)
		}
	}
}

Cheers,
Robin

Should `byteReader.extend` account for empty reads?

Hello,

I'm looking to use something like the byteReader approach for one of my projects, and looking at it a bit more closely, I see that the json package considers a call of byteReader.extend that returns 0 as EOF (see the call in Scanner.Next, for example). The documentation of Scanner.Next mentions that "If the stream is at its end, or an error has occurred, Next returns a zero length []byte slice."

However, it is possible that the underlying io.Reader returns 0, nil as a call to Read, which indicates that no data is available but no error was encountered, in which case a number of retries should probably happen in byteReader.extend? I'm thinking something like what is done in bufio.Reader.fill.

It's possible that I'm missing something and the io.Reader in byteReader is always expected to be of a certain type that guarantees no empty reads (without error) or something like that.

Thanks,
Martin

encoding/json compat: missing .More() method on Decoder

When re-encoding a stream back to text, the encoding/json library uses the .More() method to tell if it needs to place a comma.

You can peek ahead to the next token to see if you need to place a comma, so it's still possible to work. It just means that pkg/json is not a drop in, API compatible replacement.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.