Skip to content

Commit 140e84d

Browse files
committed
ref README
1 parent 171af2b commit 140e84d

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

README.md

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,22 @@ end
3535
question_path = ["data", "question"]
3636
answers_path = ["data", "answers", (0..-1)]
3737
json_str = '{"data": {"question": "the Ultimate Question of Life, the Universe, and Everything", "answers": [42, 0, 420]}}'
38-
questions_pos, answers_pos = JsonScanner.scan(json_str, [question_path, answers_path])
39-
question_pos = questions_pos.first
38+
(question_pos, ), answers_pos = JsonScanner.scan(json_str, [question_path, answers_path])
4039
question = JSON.parse(json_str.byteslice(question_pos[0]...question_pos[1]), quirks_mode: true)
4140
# => "the Ultimate Question of Life, the Universe, and Everything"
42-
answers = answers_pos.map { |begin_pos, end_pos, _type| JSON.parse(json_str.byteslice(begin_pos...end_pos), quirks_mode: true) }
41+
answers = answers_pos.map do |begin_pos, end_pos, _type|
42+
JSON.parse(json_str.byteslice(begin_pos...end_pos), quirks_mode: true)
43+
end
4344
# => [42, 0, 420]
4445

4546
# Result contains byte offsets, you need to be careful when working with non-binary strings
4647
emoji_json = '{"grin": "😁", "heart": "😍", "rofl": "🤣"}'
4748
begin_pos, end_pos, = JsonScanner.scan(emoji_json, [["heart"]]).first.first
4849
emoji_json.byteslice(begin_pos...end_pos)
4950
# => "\"😍\""
50-
# Note: You most likely don't need the `quirks_mode` option unless you are using an older version
51-
# of Ruby with the stdlib - or just also old - version of the json gem. In newer versions, `quirks_mode` is enabled by default.
51+
# Note: You most likely don't need the `quirks_mode` option unless you are using
52+
# an older version of Ruby with the stdlib - or just also old - version of the json gem.
53+
# In newer versions, `quirks_mode` is enabled by default.
5254
JSON.parse(emoji_json.byteslice(begin_pos...end_pos), quirks_mode: true)
5355
# => "😍"
5456
# You can also do this
@@ -102,15 +104,21 @@ JsonScanner.scan('[0, /* answer */ 42, 0]', [[(1..-1)]], allow_comments: true)
102104
# => [[[17, 19, :number], [21, 22, :number]]]
103105
JsonScanner.scan("\"\x81\x83\"", [[]], dont_validate_strings: true)
104106
# => [[[0, 4, :string]]]
105-
JsonScanner.scan("{\"\x81\x83\": 42}", [[JsonScanner::ANY_KEY]], dont_validate_strings: true, with_path: true)
107+
JsonScanner.scan(
108+
"{\"\x81\x83\": 42}", [[JsonScanner::ANY_KEY]],
109+
dont_validate_strings: true, with_path: true.
110+
)
106111
# => [[[["\x81\x83"], [7, 9, :number]]]]
107112
JsonScanner.scan('[0, 42, 0]garbage', [[(1..-1)]], allow_trailing_garbage: true)
108113
# => [[[4, 6, :number], [8, 9, :number]]]
109114
JsonScanner.scan('[0, 42, 0] [0, 34]', [[(1..-1)]], allow_multiple_values: true)
110115
# => [[[4, 6, :number], [8, 9, :number], [16, 18, :number]]]
111116
JsonScanner.scan('[0, 42, 0', [[(1..-1)]], allow_partial_values: true)
112117
# => [[[4, 6, :number], [8, 9, :number]]]
113-
JsonScanner.scan('{"a": 1}', [[JsonScanner::ANY_KEY]], with_path: true, symbolize_path_keys: true)
118+
JsonScanner.scan(
119+
'{"a": 1}', [[JsonScanner::ANY_KEY]],
120+
with_path: true, symbolize_path_keys: true,
121+
)
114122
# => [[[[:a], [6, 7, :number]]]]
115123
```
116124

@@ -119,7 +127,9 @@ JsonScanner.scan('{"a": 1}', [[JsonScanner::ANY_KEY]], with_path: true, symboliz
119127
Note that the standard `JSON` library supports comments, so you may want to enable it in the `JsonScanner` as well
120128
```ruby
121129
json_str = '{"answer": {"value": 42 /* the Ultimate Question of Life, the Universe, and Everything */ }}'
122-
JsonScanner.scan(json_str, [["answer"]], allow_comments: true).first.map do |begin_pos, end_pos, _type|
130+
JsonScanner.scan(
131+
json_str, [["answer"]], allow_comments: true.
132+
).first.map do |begin_pos, end_pos, _type|
123133
JSON.parse(json_str.byteslice(begin_pos...end_pos), quirks_mode: true)
124134
end
125135
# => [{"value"=>42}]
@@ -133,7 +143,9 @@ script_text = <<~'JS'
133143
<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>
134144
JS
135145
json_with_trailing_garbage = script_text[/__APOLLO_STATE__\s*=\s*({.+)/, 1]
136-
json_end_pos = JsonScanner.scan(json_with_trailing_garbage, [[]], allow_trailing_garbage: true).first.first[1]
146+
json_end_pos = JsonScanner.scan(
147+
json_with_trailing_garbage, [[]], allow_trailing_garbage: true,
148+
).first.first[1]
137149
apollo_state = JSON.parse(json_with_trailing_garbage[0...json_end_pos])
138150
```
139151

0 commit comments

Comments
 (0)