Commit Graph

81 Commits

Author SHA1 Message Date
Dustin Blackman 4c7d6ff4a9
fix GetManyBytes to use byte related methods 2018-10-25 16:34:28 -04:00
tidwall 1e3f6aeaa5 Fix leftover array and map values
fixes #81
2018-08-02 08:58:17 -07:00
Josh Baker f92dbfc6b2 Fix different reuslts on duplicate keys
fixes #79
2018-07-30 14:44:31 -07:00
Josh Baker ba784d767a Fix string output for large integers
This fix makes calling String() on a JSON Number return the original value
as it was represented in the JSON document for signed and unsigned integers.
This ensures that very big (plus-53bit) integers are correctly returned.
Floating points maintain their previous behavior [-+]?[0-9]*\.?[0-9]*.

closes #74
2018-07-10 18:10:33 -07:00
Josh Baker f123b34087 Add appengine support 2018-06-21 11:09:58 -07:00
Josh Baker afaeb95620 Fix false validation
closes #73
2018-06-13 11:46:59 -07:00
speier 3a977634eb valid bytes method 2018-04-27 15:54:16 +02:00
hbc 93d61e6369
Add `arrayOrMap` result description
Explain `Result.Value` will return array and map when possible.
2018-02-28 11:21:22 +08:00
Hexilee 9fa9086994 Result.Bool() can parse 'false' as false 2018-02-23 20:25:48 +08:00
Josh Baker aff9dcea3c uncomment function 2018-02-18 10:09:03 -07:00
Josh Baker c2e370e1b3 comment on Valid 2018-02-18 10:05:23 -07:00
Josh Baker a2f35b522e Added support for JSON Lines
Added support for JSON Lines (http://jsonlines.org) using the `..` prefix.
Which when specified, treats the multi-lined document as an array.

For example:

```
{"name": "Gilbert", "age": 61}
{"name": "Alexa", "age": 34}
{"name": "May", "age": 57}
{"name": "Deloise", "age": 44}
```

```
..#                   >> 4
..1                   >> {"name": "Alexa", "age": 34}
..3                   >> {"name": "Deloise", "age": 44}
..#.name              >> ["Gilbert","Alexa","May","Deloise"]
..#[name="May"].age   >> 57
```

Closes #60
2018-02-09 15:42:42 -07:00
Josh Baker 87033efcae array query mismatch, fixes #58 2018-01-23 05:45:05 -07:00
Josh Baker 5cd723d566 simplify getmanybytes 2017-12-22 07:19:48 -07:00
Josh Baker e62d62a3e1 match GetMany and Get results, fixes #55 2017-12-22 06:58:04 -07:00
Josh Baker 080cd22816 fix mysterious missing result
fixes #54
2017-12-13 16:29:27 -07:00
Josh Baker 182ad76050 Array() from null becomes zero length Go array
fixes #53
2017-12-01 14:13:24 -07:00
Erik Johnston 922b012d22 Fix bug where Result.Raw of literal 'false' was 'f' 2017-11-20 17:59:58 +00:00
Josh Baker ac7b6ae6f2 deprecated unmarshalling 2017-10-18 05:19:20 -07:00
Josh Baker 5a69e67cfd GetMany result value missing, fixes #48 2017-09-25 04:39:06 -07:00
Josh Baker 3c91814cf6 GetMany result incorrect, fixes #47 2017-09-25 04:37:57 -07:00
Josh Baker be96719f99 incomplete surrogate codepoints, fixes #38 2017-08-30 10:08:10 -07:00
Josh Baker ccc7f39b3a added IsObject IsArray helper functions 2017-08-14 08:23:21 -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 2e78916f4a option to disable validation 2017-05-08 17:47:46 -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 635226ae42 minor format updates 2017-04-10 11:41:19 -07:00
Josh Baker 01736e2faf inlined unicode decoding 2017-04-05 10:22:45 -07:00
Josh Baker 458c2c85b8 reorg imports 2017-04-04 10:46:00 -07:00
Josh Baker e0cd4f26a8 proper emoji decoding, fixed #23 2017-04-04 10:42:36 -07:00
Josh Baker e8d1a9ab93 end of path regression, fixes #21
thanks @Poorva17
2017-03-29 08:19:50 -07:00
Josh Baker 9944282cf6 updated comments 2017-03-29 08:18:26 -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 09d1c5c5bc added gjson-safe comment 2017-02-05 09:10:42 -07:00
Josh Baker b0e589ad0b index for iterator vals 2016-12-08 15:00:05 -07:00
Josh Baker 456225d161 key index for ForEach 2016-12-07 16:37:33 -07:00
Josh Baker c1e65a498d added ParseBytes 2016-12-02 11:59:39 -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 1303e83611 fix lead comma error 2016-11-29 18:08:02 -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 a02d704254 added result.Uint() function resolves #9 2016-11-02 08:24:13 -07:00
Josh Baker 27c108f475 fuzzing test on parse 2016-10-18 17:13:15 -07:00