Commit Graph

81 Commits

Author SHA1 Message Date
tidwall 1471a933ec Rename test 2019-11-01 05:15:43 -07:00
tidwall 1c258afe09 Allow for modifiers in sub-selectors 2019-11-01 05:08:48 -07:00
tidwall c5e72cdf74 Fix panic when key starts at-sign 2019-07-15 07:54:43 -07:00
tidwall 1e964df7d9 Support subqueries
It's now possible to do a query like

  topology.instances.#(service_roles.#(=="one"))#.service_version

On a JSON document such as

  {
    "topology": {
      "instances": [{
        "service_version": "1.2.3",
        "service_roles": ["one", "two"]
      },{
        "service_version": "1.2.4",
        "service_roles": ["three", "four"]
      },{
        "service_version": "1.2.2",
        "service_roles": ["one"]
      }]
    }
  }

Resulting in

  ["1.2.3","1.2.2"]
2019-07-12 06:14:45 -07:00
tidwall d7c940e593 Fix missing raw result for array counts 2019-06-30 04:48:53 -07:00
tidwall 3b5bf6bb5e Added Subselectors
It now possible to select multiple independent paths and join
their results into a single JSON document.

For example, given the following JSON

    {
      "info": {
        "friends": [
          {"first": "Dale", "last": "Murphy", "age": 44},
          {"first": "Roger", "last": "Craig", "age": 68},
          {"first": "Jane", "last": "Murphy", "age": 47}
        ]
      }
    }

The path `[info.friends.0.first,info.friends.1.last]` returns

    ["Dale","Craig"]

Or path `{info.friends.0.first,info.friends.1.last}` returns

    {"first":"Dale","last":"Craig"}

You can also rename Object members such as

`{"alt1":info.friends.0.first,"alt2":info.friends.1.last}` returns

    {"alt1":"Dale","alt2":"Craig"}

Finally you can combine this with any GJSON component

`info.friends.[0.first,1.age]` returns

    ["Dale",68]

This feature was request by @errashe in issue #113.
2019-06-29 15:23:32 -07:00
tidwall 6781e4ee59 Allow paren queries 2019-06-29 11:31:27 -07:00
tidwall ebf0b3fb77 Use no path for simple value queries 2019-06-28 10:18:23 -07:00
tidwall 00b15d756e Added querying array values 2019-06-28 05:55:18 -07:00
tidwall d95cbcaa9d Require multiarr results to always exist 2019-06-28 05:24:28 -07:00
tidwall e91a88bec4 Allow for chaining syntax in multi array 2019-06-28 04:41:32 -07:00
tidwall 6eb62ca33f Added new tests for deep selectors 2019-06-27 18:37:09 -07:00
tidwall 7660d0f79f Allow for pipe and dot mixing 2019-06-27 18:27:53 -07:00
tidwall 7b8705a6b6 Use 80 column width 2019-06-27 18:03:46 -07:00
tidwall b877bd43b1 Allow for chaining syntax in array subselects 2019-06-27 17:51:42 -07:00
tidwall 89b19799ff Fix non-existent selectors results 2019-06-27 15:50:15 -07:00
tidwall eee0b6226f Fixed chained array result 2019-02-16 19:06:55 -07:00
tidwall 1ed2249f74 Added modifiers and path chaining
A modifier is a path component that performs custom processing on
the json.

Multiple paths can be "chained" together using the pipe character.
This is useful for getting results from a modified query.

See the README file for more information.
2019-02-16 18:29:39 -07:00
tidwall 5d7556ad3d Valid json optimization
Added ~20% performance boost be removing extra allocation when
Valid() is called with a json string.
2019-02-16 14:50:53 -07:00
tidwall 5a96cfda70 Added GopherJS support 2019-01-14 08:40:04 -07:00
thirstycoda cced0fa719 Added not like operator support to query 2018-10-28 00:20:01 +01: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 afaeb95620 Fix false validation
closes #73
2018-06-13 11:46:59 -07:00
Hexilee 9fa9086994 Result.Bool() can parse 'false' as false 2018-02-23 20:25:48 +08: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 62ee2064df remove commented line 2017-12-22 07:17:19 -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 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 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