Commit Graph

132 Commits

Author SHA1 Message Date
tidwall 5ab551f3ac Add flag for disabling HTML escaping
Adds the DisableEscapeHTML flag for disable the automatic
escaping of the HTML characters '>', '<' and '&'.

The previous commit introduced a potentially breaking change by
removing HTML escaping altogether. This commit fixes that issue
by allowing the user to choose at runtime.
2024-07-30 04:47:27 -07:00
tidwall 28d458b14c Disable html escaping
This commit removes the automatic escaping of html characters,
effectively rendering JSON strings in the same way as is the
builtin Go encoder with SetEscapeHTML(false).
This should not affect the quality of the resulting JSON and
hopefully will not cause any downstream issues.
2024-07-29 07:08:06 -07:00
tidwall bbf40bb0e4 Fix backspace and form-feed for Go 1.22 2024-02-14 20:51:40 -07:00
Kai A. Hiller 0f87896dc3 Test \b and \f JSON encoding explicitly 2024-02-14 21:15:12 +01:00
tidwall 6ee9f877d6 Added Escape function
This commit adds the Escape function for escaping a path
component, making it possible to directly querying keys that have
special characters like dots.

```
json := `{
  "user":{
      "first.name": "Janet",
      "last.name": "Prichard"
    }
}`
user := gjson.Get(json, "user")
println(user.Get(gjson.Escape("first.name")).String())
println(user.Get(gjson.Escape("last.name")).String())
// Output:
// Janet
// Prichard
```

See #333
2023-09-22 10:13:56 -07:00
tidwall e8e87f2a00 Add @dig modifier for recursive descent searches
This commit adds the "@dig" modifier, which allows for searching
for values in deep or arbitrarily nested json documents

For example, using the following json:

```
{ "something": {
    "anything": {
      "abcdefg": {
          "finally": {
            "important": {
                "secret": "password"
            }
        }
      }
    }
  }
}
```

```
@dig:secret  ->  ["password"]
```

See #130
2023-08-09 14:54:46 -07:00
tidwall 8d2c36ffa4 Added new tilde types and fixed ~false
This commit fixes an issue with ~false where the it's value was
simply the opposite of ~true. Now ~false explicitly checks for
false-ish values.

Also added ~null and ~* for testing null-ish and non-existent
values.

see #327
2023-07-27 06:23:49 -07:00
tidwall e14b8d3708 Add test 2022-11-21 18:54:56 -07:00
Tomasz Janiszewski 9e848707d6
Handle modifiers with options 2022-08-09 15:41:28 +02:00
tidwall 475b4036c3 Allow for Index > 0 on path compontent that are not modifiers.
This commit fixes an issue where non-modifier path components
such as '@hello' return 0 for the Result.Index value.
2022-08-04 18:06:36 -07:00
L2ncE 72953c7472 Fix a syntax error in a code comment 2022-06-15 16:59:08 +08:00
tidwall c3bb2c39ba Remove encoding/json dependency
The only purpose of using the built-in Go was to encode json
strings that had unicode or needed to escaped.

This commit adds the new function `AppendJSONString` which allows
for appending strings as their json representation to a byte
slice.

It's about 2x faster than using json.Marshal.
2022-04-19 15:14:33 -07:00
tidwall e4fc67c92a Added group modifier
The new "@group" modifier allows for grouping arrays of objects.

For example, using the "@group" modifier on the following json...

  {"id":["123","456","789"],"val":[2,1]}

will results in...

  [{"id":"123","val":2},{"id":"456","val":1},{"id":"789"}]
2022-02-02 04:43:04 -07:00
tidwall 38071ea7f2 Add tostr and fromstr modifiers
For wrapping and unwrapping json strings
2022-01-13 09:15:39 -07:00
tidwall db0033701c Set array index as key for ForEach
See #248
2021-11-30 17:20:07 -07:00
tidwall d3a134957c Fix modifier bug in multipath selector
Closes #253
2021-11-25 13:57:06 -07:00
tidwall 6b6af2ad5e Added new static value character
You can use the '!' character to define static json as a path
component.

For example,

  {name.last,"foo":!"bar"} => {name.last,"foo":"bar"}

see #249
2021-11-10 09:18:18 -07:00
tidwall 2c9fd2476a Added Path and Paths for getting the original path of a Result
This commit adds a two new functions of the Result type:

- Result.Path:  Returns the original path of a `Result` that was
                returned from a simple `Get` operation.
- Result.Paths: Returns the original paths of a `Result` that was
                returned from a `Get` operation with a query.

See issue #206 for more details
2021-10-29 17:30:57 -07:00
tidwall 7cadbb5756 Ensure Parse handles NaN/Inf and returns correct Index 2021-10-28 09:09:00 -07:00
tidwall 0b52f9a361 Fix empty string operator not matching
fixes #246
2021-10-25 07:11:04 -07:00
tidwall 5784e4879e cleanup test 2021-10-22 04:05:41 -07:00
tidwall 0cbc0f402f Accept NaN/Inf
This commit allows for NaN and Inf numbers.

see #242
2021-10-22 04:00:22 -07:00
tidwall 35fa0d71c8 Added @keys and @values modifiers
The "@keys" and "@values" modifiers converts an object into an
array of its keys or values respectively.

Take this json for example:

{"first":"Tom","last":"Smith"}

@keys   -> ["first","last"]
@values -> ["Tom","Smith"]

This feature was requested in #161.
2021-10-20 16:31:29 -07:00
tidwall 4fe1916c56 The Array method should return back one item for JSON objects.
This commit fixes an issue where the Array method was not
returning single value arrays when the reciever Result was a
JSON Object.

fixes #240
2021-10-12 10:52:30 -07:00
tidwall 77a57fda87 Limit the complexity of "like" queries that match on a pattern.
This commit adds the uses the MatchLimit function, which it the
same as Match but will limit the complexity of the input pattern.
This is to avoid long running matches, specifically to avoid ReDos
attacks from arbritary inputs.
2021-10-08 07:42:49 -07:00
tidwall 44b8c19d87 Minor update to test 2021-09-01 07:24:15 -07:00
Sebastian Spaink 7405f21134 Rename to indexes
Set to nil when modifiers used
2021-07-09 18:15:56 -05:00
Bas 7460ecfe69 Fix test 2021-07-01 06:00:44 -05:00
Bas 9b49f6eef5 Undo unnecessary change 2021-07-01 05:58:26 -05:00
Bas 73b86a9fc9 Support queries 2021-07-01 05:58:24 -05:00
Sebastian Spaink 34ea511746 Support getting index for hastags 2021-07-01 05:57:36 -05:00
tidwall 2feb4037b4 Fix escaped strings missing double quote
fixes #223
2021-06-30 13:18:28 -07:00
tidwall 7a0721334f Fix syntax 2021-05-14 09:02:12 -07:00
tidwall d7dbdd1d92 Add tilde boolean operator
This commit adds the new tilde '~' operator, which when used will
convert a value to a boolean before comparison.

For example, using the following JSON:

    {
      "vals": [
        { "a": 1, "b": true },
        { "a": 2, "b": true },
        { "a": 3, "b": false },
        { "a": 4, "b": "0" },
        { "a": 5, "b": 0 },
        { "a": 6, "b": "1" },
        { "a": 7, "b": 1 }
        { "a": 8, "b": "true" },
        { "a": 9, "b": false }
        { "a": 10, "b": null }
        { "a": 11 }
      ]
    }

You can now query for all true(ish) values:

    vals.#(b==~true)#

Which returns:

    [1,2,6,7,8]

Or all false(ish) values:

    vals.#(b==~false)#

Which returns:

    [3,4,5,9,10,11]

The last value which was non-existent is treated as "false"
2021-05-14 08:53:58 -07:00
tidwall d9710733f0 Allow escaped string characters in query values
Fixes #215
2021-04-19 09:43:16 -07:00
tidwall d6d786db3c Remove dead code 2021-04-19 09:18:03 -07:00
tidwall dee0375ffd Fix negative without int part for valid
fixes #210
2021-03-28 18:30:25 -07:00
tidwall d6cb589fc4 Fix non-existent response from multires query on empty array
This commit fixes an issue where a multires query on an empty
array will result in a non-existent (empty string) result.

For example `Get("[]", "#(key=value)#").Raw` resulted in an
empty string. But, it should actually result in the empty
array `[]`.
2021-03-25 15:31:00 -07:00
tidwall c75c954102 Fix mod flatten including non-existent arrays 2021-03-25 08:28:15 -07:00
tidwall d18e16d152 Allow chaining multipaths using the dot 2021-03-22 04:19:47 -07:00
tidwall 97ec619cbe Restrict pretty indent and prefixes to whitespace 2020-12-25 06:42:20 -07:00
tidwall bf4efcb3c1 Fix slice out of bounds panic
fixes #196
2020-12-24 09:51:53 -07:00
tidwall 9f58baa7a6 Update the match package
This commit updates the match package to v1.0.3, which includes
a fix to an issue where a pattern with lots of repetitive stars
will increasingly slow down a Match operation.

Fixes #195
2020-12-23 05:00:23 -07:00
tidwall f0ee9ebde4 Fix bounds out of range panic
This commit fixes an issues where gjson panics when the input json
or path ends in incomplete quoted string.

fixes #192
2020-12-07 05:25:20 -07:00
tidwall 2f043b7abd Adjusted test 2020-11-04 16:01:22 -07:00
Jeffrey Koehler 78a59792a3 add test of different strings
Added test for each of the different accepted values for ParseBool, and a few arbitrary results that should return false.
2020-05-30 06:34:47 -04:00
tidwall 0360deb6d8 Added new modifiers
`@flatten` Flattens an array with child arrays.
  [1,[2],[3,4],[5,[6,7]]] -> [1,2,3,4,5,[6,7]]
The {"deep":true} arg can be provide for deep flattening.
  [1,[2],[3,4],[5,[6,7]]] -> [1,2,3,4,5,6,7]
The original json is returned when the json is not an array.

`@join` Joins multiple objects into a single object.
  [{"first":"Tom"},{"last":"Smith"}] -> {"first","Tom","last":"Smith"}
The arg can be "true" to specify that duplicate keys should be preserved.
  [{"first":"Tom","age":37},{"age":41}] -> {"first","Tom","age":37,"age":41}
Without preserved keys:
  [{"first":"Tom","age":37},{"age":41}] -> {"first","Tom","age":41}
The original json is returned when the json is not an object.

`@valid` Ensures that the json is valid before moving on. An
empty string is returned when the json is not valid, otherwise
it returns the original json.
2020-02-10 11:13:30 -07:00
tidwall d10932a0d0 Removed unmarshalling 2020-01-20 12:32:37 -07:00
aeneasr 8e8823353c
Add @this modifier
This modifier returns the current element as-is and can be used
to retrieve the JSON document itself. It is equivalent to the `#/` JSON Pointer.

Closes #149
2020-01-20 15:01:54 +01:00
tidwall 5c2e4b3824 Fixed modifier pipe issue
This commit fixes an issue where chaining modifiers that used a
string arg would fail to process the modifier following the first.

fixes #143
2019-11-18 09:51:37 -07:00