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

WIP: Add initial Set implementation #98

Closed
wants to merge 1 commit into from

Conversation

ryanleary
Copy link

@ryanleary ryanleary commented Mar 28, 2017

Description:
This PR starts working toward support for a Set method, enabling end users to use jsonparser to generate json efficiently.

Benchmark before change:

BenchmarkJsonParserLarge-8                    	  100000	     64362 ns/op	       0 B/op	       0 allocs/op
BenchmarkJsonParserMedium-8                   	  500000	     11447 ns/op	       0 B/op	       0 allocs/op
BenchmarkJsonParserEachKeyManualMedium-8      	 1000000	      6699 ns/op	     112 B/op	       2 allocs/op
BenchmarkJsonParserEachKeyStructMedium-8      	 1000000	      7142 ns/op	     704 B/op	      13 allocs/op
BenchmarkJsonParserObjectEachStructMedium-8   	  500000	     11236 ns/op	     656 B/op	      12 allocs/op
BenchmarkJsonParserSmall-8                    	10000000	       993 ns/op	       0 B/op	       0 allocs/op
BenchmarkJsonParserEachKeyManualSmall-8       	10000000	       663 ns/op	      80 B/op	       2 allocs/op
BenchmarkJsonParserEachKeyStructSmall-8       	10000000	      1014 ns/op	     256 B/op	       9 allocs/op
BenchmarkJsonParserObjectEachStructSmall-8    	10000000	      1098 ns/op	     240 B/op	       8 allocs/op

Benchmark after change:

BenchmarkJsonParserLarge-8                    	  100000	     66026 ns/op	       0 B/op	       0 allocs/op
BenchmarkJsonParserMedium-8                   	 1000000	     10733 ns/op	       0 B/op	       0 allocs/op
BenchmarkJsonParserEachKeyManualMedium-8      	 1000000	      6900 ns/op	     112 B/op	       2 allocs/op
BenchmarkJsonParserEachKeyStructMedium-8      	 1000000	      8238 ns/op	     704 B/op	      13 allocs/op
BenchmarkJsonParserObjectEachStructMedium-8   	  500000	     12796 ns/op	     656 B/op	      12 allocs/op
BenchmarkJsonParserSmall-8                    	10000000	      1041 ns/op	       0 B/op	       0 allocs/op
BenchmarkJsonParserEachKeyManualSmall-8       	10000000	       752 ns/op	      80 B/op	       2 allocs/op
BenchmarkJsonParserEachKeyStructSmall-8       	10000000	      1058 ns/op	     256 B/op	       9 allocs/op
BenchmarkJsonParserObjectEachStructSmall-8    	10000000	       975 ns/op	     240 B/op	       8 allocs/op

@ryanleary ryanleary changed the title WIP Add initial Set implementation WIP: Add initial Set implementation Mar 28, 2017
@ryanleary
Copy link
Author

This still has some rough edges, but the spirit of it is right, I think. @buger is this something you'd be interested in merging when complete?

@buger buger mentioned this pull request Apr 24, 2017
if err != nil {
if err != KeyPathNotFoundError {
// problem parsing the data
return []byte{}, err
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this in the Get method too (and likely you are following the existing practice), but what is the reason for initializing a new empty byte slice as suppose to return nil? If an error occurs, it is more idiomatic to return nil so it is clearer in the code.

@bruth
Copy link

bruth commented Apr 26, 2017

I ran the following edge cases:

// output: `{},"foo:null`
jsonparser.Set([]byte("{}"), []byte("null"), "foo")

// output: `{},"foo":{"bar":null}`
jsonparser.Set([]byte("{}"), []byte("null"), "foo", "bar")

@bruth
Copy link

bruth commented Apr 26, 2017

A couple more:

// output: `,"foo":null`
jsonparser.Set([]byte(""), []byte("null"), "foo")

// output: `1.323,"foo":null`
jsonparser.Set([]byte("1.323"), []byte("null"), "foo")

@guillaumerose
Copy link

Just to mention, that this PR enables to modify a json in-place (preserving keys order).
I think this is the only golang library that do that ! Thanks a lot.

@ryanleary
Copy link
Author

Closing this in favor of #102 which @JoshKCarroll has kindly opened to continue this effort.

@ryanleary ryanleary closed this May 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants