Git Product home page Git Product logo

Comments (11)

buger avatar buger commented on July 22, 2024 3

Can you try this branch https://github.com/buger/jsonparser/tree/key-search, i rewrote how key search works. Thanks!

from jsonparser.

buger avatar buger commented on July 22, 2024

Good point, i have few ideas how to fix it, will keep you updated, thanks!

from jsonparser.

buger avatar buger commented on July 22, 2024

Should be fixed now, for each next key it will limit scope to parent key object.

from jsonparser.

daboyuka avatar daboyuka commented on July 22, 2024

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.

buger avatar buger commented on July 22, 2024

I see what you mean, thank you for pointing. Yes, it is definitely a bit harder, will try to get it fixed.

from jsonparser.

daboyuka avatar daboyuka commented on July 22, 2024

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:

  1. nextValue to seek to a key
  2. bytes.Equal to compare the key
  3. if failed, stringEnd to skip the key
  4. skip whitespace, colon, whitespace
  5. check type of value, use trailingBracket to skip object, etc.
  6. Repeat

Unfortunately this won't be nearly as efficient as bytes.Index, so perhaps there is a more efficient way.

from jsonparser.

daboyuka avatar daboyuka commented on July 22, 2024

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.

buger avatar buger commented on July 22, 2024

(to test branch, create vendor folder and put jsonparser to vendor/github.com/buger/jsonparser, then checkout needed branch there)

from jsonparser.

daboyuka avatar daboyuka commented on July 22, 2024

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.

daboyuka avatar daboyuka commented on July 22, 2024

Your key-search branch passes the three cases identified in this issue (test cases added in #7). Thanks!

from jsonparser.

buger avatar buger commented on July 22, 2024

Merged to master, thanks!

from jsonparser.

Related Issues (20)

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.