Comments (11)
Can you try this branch https://github.com/buger/jsonparser/tree/key-search, i rewrote how key search works. Thanks!
from jsonparser.
Good point, i have few ideas how to fix it, will keep you updated, thanks!
from jsonparser.
Should be fixed now, for each next key it will limit scope to parent key object.
from jsonparser.
Sorry to say, your patch only fixes the second failing assertion, but not the first (which I think is the more difficult one, sadly).
from jsonparser.
I see what you mean, thank you for pointing. Yes, it is definitely a bit harder, will try to get it fixed.
from jsonparser.
For what it's worth, my suggestion would be to use your parsing functions to more carefully step through the keys in an object. A (very) rough sketch:
- nextValue to seek to a key
- bytes.Equal to compare the key
- if failed, stringEnd to skip the key
- skip whitespace, colon, whitespace
- check type of value, use trailingBracket to skip object, etc.
- Repeat
Unfortunately this won't be nearly as efficient as bytes.Index, so perhaps there is a more efficient way.
from jsonparser.
I found another case that breaks the current parser: key "<anything>\"abc"
: matches a lookup for key "abc". Here's an expanded test case including that:
package jsonparser_test
import (
"github.com/buger-jsonparser"
. "gopkg.in/check.v1"
"testing"
)
func (s *JsonParserTests) TestJsonParserSearchBleed(c *C) {
//c.Skip("jsonparser is broken as of 2016-03-21")
killer := []byte(`{
"parentkey": {
"childkey": {
"grandchildkey": 123
},
"otherchildkey": 123
},
"bad key\"good key": 123,
}`)
var jtype int
_, jtype, _, _ = jsonparser.Get(killer, "childkey")
c.Assert(jtype, Equals, jsonparser.NotExist) // fails, returns data from parentkey.childkey
_, jtype, _, _ = jsonparser.Get(killer, "parentkey", "childkey", "otherchildkey")
c.Assert(jtype, Equals, jsonparser.NotExist) // fails, returns data from parentkey.otherchildkey
_, jtype, _, _ = jsonparser.Get(killer, "good key")
c.Assert(jtype, Equals, jsonparser.NotExist) // fails, returns data from badkey"goodkey
}
// Boilerplate
func Test(t *testing.T) { TestingT(t) }
type JsonParserTests struct{}
var _ = Suite(&JsonParserTests{})
from jsonparser.
(to test branch, create vendor folder and put jsonparser to vendor/github.com/buger/jsonparser, then checkout needed branch there)
from jsonparser.
Thanks, I'll check it in a little while. I'll also make a PR with the tests above for your convenience. Thanks for tackling this!
from jsonparser.
Your key-search branch passes the three cases identified in this issue (test cases added in #7). Thanks!
from jsonparser.
Merged to master, thanks!
from jsonparser.
Related Issues (20)
- how to add item in slice?
- How to break in ArrayEach ?
- Continuing iteration in ArrayEach() HOT 1
- The set function does not work
- package github.com/buger/jsonparser is not a main package HOT 3
- Get call panics on empty json array or invalid json with an empty path - expecting an error to be returned
- [Proposal] New function based on EachKey
- Iterator support for Arrays & Objects
- Dead / Faulty code in ArrayEach? HOT 2
- The maximum number of byte files that ArrayEach supports
- Security - CVE-2020-10675 HOT 5
- Are there plans to release a new version?
- I don`t why it is not work
- how can i get length of array attribute
- ArrayEach correct behaviour
- Delete func data cannot has blank character
- Why were the patch versions for CVE-2020-10675 released so late?
- How to get key by index? HOT 3
- Set with index overrides always first position of the array
- Set() doesnt return a normal json data
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsonparser.