@@ -56,7 +56,9 @@ JsonScanner.scan('{"a": 1, "b": 2}', [[JsonScanner::ANY_KEY]], with_path: true)
56
56
# => [[[["a"], [6, 7, :number]], [["b"], [14, 15, :number]]]]
57
57
```
58
58
59
- It supports multiple options
59
+ ## Options
60
+
61
+ ` JsonScanner ` supports multiple options
60
62
61
63
``` ruby
62
64
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
81
83
# => [[[[:a], [6, 7, :number]]]]
82
84
```
83
85
86
+ ### Comments in the JSON
87
+
84
88
Note that the standard ` JSON ` library supports comments, so you may want to enable it in the ` JsonScanner ` as well
85
89
``` ruby
86
90
json_str = ' {"answer": {"value": 42 /* the Ultimate Question of Life, the Universe, and Everything */ }}'
90
94
# => [{"value"=>42}]
91
95
```
92
96
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
94
112
95
113
``` ruby
96
114
require " json_scanner"
0 commit comments