Commit Graph

45 Commits

Author SHA1 Message Date
Josh Baker ccc7f39b3a added IsObject IsArray helper functions 2017-08-14 08:23:21 -07:00
Josh Baker 6daf3373dc Moved benchmark code
Moved benchmark to a different repository to avoid the fetching of
unneeded imports. Please find these benchmarks at
https://github.com/tidwall/gjson-benchmarks
2017-08-14 08:21:16 -07:00
Josh Baker c784c41781 Allow parsing of large integers
This commit fixes an issue in which GJSON was not representing integers
correctly that were greater than 53-bits when calling the result.Int()
and result.Uint() functions. This happened because GJSON stored all
numbers as float64s in the result.Num field, and Int()/Uint() would
simply try to convert the float64 to int64/uint64 by issuing
int64(result.Num) or uint64(result.Num) operations.

Now rather than a simple cast, GJSON checks to see if the float64 is a
whole integer and if the integer can fit within 53-bits. If so, then
the cast method can be used. Otherwise GJSON attempts to parse the
result.Raw directly. If that fails too, it falls back to the original
method.

This fix should maintain compatibility with existing applications.

thanks @joelpresence for reporting
fixes #29
2017-05-25 19:39:18 -07:00
Josh Baker 2555fc0b61 Unmarshal Validation
The Unmarshal function now returns an error if the JSON is not valid.
2017-05-08 17:33:03 -07:00
Josh Baker 3f5adf1ba9 New gjson.Unmarshal function
It's a drop in replacement for json.Unmarshal and you can typically see
a 3 to 4 times boost in performance without the need for external tools
or generators.

This function works almost identically to json.Unmarshal except that
it expects the json to be well-formed prior to being called. Invalid
json will not panic, but it may return back unexpected results.
Therefore the return value of this function will always be nil.

Another difference is that gjson.Unmarshal will automatically attempt
to convert JSON values to any Go type. For example, the JSON string
"100" or the JSON number 100 can be equally assigned to Go string,
int, byte, uint64, etc. This rule applies to all types.
2017-05-07 18:26:54 -07:00
Josh Baker e30a9c1037 Default String() to empty when nonexistent or null
This commit alters the behavior of string handling.

Prior to this change, calling result.String() for nonexistent and null
JSON members would return "null". This runs counter to the zero and omitempty
defaults of Go. Thus I've been seeing in the wild:

    s := result.String()
    if s == "null" || s == "" {
        // ... handle empty string condition
    }

Now we can simply write:

    if result.String() == "" {
        // ... handle empty string condition
    }

It's still possible to explicitly check for null and existence.

    result.Type == gjson.Null
    result.Exists()
2017-04-18 09:28:51 -07:00
Josh Baker 039b641eab added result.Time() function 2017-04-14 17:58:25 -07:00
Josh Baker 6e0babc7e8 updated benchmarks, added features list 2017-04-14 11:13:32 -07:00
Josh Baker 7c63fbd56a better test coverage 2017-04-11 08:54:19 -07:00
Josh Baker 92a0497357 reduced some complexity 2017-04-10 20:45:34 -07:00
Josh Baker 635226ae42 minor format updates 2017-04-10 11:41:19 -07:00
Josh Baker 20d521de6e added emoji test 2017-04-09 19:40:55 -07:00
Josh Baker e8d1a9ab93 end of path regression, fixes #21
thanks @Poorva17
2017-03-29 08:19:50 -07:00
Josh Baker 256887a8aa Fix for invalid matching on prefixed key
Thanks to @Poorva17 for finding this issue.
Fixes #20
2017-03-28 17:04:10 -07:00
Josh Baker 7afd24f7a2 added != comparison operator 2016-11-30 14:38:08 -07:00
Josh Baker 90669a0cbe query matching with % 2016-11-30 14:32:17 -07:00
Josh Baker 62892351c5 added ForEach function 2016-11-30 10:50:59 -07:00
Josh Baker 86b1b630e4 Query array for multiple matches
It's now possible to query an array for multiple matches by adding the
'#' character immediately following the query.

For example, using the following JSON:

  {
    "friends": [
      {"first": "Dale", "last": "Murphy"},
      {"first": "Roger", "last": "Craig"},
      {"first": "Jane", "last": "Murphy"}
    ]
  }

To return the first match:

  `friends.#[last="Murphy"].first` >> "Dale"

To return all matches:

  `friends.#[last="Murphy"]#.first` >> ["Dale","Jane"]

Thanks to @chuttam for requesting this feature, closes #15.
2016-11-30 07:59:24 -07:00
Josh Baker ae5d307631 Added GetMany. Query many paths at once.
The `GetMany(json, paths...)` function can be used to get multiple
values at one time from the same json string.
This is preferrable to calling `Get(json, path)` over and over.
It's also optimized to scan over a JSON payload once.

This addresses a feature request by @FZambia, and closes #13.
2016-11-28 15:10:11 -07:00
Josh Baker ac4cd1ab55 empty arrays for non-existent values #11 2016-11-02 13:45:25 -07:00
William Poussier 495633298f Modify behavior of Array() on non-array result.
Closes #10.
2016-11-02 20:20:24 +01:00
Josh Baker 27c108f475 fuzzing test on parse 2016-10-18 17:13:15 -07:00
Josh Baker 7c631e9868 fix out of range error 2016-09-08 09:02:40 -07:00
Josh Baker b39aa6075f bytes substring optimization 2016-09-08 08:34:01 -07:00
Josh Baker 124a25db9e added direct bytes interface 2016-09-08 08:08:53 -07:00
Josh Baker b7e578ec72 added extra test 2016-08-31 13:38:53 -07:00
Josh Baker f40fe4ac37 subquery syntax 2016-08-31 13:23:20 -07:00
Josh Baker 4d7d1a76a8 import match package 2016-08-30 07:21:20 -07:00
Josh Baker 550c66c276 added reset timer to all benchmarks 2016-08-27 06:20:43 -07:00
Josh Baker 19f9404e51 Updated logic for better ssa compilation in Go 1.7 2016-08-25 07:22:48 -07:00
Josh Baker 050904424f added unmarshal map test 2016-08-24 15:59:54 -07:00
Josh Baker f4c008206b added wildcard test 2016-08-24 15:50:18 -07:00
Josh Baker 4fceff029c unicode patterns 2016-08-24 13:26:44 -07:00
Josh Baker 67e38154bd sped up map[string]interface{} unmarshalling 2016-08-24 12:12:07 -07:00
Josh Baker c6faea1947 addtional test 2016-08-22 08:26:44 -07:00
Josh Baker 16ecfe5970 addtional test 2016-08-22 08:25:33 -07:00
Josh Baker cd422a3e10 Removed Multi. Added Parse and result.Get funcs
The Multi field was too bulky. fixes #4
Added a Parse(json) function that will do a simple parse of json.
Added a result.Get(path) function that returns a child result.
Added Bool(), Int(), and Float() to result type. fixes #5
2016-08-22 06:05:51 -07:00
Josh Baker 0757a4d1e7 get nested array values, fixes #4
To get all the friends last names use:

"friends.#.last"

{
  "friends": [
    {"first": "James", "last": "Murphy"},
    {"first": "Roger", "last": "Craig"}
  ]
}
2016-08-21 06:57:13 -07:00
Josh Baker 0e6e567424 optimistic optimizations 2016-08-19 11:22:59 -07:00
Josh Baker 8be3ed7605 reset count on append, fixes #3 2016-08-15 04:56:55 -07:00
Josh Baker 725f3caa55 exists function 2016-08-12 08:39:08 -07:00
Josh Baker 1e941a433e added path escaping 2016-08-11 18:51:29 -07:00
Josh Baker 06af1af34e fixed benchmark bug with jsonparser
Big thanks to dlsspy for pointing this out.
2016-08-11 18:15:15 -07:00
Josh Baker 9a63765fe5 added jsonparser benchmark 2016-08-11 10:53:50 -07:00
Josh Baker f4afb106da first commit 2016-08-11 07:50:34 -07:00