Skip to content

Commit 2f53faf

Browse files
committed
REAMDE: add more examples
1 parent 46a9ca9 commit 2f53faf

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ JsonScanner.scan('{"a": 1, "b": 2}', [[JsonScanner::ANY_KEY]], with_path: true)
5656
# => [[[["a"], [6, 7, :number]], [["b"], [14, 15, :number]]]]
5757
```
5858

59-
It supports multiple options
59+
## Options
60+
61+
`JsonScanner` supports multiple options
6062

6163
```ruby
6264
JsonScanner.scan('[0, 42, 0]', [[(1..-1)]], with_path: true)
@@ -81,6 +83,8 @@ JsonScanner.scan('{"a": 1}', [[JsonScanner::ANY_KEY]], with_path: true, symboliz
8183
# => [[[[:a], [6, 7, :number]]]]
8284
```
8385

86+
### Comments in the JSON
87+
8488
Note that the standard `JSON` library supports comments, so you may want to enable it in the `JsonScanner` as well
8589
```ruby
8690
json_str = '{"answer": {"value": 42 /* the Ultimate Question of Life, the Universe, and Everything */ }}'
@@ -90,7 +94,21 @@ end
9094
# => [{"value"=>42}]
9195
```
9296

93-
You can also create a config and reuse it
97+
### Find the end of a JSON string
98+
99+
`allow_trailing_garbage` option may come in handy if you want to extract a JSON string from a JS text
100+
```ruby
101+
script_text = <<~'JS'
102+
<script>window.__APOLLO_STATE__={"ContentItem:0":{"__typename":"ContentItem","id":0, "configurationType":"NO_CONFIGURATION","replacementPartsUrl":null,"relatedCategories":[{"__ref":"Category:109450"},{"__ref":"Category:82044355"},{"__ref":"Category:109441"},{"__ref":"Category:109442"},{"__ref":"Category:109449"},{"__ref":"Category:109444"},{"__ref":"Category:82043730"}],"recommendedOptions":[]}};window.__APPVERSION__=7018;window.__CONFIG_ENV__={value: 'PRODUCTION'};</script>
103+
JS
104+
json_with_trailing_garbage = script_text[/__APOLLO_STATE__\s*=\s*({.+)/, 1]
105+
json_end_pos = JsonScanner.scan(json_with_trailing_garbage, [[]], allow_trailing_garbage: true).first.first[1]
106+
apollo_state = JSON.parse(json_with_trailing_garbage[0...json_end_pos])
107+
```
108+
109+
## Reuse configuration
110+
111+
You can create a `JsonScanner::Config` instance and reuse it between `JsonScanner.scan` calls
94112

95113
```ruby
96114
require "json_scanner"

ext/json_scanner/json_scanner.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,8 @@ VALUE scan_ctx_init(scan_ctx *ctx, VALUE path_ary, VALUE string_keys)
254254
if (string_keys != Qundef)
255255
{
256256
// If string_keys is provided, we need to duplicate the string
257-
// to avoid use-after-free issues and to add the newly created string to the string_keys array
257+
// to avoid use-after-free issues and to add the newly created string to the string_keys array.
258+
// In Ruby 2.2 and newer symbols can be GC-ed, so we need to duplicate them as well.
258259
entry = rb_str_dup(entry);
259260
rb_ary_push(string_keys, entry);
260261
}

0 commit comments

Comments
 (0)