mocktail 2.0.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (279) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/main.yml +2 -2
  3. data/CHANGELOG.md +5 -0
  4. data/Gemfile +3 -2
  5. data/Gemfile.lock +76 -82
  6. data/README.md +2 -0
  7. data/docs/code_of_conduct.md +8 -0
  8. data/docs/configuring_minitest.md +20 -0
  9. data/docs/configuring_rspec.md +19 -0
  10. data/docs/example_test.md +594 -0
  11. data/docs/faq/existing_tests.md +47 -0
  12. data/docs/faq/mocking_http.md +151 -0
  13. data/docs/faq/mocking_the_subject.md +161 -0
  14. data/docs/faq/mocking_time.md +34 -0
  15. data/docs/faq/partial_mocks.md +34 -0
  16. data/docs/faq/verifying_real_interactions.md +52 -0
  17. data/docs/img/delegator_tree.png +0 -0
  18. data/docs/img/example_test.png +0 -0
  19. data/docs/img/extract_transform_load.png +0 -0
  20. data/docs/img/mocktail_sorbet.jpg +0 -0
  21. data/docs/img/mocktail_untyped.jpg +0 -0
  22. data/docs/img/spacer.png +0 -0
  23. data/docs/installation_sorbet.md +99 -0
  24. data/docs/installation_untyped.md +29 -0
  25. data/docs/other_uses.md +32 -0
  26. data/docs/stubbing_and_verifying.md +515 -0
  27. data/docs/support/api.md +819 -0
  28. data/docs/support/example_test.rb +94 -0
  29. data/docs/support/glossary.md +426 -0
  30. data/docs/tdd/class_methods.md +44 -0
  31. data/docs/tdd/poro/dependency_inception.md +136 -0
  32. data/docs/tdd/poro/dependency_injection.md +82 -0
  33. data/docs/tdd/poro.md +15 -0
  34. data/docs/tdd/third_party.md +220 -0
  35. data/docs/tdd.md +15 -0
  36. data/lib/mocktail/debug.rb +4 -3
  37. data/lib/mocktail/share/stringifies_call.rb +0 -1
  38. data/lib/mocktail/simulates_argument_error/transforms_params.rb +2 -2
  39. data/lib/mocktail/sorbet/mocktail/debug.rb +4 -3
  40. data/lib/mocktail/sorbet/mocktail/share/stringifies_call.rb +0 -1
  41. data/lib/mocktail/sorbet/mocktail/simulates_argument_error/transforms_params.rb +2 -2
  42. data/lib/mocktail/sorbet/mocktail/value/call.rb +1 -1
  43. data/lib/mocktail/sorbet/mocktail/version.rb +1 -1
  44. data/lib/mocktail/value/call.rb +1 -1
  45. data/lib/mocktail/version.rb +1 -1
  46. data/mocktail.gemspec +2 -2
  47. data/rbi/mocktail-pregenerated.rbi +58 -0
  48. data/script/build +47 -0
  49. data/script/setup +17 -0
  50. data/script/spoom_me +7 -0
  51. data/script/strip_sigils +17 -0
  52. data/script/test +33 -0
  53. data/script/test_double_require_warnings +46 -0
  54. data/script/update +21 -0
  55. data/sorbet/config +12 -0
  56. data/sorbet/rbi/annotations/rainbow.rbi +269 -0
  57. data/sorbet/rbi/gems/ast@2.4.2.rbi +584 -0
  58. data/sorbet/rbi/gems/diff-lcs@1.5.0.rbi +1083 -0
  59. data/sorbet/rbi/gems/docile@1.4.0.rbi +376 -0
  60. data/sorbet/rbi/gems/json@2.6.3.rbi +1533 -0
  61. data/sorbet/rbi/gems/language_server-protocol@3.17.0.3.rbi +14237 -0
  62. data/sorbet/rbi/gems/lint_roller@1.0.0.rbi +230 -0
  63. data/sorbet/rbi/gems/m@1.6.1.rbi +257 -0
  64. data/sorbet/rbi/gems/method_source@1.0.0.rbi +272 -0
  65. data/sorbet/rbi/gems/minitest@5.18.0.rbi +1491 -0
  66. data/sorbet/rbi/gems/netrc@0.11.0.rbi +158 -0
  67. data/sorbet/rbi/gems/parallel@1.23.0.rbi +273 -0
  68. data/sorbet/rbi/gems/parser@3.2.2.1.rbi +7253 -0
  69. data/sorbet/rbi/gems/rainbow@3.1.1.rbi +402 -0
  70. data/sorbet/rbi/gems/rake@13.0.6.rbi +3024 -0
  71. data/sorbet/rbi/gems/rbi@0.0.16.rbi +3049 -0
  72. data/sorbet/rbi/gems/regexp_parser@2.8.0.rbi +3748 -0
  73. data/sorbet/rbi/gems/rexml@3.2.5.rbi +4717 -0
  74. data/sorbet/rbi/gems/rubocop-ast@1.29.0.rbi +7003 -0
  75. data/sorbet/rbi/gems/rubocop-performance@1.18.0.rbi +8 -0
  76. data/sorbet/rbi/gems/rubocop-sorbet@0.7.0.rbi +1043 -0
  77. data/sorbet/rbi/gems/rubocop@1.52.0.rbi +55500 -0
  78. data/sorbet/rbi/gems/ruby-progressbar@1.13.0.rbi +1317 -0
  79. data/sorbet/rbi/gems/simplecov-html@0.12.3.rbi +216 -0
  80. data/sorbet/rbi/gems/simplecov@0.22.0.rbi +2148 -0
  81. data/sorbet/rbi/gems/simplecov_json_formatter@0.1.4.rbi +8 -0
  82. data/sorbet/rbi/gems/spoom@1.2.1.rbi +2536 -0
  83. data/sorbet/rbi/gems/standard-custom@1.0.1.rbi +63 -0
  84. data/sorbet/rbi/gems/standard-performance@1.1.0.rbi +8 -0
  85. data/sorbet/rbi/gems/standard@1.29.0.rbi +850 -0
  86. data/sorbet/rbi/gems/tapioca@0.11.6.rbi +3301 -0
  87. data/sorbet/rbi/gems/thor@1.2.2.rbi +3965 -0
  88. data/sorbet/rbi/gems/unicode-display_width@2.4.2.rbi +65 -0
  89. data/sorbet/rbi/gems/unparser@0.6.7.rbi +4524 -0
  90. data/sorbet/rbi/gems/yard-sorbet@0.8.1.rbi +428 -0
  91. data/sorbet/rbi/gems/yard@0.9.34.rbi +18219 -0
  92. data/sorbet/tapioca/config.yml +13 -0
  93. data/sorbet/tapioca/require.rb +4 -0
  94. data/spoom_data/05f0c49.json +1 -0
  95. data/spoom_data/1657012.json +1 -0
  96. data/spoom_data/167434b.json +1 -0
  97. data/spoom_data/1d0ba5b.json +1 -0
  98. data/spoom_data/1ffa724.json +1 -0
  99. data/spoom_data/2129b3d.json +1 -0
  100. data/spoom_data/24b1c92.json +1 -0
  101. data/spoom_data/2c46aee.json +1 -0
  102. data/spoom_data/305ec0b.json +1 -0
  103. data/spoom_data/30e9528.json +1 -0
  104. data/spoom_data/4638cd5.json +1 -0
  105. data/spoom_data/47c7dad.json +1 -0
  106. data/spoom_data/4b1edef.json +1 -0
  107. data/spoom_data/4de157f.json +1 -0
  108. data/spoom_data/526e7db.json +1 -0
  109. data/spoom_data/5d093b9.json +1 -0
  110. data/spoom_data/5db3b43.json +1 -0
  111. data/spoom_data/5fe2a65.json +1 -0
  112. data/spoom_data/6891312.json +1 -0
  113. data/spoom_data/6b0fef4.json +1 -0
  114. data/spoom_data/6b83d12.json +1 -0
  115. data/spoom_data/74c83c2.json +1 -0
  116. data/spoom_data/7644ff4.json +1 -0
  117. data/spoom_data/79054db.json +1 -0
  118. data/spoom_data/814e515.json +1 -0
  119. data/spoom_data/88c3b60.json +1 -0
  120. data/spoom_data/8bd4b6e.json +1 -0
  121. data/spoom_data/93f8153.json +1 -0
  122. data/spoom_data/95242fe.json +1 -0
  123. data/spoom_data/97f4c09.json +1 -0
  124. data/spoom_data/a13d150.json +1 -0
  125. data/spoom_data/a17f215.json +1 -0
  126. data/spoom_data/b705a9d.json +1 -0
  127. data/spoom_data/ba19195.json +1 -0
  128. data/spoom_data/bef51ca.json +1 -0
  129. data/spoom_data/dbb595a.json +1 -0
  130. data/spoom_data/e8fab92.json +1 -0
  131. data/spoom_data/f166c87.json +1 -0
  132. data/spoom_data/f57992d.json +1 -0
  133. data/spoom_data/f5a1e40.json +1 -0
  134. data/spoom_data/f6d6431.json +1 -0
  135. data/spoom_data/f72b67c.json +1 -0
  136. data/spoom_data/fc2f231.json +1 -0
  137. data/src/mocktail/collects_calls.rb +18 -0
  138. data/src/mocktail/debug.rb +55 -0
  139. data/src/mocktail/dsl.rb +46 -0
  140. data/src/mocktail/errors.rb +19 -0
  141. data/src/mocktail/explains_nils.rb +41 -0
  142. data/src/mocktail/explains_thing.rb +137 -0
  143. data/src/mocktail/grabs_original_method_parameters.rb +33 -0
  144. data/src/mocktail/handles_dry_call/fulfills_stubbing/describes_unsatisfied_stubbing.rb +27 -0
  145. data/src/mocktail/handles_dry_call/fulfills_stubbing/finds_satisfaction.rb +24 -0
  146. data/src/mocktail/handles_dry_call/fulfills_stubbing.rb +45 -0
  147. data/src/mocktail/handles_dry_call/logs_call.rb +12 -0
  148. data/src/mocktail/handles_dry_call/validates_arguments.rb +45 -0
  149. data/src/mocktail/handles_dry_call.rb +25 -0
  150. data/src/mocktail/handles_dry_new_call.rb +42 -0
  151. data/src/mocktail/imitates_type/ensures_imitation_support.rb +16 -0
  152. data/src/mocktail/imitates_type/makes_double/declares_dry_class/reconstructs_call.rb +73 -0
  153. data/src/mocktail/imitates_type/makes_double/declares_dry_class.rb +136 -0
  154. data/src/mocktail/imitates_type/makes_double/gathers_fakeable_instance_methods.rb +28 -0
  155. data/src/mocktail/imitates_type/makes_double.rb +29 -0
  156. data/src/mocktail/imitates_type.rb +29 -0
  157. data/src/mocktail/initialize_based_on_type_system_mode_switching.rb +11 -0
  158. data/src/mocktail/initializes_mocktail.rb +25 -0
  159. data/src/mocktail/matcher_presentation.rb +21 -0
  160. data/src/mocktail/matchers/any.rb +27 -0
  161. data/src/mocktail/matchers/base.rb +39 -0
  162. data/src/mocktail/matchers/captor.rb +76 -0
  163. data/src/mocktail/matchers/includes.rb +32 -0
  164. data/src/mocktail/matchers/includes_hash.rb +12 -0
  165. data/src/mocktail/matchers/includes_key.rb +12 -0
  166. data/src/mocktail/matchers/includes_string.rb +12 -0
  167. data/src/mocktail/matchers/is_a.rb +17 -0
  168. data/src/mocktail/matchers/matches.rb +19 -0
  169. data/src/mocktail/matchers/not.rb +17 -0
  170. data/src/mocktail/matchers/numeric.rb +27 -0
  171. data/src/mocktail/matchers/that.rb +32 -0
  172. data/src/mocktail/matchers.rb +19 -0
  173. data/src/mocktail/raises_neato_no_method_error.rb +93 -0
  174. data/src/mocktail/records_demonstration.rb +43 -0
  175. data/src/mocktail/registers_matcher.rb +65 -0
  176. data/src/mocktail/registers_stubbing.rb +31 -0
  177. data/src/mocktail/replaces_next.rb +55 -0
  178. data/src/mocktail/replaces_type/redefines_new.rb +32 -0
  179. data/src/mocktail/replaces_type/redefines_singleton_methods.rb +80 -0
  180. data/src/mocktail/replaces_type/runs_sorbet_sig_blocks_before_replacement.rb +39 -0
  181. data/src/mocktail/replaces_type.rb +36 -0
  182. data/src/mocktail/resets_state.rb +14 -0
  183. data/src/mocktail/share/bind.rb +18 -0
  184. data/src/mocktail/share/cleans_backtrace.rb +22 -0
  185. data/src/mocktail/share/creates_identifier.rb +39 -0
  186. data/src/mocktail/share/determines_matching_calls.rb +72 -0
  187. data/src/mocktail/share/stringifies_call.rb +84 -0
  188. data/src/mocktail/share/stringifies_method_name.rb +16 -0
  189. data/src/mocktail/simulates_argument_error/reconciles_args_with_params.rb +27 -0
  190. data/src/mocktail/simulates_argument_error/recreates_message.rb +34 -0
  191. data/src/mocktail/simulates_argument_error/transforms_params.rb +58 -0
  192. data/src/mocktail/simulates_argument_error.rb +36 -0
  193. data/src/mocktail/sorbet.rb +3 -0
  194. data/src/mocktail/stringifies_method_signature.rb +53 -0
  195. data/src/mocktail/typed.rb +5 -0
  196. data/src/mocktail/value/cabinet.rb +91 -0
  197. data/src/mocktail/value/call.rb +51 -0
  198. data/src/mocktail/value/demo_config.rb +10 -0
  199. data/src/mocktail/value/double.rb +10 -0
  200. data/src/mocktail/value/double_data.rb +15 -0
  201. data/src/mocktail/value/explanation.rb +68 -0
  202. data/src/mocktail/value/explanation_data.rb +19 -0
  203. data/src/mocktail/value/fake_method_data.rb +11 -0
  204. data/src/mocktail/value/matcher_registry.rb +27 -0
  205. data/src/mocktail/value/no_explanation_data.rb +20 -0
  206. data/src/mocktail/value/signature.rb +35 -0
  207. data/src/mocktail/value/stubbing.rb +26 -0
  208. data/src/mocktail/value/top_shelf.rb +79 -0
  209. data/src/mocktail/value/type_replacement.rb +11 -0
  210. data/src/mocktail/value/type_replacement_data.rb +19 -0
  211. data/src/mocktail/value/unsatisfying_call.rb +9 -0
  212. data/src/mocktail/value/unsatisfying_call_explanation.rb +24 -0
  213. data/src/mocktail/value.rb +19 -0
  214. data/src/mocktail/verifies_call/finds_verifiable_calls.rb +21 -0
  215. data/src/mocktail/verifies_call/raises_verification_error/gathers_calls_of_method.rb +15 -0
  216. data/src/mocktail/verifies_call/raises_verification_error.rb +74 -0
  217. data/src/mocktail/verifies_call.rb +37 -0
  218. data/src/mocktail/version.rb +12 -0
  219. data/src/mocktail.rb +154 -0
  220. data/sub_projects/rbi_generator/Gemfile +4 -0
  221. data/sub_projects/rbi_generator/Gemfile.lock +63 -0
  222. data/sub_projects/rbi_generator/sorbet/rbi/gems/.gitattributes +1 -0
  223. data/sub_projects/sorbet_user/Gemfile +13 -0
  224. data/sub_projects/sorbet_user/Gemfile.lock +126 -0
  225. data/sub_projects/sorbet_user/Rakefile +10 -0
  226. data/sub_projects/sorbet_user/bin/tapioca +27 -0
  227. data/sub_projects/sorbet_user/rbi/mocktail.rbi +9 -0
  228. data/sub_projects/sorbet_user/script/test +13 -0
  229. data/sub_projects/sorbet_user/sorbet/config +4 -0
  230. data/sub_projects/sorbet_user/sorbet/rbi/gems/.gitattributes +1 -0
  231. data/sub_projects/sorbet_user/sorbet/rbi/gems/ast@2.4.3.rbi +585 -0
  232. data/sub_projects/sorbet_user/sorbet/rbi/gems/benchmark@0.4.0.rbi +618 -0
  233. data/sub_projects/sorbet_user/sorbet/rbi/gems/erubi@1.13.1.rbi +155 -0
  234. data/sub_projects/sorbet_user/sorbet/rbi/gems/json@2.21.2.rbi +2140 -0
  235. data/sub_projects/sorbet_user/sorbet/rbi/gems/language_server-protocol@3.17.0.4.rbi +14244 -0
  236. data/sub_projects/sorbet_user/sorbet/rbi/gems/lint_roller@1.1.0.rbi +239 -0
  237. data/sub_projects/sorbet_user/sorbet/rbi/gems/logger@1.7.0.rbi +963 -0
  238. data/sub_projects/sorbet_user/sorbet/rbi/gems/m@1.6.2.rbi +258 -0
  239. data/sub_projects/sorbet_user/sorbet/rbi/gems/method_source@1.1.0.rbi +9 -0
  240. data/sub_projects/sorbet_user/sorbet/rbi/gems/minitest@5.27.0.rbi +1549 -0
  241. data/sub_projects/sorbet_user/sorbet/rbi/gems/mocktail@3.0.0.rbi +1923 -0
  242. data/sub_projects/sorbet_user/sorbet/rbi/gems/netrc@0.11.0.rbi +158 -0
  243. data/sub_projects/sorbet_user/sorbet/rbi/gems/parallel@1.26.3.rbi +291 -0
  244. data/sub_projects/sorbet_user/sorbet/rbi/gems/parser@3.3.7.4.rbi +5451 -0
  245. data/sub_projects/sorbet_user/sorbet/rbi/gems/prism@1.4.0.rbi +41732 -0
  246. data/sub_projects/sorbet_user/sorbet/rbi/gems/racc@1.8.1.rbi +164 -0
  247. data/sub_projects/sorbet_user/sorbet/rbi/gems/rainbow@3.1.1.rbi +402 -0
  248. data/sub_projects/sorbet_user/sorbet/rbi/gems/rake@13.2.1.rbi +3028 -0
  249. data/sub_projects/sorbet_user/sorbet/rbi/gems/rbi@0.3.1.rbi +6599 -0
  250. data/sub_projects/sorbet_user/sorbet/rbi/gems/rbs@3.9.2.rbi +6976 -0
  251. data/sub_projects/sorbet_user/sorbet/rbi/gems/regexp_parser@2.10.0.rbi +3795 -0
  252. data/sub_projects/sorbet_user/sorbet/rbi/gems/rubocop-ast@1.43.0.rbi +7717 -0
  253. data/sub_projects/sorbet_user/sorbet/rbi/gems/rubocop-performance@1.24.0.rbi +9 -0
  254. data/sub_projects/sorbet_user/sorbet/rbi/gems/rubocop-sorbet@0.9.0.rbi +9 -0
  255. data/sub_projects/sorbet_user/sorbet/rbi/gems/rubocop@1.73.2.rbi +61306 -0
  256. data/sub_projects/sorbet_user/sorbet/rbi/gems/ruby-progressbar@1.13.0.rbi +1317 -0
  257. data/sub_projects/sorbet_user/sorbet/rbi/gems/sorbet-eraser@0.3.1.rbi +8 -0
  258. data/sub_projects/sorbet_user/sorbet/rbi/gems/spoom@1.6.1.rbi +7274 -0
  259. data/sub_projects/sorbet_user/sorbet/rbi/gems/standard-custom@1.0.2.rbi +8 -0
  260. data/sub_projects/sorbet_user/sorbet/rbi/gems/standard-performance@1.7.0.rbi +9 -0
  261. data/sub_projects/sorbet_user/sorbet/rbi/gems/standard-sorbet@0.0.3.rbi +53 -0
  262. data/sub_projects/sorbet_user/sorbet/rbi/gems/standard@1.47.0.rbi +935 -0
  263. data/sub_projects/sorbet_user/sorbet/rbi/gems/tapioca@0.16.11.rbi +3628 -0
  264. data/sub_projects/sorbet_user/sorbet/rbi/gems/thor@1.3.2.rbi +4378 -0
  265. data/sub_projects/sorbet_user/sorbet/rbi/gems/unicode-display_width@3.1.4.rbi +132 -0
  266. data/sub_projects/sorbet_user/sorbet/rbi/gems/unicode-emoji@4.2.0.rbi +254 -0
  267. data/sub_projects/sorbet_user/sorbet/rbi/gems/yard-sorbet@0.9.0.rbi +435 -0
  268. data/sub_projects/sorbet_user/sorbet/rbi/gems/yard@0.9.45.rbi +18916 -0
  269. data/sub_projects/sorbet_user/sorbet/tapioca/config.yml +13 -0
  270. data/sub_projects/sorbet_user/sorbet/tapioca/require.rb +4 -0
  271. data/sub_projects/sorbet_user/test/ensure_type_safety_test.rb +21 -0
  272. data/sub_projects/sorbet_user/test/paint_by_number_test.rb +113 -0
  273. data/sub_projects/sorbet_user/test/sorbet_test.rb +289 -0
  274. data/sub_projects/sorbet_user/test/test_helper.rb +35 -0
  275. data/sub_projects/untyped_user/Gemfile +4 -0
  276. data/sub_projects/untyped_user/Gemfile.lock +25 -0
  277. data/sub_projects/untyped_user/antitype_test.rb +54 -0
  278. data/sub_projects/untyped_user/script/test +5 -0
  279. metadata +264 -8
@@ -0,0 +1,2140 @@
1
+ # typed: false
2
+
3
+ # DO NOT EDIT MANUALLY
4
+ # This is an autogenerated file for types exported from the `json` gem.
5
+ # Please instead update this file by running `bin/tapioca gem json`.
6
+
7
+
8
+ # = JavaScript \Object Notation (\JSON)
9
+ #
10
+ # \JSON is a lightweight data-interchange format.
11
+ #
12
+ # \JSON is easy for us humans to read and write,
13
+ # and equally simple for machines to read (parse) and write (generate).
14
+ #
15
+ # \JSON is language-independent, making it an ideal interchange format
16
+ # for applications in differing programming languages
17
+ # and on differing operating systems.
18
+ #
19
+ # == \JSON Values
20
+ #
21
+ # A \JSON value is one of the following:
22
+ # - Double-quoted text: <tt>"foo"</tt>.
23
+ # - Number: +1+, +1.0+, +2.0e2+.
24
+ # - Boolean: +true+, +false+.
25
+ # - Null: +null+.
26
+ # - \Array: an ordered list of values, enclosed by square brackets:
27
+ # ["foo", 1, 1.0, 2.0e2, true, false, null]
28
+ #
29
+ # - \Object: a collection of name/value pairs, enclosed by curly braces;
30
+ # each name is double-quoted text;
31
+ # the values may be any \JSON values:
32
+ # {"a": "foo", "b": 1, "c": 1.0, "d": 2.0e2, "e": true, "f": false, "g": null}
33
+ #
34
+ # A \JSON array or object may contain nested arrays, objects, and scalars
35
+ # to any depth:
36
+ # {"foo": {"bar": 1, "baz": 2}, "bat": [0, 1, 2]}
37
+ # [{"foo": 0, "bar": 1}, ["baz", 2]]
38
+ #
39
+ # == Using \Module \JSON
40
+ #
41
+ # To make module \JSON available in your code, begin with:
42
+ # require 'json'
43
+ #
44
+ # All examples here assume that this has been done.
45
+ #
46
+ # === Parsing \JSON
47
+ #
48
+ # You can parse a \String containing \JSON data using
49
+ # either of two methods:
50
+ # - <tt>JSON.parse(source, opts)</tt>
51
+ # - <tt>JSON.parse!(source, opts)</tt>
52
+ #
53
+ # where
54
+ # - +source+ is a Ruby object.
55
+ # - +opts+ is a \Hash object containing options
56
+ # that control both input allowed and output formatting.
57
+ #
58
+ # The difference between the two methods
59
+ # is that JSON.parse! omits some checks
60
+ # and may not be safe for some +source+ data;
61
+ # use it only for data from trusted sources.
62
+ # Use the safer method JSON.parse for less trusted sources.
63
+ #
64
+ # ==== Parsing \JSON Arrays
65
+ #
66
+ # When +source+ is a \JSON array, JSON.parse by default returns a Ruby \Array:
67
+ # json = '["foo", 1, 1.0, 2.0e2, true, false, null]'
68
+ # ruby = JSON.parse(json)
69
+ # ruby # => ["foo", 1, 1.0, 200.0, true, false, nil]
70
+ # ruby.class # => Array
71
+ #
72
+ # The \JSON array may contain nested arrays, objects, and scalars
73
+ # to any depth:
74
+ # json = '[{"foo": 0, "bar": 1}, ["baz", 2]]'
75
+ # JSON.parse(json) # => [{"foo"=>0, "bar"=>1}, ["baz", 2]]
76
+ #
77
+ # ==== Parsing \JSON \Objects
78
+ #
79
+ # When the source is a \JSON object, JSON.parse by default returns a Ruby \Hash:
80
+ # json = '{"a": "foo", "b": 1, "c": 1.0, "d": 2.0e2, "e": true, "f": false, "g": null}'
81
+ # ruby = JSON.parse(json)
82
+ # ruby # => {"a"=>"foo", "b"=>1, "c"=>1.0, "d"=>200.0, "e"=>true, "f"=>false, "g"=>nil}
83
+ # ruby.class # => Hash
84
+ #
85
+ # The \JSON object may contain nested arrays, objects, and scalars
86
+ # to any depth:
87
+ # json = '{"foo": {"bar": 1, "baz": 2}, "bat": [0, 1, 2]}'
88
+ # JSON.parse(json) # => {"foo"=>{"bar"=>1, "baz"=>2}, "bat"=>[0, 1, 2]}
89
+ #
90
+ # ==== Parsing \JSON Scalars
91
+ #
92
+ # When the source is a \JSON scalar (not an array or object),
93
+ # JSON.parse returns a Ruby scalar.
94
+ #
95
+ # \String:
96
+ # ruby = JSON.parse('"foo"')
97
+ # ruby # => 'foo'
98
+ # ruby.class # => String
99
+ # \Integer:
100
+ # ruby = JSON.parse('1')
101
+ # ruby # => 1
102
+ # ruby.class # => Integer
103
+ # \Float:
104
+ # ruby = JSON.parse('1.0')
105
+ # ruby # => 1.0
106
+ # ruby.class # => Float
107
+ # ruby = JSON.parse('2.0e2')
108
+ # ruby # => 200
109
+ # ruby.class # => Float
110
+ # Boolean:
111
+ # ruby = JSON.parse('true')
112
+ # ruby # => true
113
+ # ruby.class # => TrueClass
114
+ # ruby = JSON.parse('false')
115
+ # ruby # => false
116
+ # ruby.class # => FalseClass
117
+ # Null:
118
+ # ruby = JSON.parse('null')
119
+ # ruby # => nil
120
+ # ruby.class # => NilClass
121
+ #
122
+ # ==== Parsing Options
123
+ #
124
+ # ====== Input Options
125
+ #
126
+ # Option +max_nesting+ (\Integer) specifies the maximum nesting depth allowed;
127
+ # defaults to +100+;
128
+ # You can set it to +false+ to disable depth checking entirely, but that is dangerous
129
+ # when parsing untrusted input.
130
+ #
131
+ # With the default, +100+:
132
+ # source = '[0, [1, [2, [3]]]]'
133
+ # ruby = JSON.parse(source)
134
+ # ruby # => [0, [1, [2, [3]]]]
135
+ # Too deep:
136
+ # # Raises JSON::NestingError (nesting of 2 is too deep):
137
+ # JSON.parse(source, {max_nesting: 1})
138
+ # Bad value:
139
+ # # Raises TypeError (wrong argument type Symbol (expected Fixnum)):
140
+ # JSON.parse(source, {max_nesting: :foo})
141
+ #
142
+ # ---
143
+ #
144
+ # Option +allow_duplicate_key+ specifies whether duplicate keys in objects
145
+ # should be ignored or cause an error to be raised:
146
+ #
147
+ # When not specified:
148
+ # # The last value is used and a deprecation warning emitted.
149
+ # JSON.parse('{"a": 1, "a":2}') => {"a" => 2}
150
+ # # warning: detected duplicate keys in JSON object.
151
+ # # This will raise an error in json 3.0 unless enabled via `allow_duplicate_key: true`
152
+ #
153
+ # When set to +true+:
154
+ # # The last value is used.
155
+ # JSON.parse('{"a": 1, "a":2}') => {"a" => 2}
156
+ #
157
+ # When set to +false+, the future default:
158
+ # JSON.parse('{"a": 1, "a":2}') => duplicate key at line 1 column 1 (JSON::ParserError)
159
+ #
160
+ # ---
161
+ #
162
+ # Option +allow_nan+ (boolean) specifies whether to allow
163
+ # NaN, Infinity, and MinusInfinity in +source+;
164
+ # defaults to +false+.
165
+ #
166
+ # With the default, +false+:
167
+ # # Raises JSON::ParserError (225: unexpected token at '[NaN]'):
168
+ # JSON.parse('[NaN]')
169
+ # # Raises JSON::ParserError (232: unexpected token at '[Infinity]'):
170
+ # JSON.parse('[Infinity]')
171
+ # # Raises JSON::ParserError (248: unexpected token at '[-Infinity]'):
172
+ # JSON.parse('[-Infinity]')
173
+ # Allow:
174
+ # source = '[NaN, Infinity, -Infinity]'
175
+ # ruby = JSON.parse(source, {allow_nan: true})
176
+ # ruby # => [NaN, Infinity, -Infinity]
177
+ #
178
+ # ---
179
+ #
180
+ # Option +allow_trailing_comma+ (boolean) specifies whether to allow
181
+ # trailing commas in objects and arrays;
182
+ # defaults to +false+.
183
+ #
184
+ # With the default, +false+:
185
+ # JSON.parse('[1,]') # unexpected character: ']' at line 1 column 4 (JSON::ParserError)
186
+ #
187
+ # When enabled:
188
+ # JSON.parse('[1,]', allow_trailing_comma: true) # => [1]
189
+ #
190
+ # ---
191
+ #
192
+ # Option +allow_comments+ (boolean) specifies whether to allow
193
+ # JavaScript style comments (either <tt>// comment</tt> or <tt>/* comment */</tt>);
194
+ # defaults to +false+.
195
+ #
196
+ # When not specified, a deprecation warning is emitted if a comment is encountered.
197
+ #
198
+ # When set to +true+, comments are ignored:
199
+ # JSON.parse('/* comment */ {"a": 1, "a":2}') # => {"a" => 2}
200
+ #
201
+ # When set to +false+, the future default:
202
+ # JSON.parse('/* comment */ {"a": 1, "a":2}') # unexpected character: '/' at line 1 column 1 (JSON::ParserError)
203
+ #
204
+ # ---
205
+ #
206
+ # Option +allow_control_characters+ (boolean) specifies whether to allow
207
+ # unescaped ASCII control characters, such as newlines, in strings;
208
+ # defaults to +false+.
209
+ #
210
+ # With the default, +false+:
211
+ # JSON.parse(%{"Hello\nWorld"}) # invalid ASCII control character in string (JSON::ParserError)
212
+ #
213
+ # When enabled:
214
+ # JSON.parse(%{"Hello\nWorld"}, allow_control_characters: true) # => "Hello\nWorld"
215
+ #
216
+ # ---
217
+ #
218
+ # Option +allow_invalid_escape+ (boolean) specifies whether to ignore backslahes that are followed
219
+ # by an invalid escape character in strings;
220
+ # defaults to +false+.
221
+ #
222
+ # With the default, +false+:
223
+ # JSON.parse('"Hell\o"') # invalid escape character in string (JSON::ParserError)
224
+ #
225
+ # When enabled:
226
+ # JSON.parse('"Hell\o"', allow_invalid_escape: true) # => "Hello"
227
+ #
228
+ # ====== Output Options
229
+ #
230
+ # Option +freeze+ (boolean) specifies whether the returned objects will be frozen;
231
+ # defaults to +false+.
232
+ #
233
+ # Option +symbolize_names+ (boolean) specifies whether returned \Hash keys
234
+ # should be Symbols;
235
+ # defaults to +false+ (use Strings).
236
+ #
237
+ # With the default, +false+:
238
+ # source = '{"a": "foo", "b": 1.0, "c": true, "d": false, "e": null}'
239
+ # ruby = JSON.parse(source)
240
+ # ruby # => {"a"=>"foo", "b"=>1.0, "c"=>true, "d"=>false, "e"=>nil}
241
+ # Use Symbols:
242
+ # ruby = JSON.parse(source, {symbolize_names: true})
243
+ # ruby # => {:a=>"foo", :b=>1.0, :c=>true, :d=>false, :e=>nil}
244
+ #
245
+ # ---
246
+ #
247
+ # Option +object_class+ (\Class) specifies the Ruby class to be used
248
+ # for each \JSON object;
249
+ # defaults to \Hash.
250
+ #
251
+ # With the default, \Hash:
252
+ # source = '{"a": "foo", "b": 1.0, "c": true, "d": false, "e": null}'
253
+ # ruby = JSON.parse(source)
254
+ # ruby.class # => Hash
255
+ # Use class \OpenStruct:
256
+ # ruby = JSON.parse(source, {object_class: OpenStruct})
257
+ # ruby # => #<OpenStruct a="foo", b=1.0, c=true, d=false, e=nil>
258
+ #
259
+ # ---
260
+ #
261
+ # Option +array_class+ (\Class) specifies the Ruby class to be used
262
+ # for each \JSON array;
263
+ # defaults to \Array.
264
+ #
265
+ # With the default, \Array:
266
+ # source = '["foo", 1.0, true, false, null]'
267
+ # ruby = JSON.parse(source)
268
+ # ruby.class # => Array
269
+ # Use class \Set:
270
+ # ruby = JSON.parse(source, {array_class: Set})
271
+ # ruby # => #<Set: {"foo", 1.0, true, false, nil}>
272
+ #
273
+ # ---
274
+ #
275
+ # Option +create_additions+ (boolean) specifies whether to use \JSON additions in parsing.
276
+ # See {\JSON Additions}[#module-JSON-label-JSON+Additions].
277
+ #
278
+ # === Generating \JSON
279
+ #
280
+ # To generate a Ruby \String containing \JSON data,
281
+ # use method <tt>JSON.generate(source, opts)</tt>, where
282
+ # - +source+ is a Ruby object.
283
+ # - +opts+ is a \Hash object containing options
284
+ # that control both input allowed and output formatting.
285
+ #
286
+ # ==== Generating \JSON from Arrays
287
+ #
288
+ # When the source is a Ruby \Array, JSON.generate returns
289
+ # a \String containing a \JSON array:
290
+ # ruby = [0, 's', :foo]
291
+ # json = JSON.generate(ruby)
292
+ # json # => '[0,"s","foo"]'
293
+ #
294
+ # The Ruby \Array array may contain nested arrays, hashes, and scalars
295
+ # to any depth:
296
+ # ruby = [0, [1, 2], {foo: 3, bar: 4}]
297
+ # json = JSON.generate(ruby)
298
+ # json # => '[0,[1,2],{"foo":3,"bar":4}]'
299
+ #
300
+ # ==== Generating \JSON from Hashes
301
+ #
302
+ # When the source is a Ruby \Hash, JSON.generate returns
303
+ # a \String containing a \JSON object:
304
+ # ruby = {foo: 0, bar: 's', baz: :bat}
305
+ # json = JSON.generate(ruby)
306
+ # json # => '{"foo":0,"bar":"s","baz":"bat"}'
307
+ #
308
+ # The Ruby \Hash array may contain nested arrays, hashes, and scalars
309
+ # to any depth:
310
+ # ruby = {foo: [0, 1], bar: {baz: 2, bat: 3}, bam: :bad}
311
+ # json = JSON.generate(ruby)
312
+ # json # => '{"foo":[0,1],"bar":{"baz":2,"bat":3},"bam":"bad"}'
313
+ #
314
+ # ==== Generating \JSON from Other Objects
315
+ #
316
+ # When the source is neither an \Array nor a \Hash,
317
+ # the generated \JSON data depends on the class of the source.
318
+ #
319
+ # When the source is a Ruby \Integer or \Float, JSON.generate returns
320
+ # a \String containing a \JSON number:
321
+ # JSON.generate(42) # => '42'
322
+ # JSON.generate(0.42) # => '0.42'
323
+ #
324
+ # When the source is a Ruby \String, JSON.generate returns
325
+ # a \String containing a \JSON string (with double-quotes):
326
+ # JSON.generate('A string') # => '"A string"'
327
+ #
328
+ # When the source is +true+, +false+ or +nil+, JSON.generate returns
329
+ # a \String containing the corresponding \JSON token:
330
+ # JSON.generate(true) # => 'true'
331
+ # JSON.generate(false) # => 'false'
332
+ # JSON.generate(nil) # => 'null'
333
+ #
334
+ # When the source is none of the above, JSON.generate returns
335
+ # a \String containing a \JSON string representation of the source:
336
+ # JSON.generate(:foo) # => '"foo"'
337
+ # JSON.generate(Complex(0, 0)) # => '"0+0i"'
338
+ # JSON.generate(Dir.new('.')) # => '"#<Dir>"'
339
+ #
340
+ # ==== Generating Options
341
+ #
342
+ # ====== Input Options
343
+ #
344
+ # Option +allow_nan+ (boolean) specifies whether
345
+ # +NaN+, +Infinity+, and <tt>-Infinity</tt> may be generated;
346
+ # defaults to +false+.
347
+ #
348
+ # With the default, +false+:
349
+ # # Raises JSON::GeneratorError (920: NaN not allowed in JSON):
350
+ # JSON.generate(JSON::NaN)
351
+ # # Raises JSON::GeneratorError (917: Infinity not allowed in JSON):
352
+ # JSON.generate(JSON::Infinity)
353
+ # # Raises JSON::GeneratorError (917: -Infinity not allowed in JSON):
354
+ # JSON.generate(JSON::MinusInfinity)
355
+ #
356
+ # Allow:
357
+ # ruby = [Float::NAN, Float::INFINITY, JSON::NaN, JSON::Infinity, JSON::MinusInfinity]
358
+ # JSON.generate(ruby, allow_nan: true) # => '[NaN,Infinity,NaN,Infinity,-Infinity]'
359
+ #
360
+ # ---
361
+ #
362
+ # Option +allow_duplicate_key+ (boolean) specifies whether
363
+ # hashes with duplicate keys should be allowed or produce an error.
364
+ # defaults to emit a deprecation warning.
365
+ #
366
+ # With the default, (not set):
367
+ # Warning[:deprecated] = true
368
+ # JSON.generate({ foo: 1, "foo" => 2 })
369
+ # # warning: detected duplicate key "foo" in {foo: 1, "foo" => 2}.
370
+ # # This will raise an error in json 3.0 unless enabled via `allow_duplicate_key: true`
371
+ # # => '{"foo":1,"foo":2}'
372
+ #
373
+ # With <tt>false</tt>
374
+ # JSON.generate({ foo: 1, "foo" => 2 }, allow_duplicate_key: false)
375
+ # # detected duplicate key "foo" in {foo: 1, "foo" => 2} (JSON::GeneratorError)
376
+ #
377
+ # In version 3.0, <tt>false</tt> will become the default.
378
+ #
379
+ # ---
380
+ #
381
+ # Option +max_nesting+ (\Integer) specifies the maximum nesting depth
382
+ # in +obj+; defaults to +100+.
383
+ #
384
+ # With the default, +100+:
385
+ # obj = [[[[[[0]]]]]]
386
+ # JSON.generate(obj) # => '[[[[[[0]]]]]]'
387
+ #
388
+ # Too deep:
389
+ # # Raises JSON::NestingError (nesting of 2 is too deep):
390
+ # JSON.generate(obj, max_nesting: 2)
391
+ #
392
+ # With +false+:
393
+ # obj = []
394
+ # obj[0] = obj
395
+ # # Raises SystemStackError: stack level too deep
396
+ # JSON.generate(obj, max_nesting: false)
397
+ #
398
+ # Setting +max_nesting+ to +false+ can lead to a stackoverflow and may leave the program
399
+ # in an unrecoverable state. It is discouraged.
400
+ #
401
+ # ====== Escaping Options
402
+ #
403
+ # Options +script_safe+ (boolean) specifies wether <tt>'\u2028'</tt>, <tt>'\u2029'</tt>
404
+ # and <tt>'/'</tt> should be escaped as to make the JSON object safe to interpolate in script
405
+ # tags.
406
+ #
407
+ # Options +ascii_only+ (boolean) specifies wether all characters outside the ASCII range
408
+ # should be escaped.
409
+ #
410
+ # ====== Output Options
411
+ #
412
+ # The default formatting options generate the most compact
413
+ # \JSON data, all on one line and with no whitespace.
414
+ #
415
+ # You can use these formatting options to generate
416
+ # \JSON data in a more open format, using whitespace.
417
+ # See also JSON.pretty_generate.
418
+ #
419
+ # - Option +array_nl+ (\String) specifies a string (usually a newline)
420
+ # to be inserted after each \JSON array; defaults to the empty \String, <tt>''</tt>.
421
+ # - Option +object_nl+ (\String) specifies a string (usually a newline)
422
+ # to be inserted after each \JSON object; defaults to the empty \String, <tt>''</tt>.
423
+ # - Option +indent+ (\String) specifies the string (usually spaces) to be
424
+ # used for indentation; defaults to the empty \String, <tt>''</tt>;
425
+ # has no effect unless options +array_nl+ or +object_nl+ specify newlines.
426
+ # - Option +space+ (\String) specifies a string (usually a space) to be
427
+ # inserted after the colon in each \JSON object's pair;
428
+ # defaults to the empty \String, <tt>''</tt>.
429
+ # - Option +space_before+ (\String) specifies a string (usually a space) to be
430
+ # inserted before the colon in each \JSON object's pair;
431
+ # defaults to the empty \String, <tt>''</tt>.
432
+ # - Option +sort_keys+ (boolean or \Proc) controls whether and how the keys of a
433
+ # hash are sorted when generating the output; defaults to <tt>false</tt>.
434
+ # When +true+, keys are sorted lexicographically. When a \Proc, it receives
435
+ # the entire \Hash and must return a \Hash with its pairs in the desired
436
+ # order, allowing for arbitrary sort orders.
437
+ #
438
+ # In this example, +obj+ is used first to generate the shortest
439
+ # \JSON data (no whitespace), then again with all formatting options
440
+ # specified:
441
+ #
442
+ # obj = {foo: [:bar, :baz], bat: {bam: 0, bad: 1}}
443
+ # json = JSON.generate(obj)
444
+ # puts 'Compact:', json
445
+ # opts = {
446
+ # array_nl: "\n",
447
+ # object_nl: "\n",
448
+ # indent: ' ',
449
+ # space_before: ' ',
450
+ # space: ' '
451
+ # }
452
+ # puts 'Open:', JSON.generate(obj, opts)
453
+ #
454
+ # Output:
455
+ # Compact:
456
+ # {"foo":["bar","baz"],"bat":{"bam":0,"bad":1}}
457
+ # Open:
458
+ # {
459
+ # "foo" : [
460
+ # "bar",
461
+ # "baz"
462
+ # ],
463
+ # "bat" : {
464
+ # "bam" : 0,
465
+ # "bad" : 1
466
+ # }
467
+ # }
468
+ #
469
+ # == \JSON Additions
470
+ #
471
+ # Note that JSON Additions must only be used with trusted data, and is
472
+ # deprecated.
473
+ #
474
+ # When you "round trip" a non-\String object from Ruby to \JSON and back,
475
+ # you have a new \String, instead of the object you began with:
476
+ # ruby0 = Range.new(0, 2)
477
+ # json = JSON.generate(ruby0)
478
+ # json # => '0..2"'
479
+ # ruby1 = JSON.parse(json)
480
+ # ruby1 # => '0..2'
481
+ # ruby1.class # => String
482
+ #
483
+ # You can use \JSON _additions_ to preserve the original object.
484
+ # The addition is an extension of a ruby class, so that:
485
+ # - \JSON.generate stores more information in the \JSON string.
486
+ # - \JSON.parse, called with option +create_additions+,
487
+ # uses that information to create a proper Ruby object.
488
+ #
489
+ # This example shows a \Range being generated into \JSON
490
+ # and parsed back into Ruby, both without and with
491
+ # the addition for \Range:
492
+ # ruby = Range.new(0, 2)
493
+ # # This passage does not use the addition for Range.
494
+ # json0 = JSON.generate(ruby)
495
+ # ruby0 = JSON.parse(json0)
496
+ # # This passage uses the addition for Range.
497
+ # require 'json/add/range'
498
+ # json1 = JSON.generate(ruby)
499
+ # ruby1 = JSON.parse(json1, create_additions: true)
500
+ # # Make a nice display.
501
+ # display = <<~EOT
502
+ # Generated JSON:
503
+ # Without addition: #{json0} (#{json0.class})
504
+ # With addition: #{json1} (#{json1.class})
505
+ # Parsed JSON:
506
+ # Without addition: #{ruby0.inspect} (#{ruby0.class})
507
+ # With addition: #{ruby1.inspect} (#{ruby1.class})
508
+ # EOT
509
+ # puts display
510
+ #
511
+ # This output shows the different results:
512
+ # Generated JSON:
513
+ # Without addition: "0..2" (String)
514
+ # With addition: {"json_class":"Range","a":[0,2,false]} (String)
515
+ # Parsed JSON:
516
+ # Without addition: "0..2" (String)
517
+ # With addition: 0..2 (Range)
518
+ #
519
+ # The \JSON module includes additions for certain classes.
520
+ # You can also craft custom additions.
521
+ # See {Custom \JSON Additions}[#module-JSON-label-Custom+JSON+Additions].
522
+ #
523
+ # === Built-in Additions
524
+ #
525
+ # The \JSON module includes additions for certain classes.
526
+ # To use an addition, +require+ its source:
527
+ # - BigDecimal: <tt>require 'json/add/bigdecimal'</tt>
528
+ # - Complex: <tt>require 'json/add/complex'</tt>
529
+ # - Date: <tt>require 'json/add/date'</tt>
530
+ # - DateTime: <tt>require 'json/add/date_time'</tt>
531
+ # - Exception: <tt>require 'json/add/exception'</tt>
532
+ # - OpenStruct: <tt>require 'json/add/ostruct'</tt>
533
+ # - Range: <tt>require 'json/add/range'</tt>
534
+ # - Rational: <tt>require 'json/add/rational'</tt>
535
+ # - Regexp: <tt>require 'json/add/regexp'</tt>
536
+ # - Set: <tt>require 'json/add/set'</tt>
537
+ # - Struct: <tt>require 'json/add/struct'</tt>
538
+ # - Symbol: <tt>require 'json/add/symbol'</tt>
539
+ # - Time: <tt>require 'json/add/time'</tt>
540
+ #
541
+ # To reduce punctuation clutter, the examples below
542
+ # show the generated \JSON via +puts+, rather than the usual +inspect+,
543
+ #
544
+ # \BigDecimal:
545
+ # require 'json/add/bigdecimal'
546
+ # ruby0 = BigDecimal(0) # 0.0
547
+ # json = JSON.generate(ruby0) # {"json_class":"BigDecimal","b":"27:0.0"}
548
+ # ruby1 = JSON.parse(json, create_additions: true) # 0.0
549
+ # ruby1.class # => BigDecimal
550
+ #
551
+ # \Complex:
552
+ # require 'json/add/complex'
553
+ # ruby0 = Complex(1+0i) # 1+0i
554
+ # json = JSON.generate(ruby0) # {"json_class":"Complex","r":1,"i":0}
555
+ # ruby1 = JSON.parse(json, create_additions: true) # 1+0i
556
+ # ruby1.class # Complex
557
+ #
558
+ # \Date:
559
+ # require 'json/add/date'
560
+ # ruby0 = Date.today # 2020-05-02
561
+ # json = JSON.generate(ruby0) # {"json_class":"Date","y":2020,"m":5,"d":2,"sg":2299161.0}
562
+ # ruby1 = JSON.parse(json, create_additions: true) # 2020-05-02
563
+ # ruby1.class # Date
564
+ #
565
+ # \DateTime:
566
+ # require 'json/add/date_time'
567
+ # ruby0 = DateTime.now # 2020-05-02T10:38:13-05:00
568
+ # json = JSON.generate(ruby0) # {"json_class":"DateTime","y":2020,"m":5,"d":2,"H":10,"M":38,"S":13,"of":"-5/24","sg":2299161.0}
569
+ # ruby1 = JSON.parse(json, create_additions: true) # 2020-05-02T10:38:13-05:00
570
+ # ruby1.class # DateTime
571
+ #
572
+ # \Exception (and its subclasses including \RuntimeError):
573
+ # require 'json/add/exception'
574
+ # ruby0 = Exception.new('A message') # A message
575
+ # json = JSON.generate(ruby0) # {"json_class":"Exception","m":"A message","b":null}
576
+ # ruby1 = JSON.parse(json, create_additions: true) # A message
577
+ # ruby1.class # Exception
578
+ # ruby0 = RuntimeError.new('Another message') # Another message
579
+ # json = JSON.generate(ruby0) # {"json_class":"RuntimeError","m":"Another message","b":null}
580
+ # ruby1 = JSON.parse(json, create_additions: true) # Another message
581
+ # ruby1.class # RuntimeError
582
+ #
583
+ # \OpenStruct:
584
+ # require 'json/add/ostruct'
585
+ # ruby0 = OpenStruct.new(name: 'Matz', language: 'Ruby') # #<OpenStruct name="Matz", language="Ruby">
586
+ # json = JSON.generate(ruby0) # {"json_class":"OpenStruct","t":{"name":"Matz","language":"Ruby"}}
587
+ # ruby1 = JSON.parse(json, create_additions: true) # #<OpenStruct name="Matz", language="Ruby">
588
+ # ruby1.class # OpenStruct
589
+ #
590
+ # \Range:
591
+ # require 'json/add/range'
592
+ # ruby0 = Range.new(0, 2) # 0..2
593
+ # json = JSON.generate(ruby0) # {"json_class":"Range","a":[0,2,false]}
594
+ # ruby1 = JSON.parse(json, create_additions: true) # 0..2
595
+ # ruby1.class # Range
596
+ #
597
+ # \Rational:
598
+ # require 'json/add/rational'
599
+ # ruby0 = Rational(1, 3) # 1/3
600
+ # json = JSON.generate(ruby0) # {"json_class":"Rational","n":1,"d":3}
601
+ # ruby1 = JSON.parse(json, create_additions: true) # 1/3
602
+ # ruby1.class # Rational
603
+ #
604
+ # \Regexp:
605
+ # require 'json/add/regexp'
606
+ # ruby0 = Regexp.new('foo') # (?-mix:foo)
607
+ # json = JSON.generate(ruby0) # {"json_class":"Regexp","o":0,"s":"foo"}
608
+ # ruby1 = JSON.parse(json, create_additions: true) # (?-mix:foo)
609
+ # ruby1.class # Regexp
610
+ #
611
+ # \Set:
612
+ # require 'json/add/set'
613
+ # ruby0 = Set.new([0, 1, 2]) # #<Set: {0, 1, 2}>
614
+ # json = JSON.generate(ruby0) # {"json_class":"Set","a":[0,1,2]}
615
+ # ruby1 = JSON.parse(json, create_additions: true) # #<Set: {0, 1, 2}>
616
+ # ruby1.class # Set
617
+ #
618
+ # \Struct:
619
+ # require 'json/add/struct'
620
+ # Customer = Struct.new(:name, :address) # Customer
621
+ # ruby0 = Customer.new("Dave", "123 Main") # #<struct Customer name="Dave", address="123 Main">
622
+ # json = JSON.generate(ruby0) # {"json_class":"Customer","v":["Dave","123 Main"]}
623
+ # ruby1 = JSON.parse(json, create_additions: true) # #<struct Customer name="Dave", address="123 Main">
624
+ # ruby1.class # Customer
625
+ #
626
+ # \Symbol:
627
+ # require 'json/add/symbol'
628
+ # ruby0 = :foo # foo
629
+ # json = JSON.generate(ruby0) # {"json_class":"Symbol","s":"foo"}
630
+ # ruby1 = JSON.parse(json, create_additions: true) # foo
631
+ # ruby1.class # Symbol
632
+ #
633
+ # \Time:
634
+ # require 'json/add/time'
635
+ # ruby0 = Time.now # 2020-05-02 11:28:26 -0500
636
+ # json = JSON.generate(ruby0) # {"json_class":"Time","s":1588436906,"n":840560000}
637
+ # ruby1 = JSON.parse(json, create_additions: true) # 2020-05-02 11:28:26 -0500
638
+ # ruby1.class # Time
639
+ #
640
+ #
641
+ # === Custom \JSON Additions
642
+ #
643
+ # In addition to the \JSON additions provided,
644
+ # you can craft \JSON additions of your own,
645
+ # either for Ruby built-in classes or for user-defined classes.
646
+ #
647
+ # Here's a user-defined class +Foo+:
648
+ # class Foo
649
+ # attr_accessor :bar, :baz
650
+ # def initialize(bar, baz)
651
+ # self.bar = bar
652
+ # self.baz = baz
653
+ # end
654
+ # end
655
+ #
656
+ # Here's the \JSON addition for it:
657
+ # # Extend class Foo with JSON addition.
658
+ # class Foo
659
+ # # Serialize Foo object with its class name and arguments
660
+ # def to_json(*args)
661
+ # {
662
+ # JSON.create_id => self.class.name,
663
+ # 'a' => [ bar, baz ]
664
+ # }.to_json(*args)
665
+ # end
666
+ # # Deserialize JSON string by constructing new Foo object with arguments.
667
+ # def self.json_create(object)
668
+ # new(*object['a'])
669
+ # end
670
+ # end
671
+ #
672
+ # Demonstration:
673
+ # require 'json'
674
+ # # This Foo object has no custom addition.
675
+ # foo0 = Foo.new(0, 1)
676
+ # json0 = JSON.generate(foo0)
677
+ # obj0 = JSON.parse(json0)
678
+ # # Lood the custom addition.
679
+ # require_relative 'foo_addition'
680
+ # # This foo has the custom addition.
681
+ # foo1 = Foo.new(0, 1)
682
+ # json1 = JSON.generate(foo1)
683
+ # obj1 = JSON.parse(json1, create_additions: true)
684
+ # # Make a nice display.
685
+ # display = <<~EOT
686
+ # Generated JSON:
687
+ # Without custom addition: #{json0} (#{json0.class})
688
+ # With custom addition: #{json1} (#{json1.class})
689
+ # Parsed JSON:
690
+ # Without custom addition: #{obj0.inspect} (#{obj0.class})
691
+ # With custom addition: #{obj1.inspect} (#{obj1.class})
692
+ # EOT
693
+ # puts display
694
+ #
695
+ # Output:
696
+ #
697
+ # Generated JSON:
698
+ # Without custom addition: "#<Foo:0x0000000006534e80>" (String)
699
+ # With custom addition: {"json_class":"Foo","a":[0,1]} (String)
700
+ # Parsed JSON:
701
+ # Without custom addition: "#<Foo:0x0000000006534e80>" (String)
702
+ # With custom addition: #<Foo:0x0000000006473bb8 @bar=0, @baz=1> (Foo)
703
+ #
704
+ # source://json//lib/json/version.rb#3
705
+ module JSON
706
+ private
707
+
708
+ # :call-seq:
709
+ # JSON.dump(obj, io = nil, limit = nil)
710
+ #
711
+ # Dumps +obj+ as a \JSON string, i.e. calls generate on the object and returns the result.
712
+ #
713
+ # The default options can be changed via method JSON.dump_default_options.
714
+ #
715
+ # - Argument +io+, if given, should respond to method +write+;
716
+ # the \JSON \String is written to +io+, and +io+ is returned.
717
+ # If +io+ is not given, the \JSON \String is returned.
718
+ # - Argument +limit+, if given, is passed to JSON.generate as option +max_nesting+.
719
+ #
720
+ # ---
721
+ #
722
+ # When argument +io+ is not given, returns the \JSON \String generated from +obj+:
723
+ # obj = {foo: [0, 1], bar: {baz: 2, bat: 3}, bam: :bad}
724
+ # json = JSON.dump(obj)
725
+ # json # => "{\"foo\":[0,1],\"bar\":{\"baz\":2,\"bat\":3},\"bam\":\"bad\"}"
726
+ #
727
+ # When argument +io+ is given, writes the \JSON \String to +io+ and returns +io+:
728
+ # path = 't.json'
729
+ # File.open(path, 'w') do |file|
730
+ # JSON.dump(obj, file)
731
+ # end # => #<File:t.json (closed)>
732
+ # puts File.read(path)
733
+ # Output:
734
+ # {"foo":[0,1],"bar":{"baz":2,"bat":3},"bam":"bad"}
735
+ #
736
+ # source://json//lib/json/common.rb#941
737
+ def dump(obj, anIO = T.unsafe(nil), limit = T.unsafe(nil), kwargs = T.unsafe(nil)); end
738
+
739
+ # :call-seq:
740
+ # JSON.fast_generate(obj, opts) -> new_string
741
+ #
742
+ # Arguments +obj+ and +opts+ here are the same as
743
+ # arguments +obj+ and +opts+ in JSON.generate.
744
+ #
745
+ # By default, generates \JSON data without checking
746
+ # for circular references in +obj+ (option +max_nesting+ set to +false+, disabled).
747
+ #
748
+ # Raises an exception if +obj+ contains circular references:
749
+ # a = []; b = []; a.push(b); b.push(a)
750
+ # # Raises SystemStackError (stack level too deep):
751
+ # JSON.fast_generate(a)
752
+ #
753
+ # source://json//lib/json/common.rb#471
754
+ def fast_generate(obj, opts = T.unsafe(nil)); end
755
+
756
+ # source://json//lib/json/common.rb#986
757
+ def fast_unparse(*_arg0, **_arg1, &_arg2); end
758
+
759
+ # :call-seq:
760
+ # JSON.generate(obj, opts = nil) -> new_string
761
+ #
762
+ # Returns a \String containing the generated \JSON data.
763
+ #
764
+ # See also JSON.pretty_generate.
765
+ #
766
+ # Argument +obj+ is the Ruby object to be converted to \JSON.
767
+ #
768
+ # Argument +opts+, if given, contains a \Hash of options for the generation.
769
+ # See {Generating Options}[#module-JSON-label-Generating+Options].
770
+ #
771
+ # ---
772
+ #
773
+ # When +obj+ is an \Array, returns a \String containing a \JSON array:
774
+ # obj = ["foo", 1.0, true, false, nil]
775
+ # json = JSON.generate(obj)
776
+ # json # => '["foo",1.0,true,false,null]'
777
+ #
778
+ # When +obj+ is a \Hash, returns a \String containing a \JSON object:
779
+ # obj = {foo: 0, bar: 's', baz: :bat}
780
+ # json = JSON.generate(obj)
781
+ # json # => '{"foo":0,"bar":"s","baz":"bat"}'
782
+ #
783
+ # For examples of generating from other Ruby objects, see
784
+ # {Generating \JSON from Other Objects}[#module-JSON-label-Generating+JSON+from+Other+Objects].
785
+ #
786
+ # ---
787
+ #
788
+ # Raises an exception if any formatting option is not a \String.
789
+ #
790
+ # Raises an exception if +obj+ contains circular references:
791
+ # a = []; b = []; a.push(b); b.push(a)
792
+ # # Raises JSON::NestingError (nesting of 100 is too deep):
793
+ # JSON.generate(a)
794
+ #
795
+ # source://json//lib/json/common.rb#450
796
+ def generate(obj, opts = T.unsafe(nil)); end
797
+
798
+ # :call-seq:
799
+ # JSON.load(source, options = {}) -> object
800
+ # JSON.load(source, proc = nil, options = {}) -> object
801
+ #
802
+ # Returns the Ruby objects created by parsing the given +source+.
803
+ #
804
+ # BEWARE: This method is meant to serialise data from trusted user input,
805
+ # like from your own database server or clients under your control, it could
806
+ # be dangerous to allow untrusted users to pass JSON sources into it.
807
+ # If you must use it, use JSON.unsafe_load instead to make it clear.
808
+ #
809
+ # Since JSON version 2.8.0, `load` emits a deprecation warning when a
810
+ # non native type is deserialized, without `create_additions` being explicitly
811
+ # enabled, and in JSON version 3.0, `load` will have `create_additions` disabled
812
+ # by default.
813
+ #
814
+ # - Argument +source+ must be, or be convertible to, a \String:
815
+ # - If +source+ responds to instance method +to_str+,
816
+ # <tt>source.to_str</tt> becomes the source.
817
+ # - If +source+ responds to instance method +to_io+,
818
+ # <tt>source.to_io.read</tt> becomes the source.
819
+ # - If +source+ responds to instance method +read+,
820
+ # <tt>source.read</tt> becomes the source.
821
+ # - If both of the following are true, source becomes the \String <tt>'null'</tt>:
822
+ # - Option +allow_blank+ specifies a truthy value.
823
+ # - The source, as defined above, is +nil+ or the empty \String <tt>''</tt>.
824
+ # - Otherwise, +source+ remains the source.
825
+ # - Argument +proc+, if given, must be a \Proc that accepts one argument.
826
+ # It will be called recursively with each result (depth-first order).
827
+ # See details below.
828
+ # - Argument +opts+, if given, contains a \Hash of options for the parsing.
829
+ # See {Parsing Options}[#module-JSON-label-Parsing+Options].
830
+ # The default options can be changed via method JSON.load_default_options=.
831
+ #
832
+ # ---
833
+ #
834
+ # When no +proc+ is given, modifies +source+ as above and returns the result of
835
+ # <tt>parse(source, opts)</tt>; see #parse.
836
+ #
837
+ # Source for following examples:
838
+ # source = <<~JSON
839
+ # {
840
+ # "name": "Dave",
841
+ # "age" :40,
842
+ # "hats": [
843
+ # "Cattleman's",
844
+ # "Panama",
845
+ # "Tophat"
846
+ # ]
847
+ # }
848
+ # JSON
849
+ #
850
+ # Load a \String:
851
+ # ruby = JSON.load(source)
852
+ # ruby # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
853
+ #
854
+ # Load an \IO object:
855
+ # require 'stringio'
856
+ # object = JSON.load(StringIO.new(source))
857
+ # object # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
858
+ #
859
+ # Load a \File object:
860
+ # path = 't.json'
861
+ # File.write(path, source)
862
+ # File.open(path) do |file|
863
+ # JSON.load(file)
864
+ # end # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
865
+ #
866
+ # ---
867
+ #
868
+ # When +proc+ is given:
869
+ # - Modifies +source+ as above.
870
+ # - Gets the +result+ from calling <tt>parse(source, opts)</tt>.
871
+ # - Recursively calls <tt>proc(result)</tt>.
872
+ # - Returns the final result.
873
+ #
874
+ # Example:
875
+ # require 'json'
876
+ #
877
+ # # Some classes for the example.
878
+ # class Base
879
+ # def initialize(attributes)
880
+ # @attributes = attributes
881
+ # end
882
+ # end
883
+ # class User < Base; end
884
+ # class Account < Base; end
885
+ # class Admin < Base; end
886
+ # # The JSON source.
887
+ # json = <<-EOF
888
+ # {
889
+ # "users": [
890
+ # {"type": "User", "username": "jane", "email": "jane@example.com"},
891
+ # {"type": "User", "username": "john", "email": "john@example.com"}
892
+ # ],
893
+ # "accounts": [
894
+ # {"account": {"type": "Account", "paid": true, "account_id": "1234"}},
895
+ # {"account": {"type": "Account", "paid": false, "account_id": "1235"}}
896
+ # ],
897
+ # "admins": {"type": "Admin", "password": "0wn3d"}
898
+ # }
899
+ # EOF
900
+ # # Deserializer method.
901
+ # def deserialize_obj(obj, safe_types = %w(User Account Admin))
902
+ # type = obj.is_a?(Hash) && obj["type"]
903
+ # safe_types.include?(type) ? Object.const_get(type).new(obj) : obj
904
+ # end
905
+ # # Call to JSON.load
906
+ # ruby = JSON.load(json, proc {|obj|
907
+ # case obj
908
+ # when Hash
909
+ # obj.each {|k, v| obj[k] = deserialize_obj v }
910
+ # when Array
911
+ # obj.map! {|v| deserialize_obj v }
912
+ # end
913
+ # obj
914
+ # })
915
+ # pp ruby
916
+ # Output:
917
+ # {"users"=>
918
+ # [#<User:0x00000000064c4c98
919
+ # @attributes=
920
+ # {"type"=>"User", "username"=>"jane", "email"=>"jane@example.com"}>,
921
+ # #<User:0x00000000064c4bd0
922
+ # @attributes=
923
+ # {"type"=>"User", "username"=>"john", "email"=>"john@example.com"}>],
924
+ # "accounts"=>
925
+ # [{"account"=>
926
+ # #<Account:0x00000000064c4928
927
+ # @attributes={"type"=>"Account", "paid"=>true, "account_id"=>"1234"}>},
928
+ # {"account"=>
929
+ # #<Account:0x00000000064c4680
930
+ # @attributes={"type"=>"Account", "paid"=>false, "account_id"=>"1235"}>}],
931
+ # "admins"=>
932
+ # #<Admin:0x00000000064c41f8
933
+ # @attributes={"type"=>"Admin", "password"=>"0wn3d"}>}
934
+ #
935
+ # source://json//lib/json/common.rb#865
936
+ def load(source, proc = T.unsafe(nil), options = T.unsafe(nil)); end
937
+
938
+ # :call-seq:
939
+ # JSON.load_file(path, opts={}) -> object
940
+ #
941
+ # Calls:
942
+ # parse(File.read(path), opts)
943
+ #
944
+ # See method #parse.
945
+ #
946
+ # source://json//lib/json/common.rb#399
947
+ def load_file(filespec, opts = T.unsafe(nil)); end
948
+
949
+ # :call-seq:
950
+ # JSON.load_file!(path, opts = {})
951
+ #
952
+ # Calls:
953
+ # JSON.parse!(File.read(path, opts))
954
+ #
955
+ # See method #parse!
956
+ #
957
+ # source://json//lib/json/common.rb#410
958
+ def load_file!(filespec, opts = T.unsafe(nil)); end
959
+
960
+ # :call-seq:
961
+ # JSON.parse(source, opts) -> object
962
+ #
963
+ # Returns the Ruby objects created by parsing the given +source+.
964
+ #
965
+ # Argument +source+ contains the \String to be parsed.
966
+ #
967
+ # Argument +opts+, if given, contains a \Hash of options for the parsing.
968
+ # See {Parsing Options}[#module-JSON-label-Parsing+Options].
969
+ #
970
+ # ---
971
+ #
972
+ # When +source+ is a \JSON array, returns a Ruby \Array:
973
+ # source = '["foo", 1.0, true, false, null]'
974
+ # ruby = JSON.parse(source)
975
+ # ruby # => ["foo", 1.0, true, false, nil]
976
+ # ruby.class # => Array
977
+ #
978
+ # When +source+ is a \JSON object, returns a Ruby \Hash:
979
+ # source = '{"a": "foo", "b": 1.0, "c": true, "d": false, "e": null}'
980
+ # ruby = JSON.parse(source)
981
+ # ruby # => {"a"=>"foo", "b"=>1.0, "c"=>true, "d"=>false, "e"=>nil}
982
+ # ruby.class # => Hash
983
+ #
984
+ # For examples of parsing for all \JSON data types, see
985
+ # {Parsing \JSON}[#module-JSON-label-Parsing+JSON].
986
+ #
987
+ # Parses nested JSON objects:
988
+ # source = <<~JSON
989
+ # {
990
+ # "name": "Dave",
991
+ # "age" :40,
992
+ # "hats": [
993
+ # "Cattleman's",
994
+ # "Panama",
995
+ # "Tophat"
996
+ # ]
997
+ # }
998
+ # JSON
999
+ # ruby = JSON.parse(source)
1000
+ # ruby # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
1001
+ #
1002
+ # ---
1003
+ #
1004
+ # Raises an exception if +source+ is not valid JSON:
1005
+ # # Raises JSON::ParserError (783: unexpected token at ''):
1006
+ # JSON.parse('')
1007
+ #
1008
+ # source://json//lib/json/common.rb#362
1009
+ def parse(source, opts = T.unsafe(nil)); end
1010
+
1011
+ # :call-seq:
1012
+ # JSON.parse!(source, opts) -> object
1013
+ #
1014
+ # Calls
1015
+ # parse(source, opts)
1016
+ # with +source+ and possibly modified +opts+.
1017
+ #
1018
+ # Differences from JSON.parse:
1019
+ # - Option +max_nesting+, if not provided, defaults to +false+,
1020
+ # which disables checking for nesting depth.
1021
+ # - Option +allow_nan+, if not provided, defaults to +true+.
1022
+ #
1023
+ # source://json//lib/json/common.rb#384
1024
+ def parse!(source, opts = T.unsafe(nil)); end
1025
+
1026
+ # :call-seq:
1027
+ # JSON.pretty_generate(obj, opts = nil) -> new_string
1028
+ #
1029
+ # Arguments +obj+ and +opts+ here are the same as
1030
+ # arguments +obj+ and +opts+ in JSON.generate.
1031
+ #
1032
+ # Default options are:
1033
+ # {
1034
+ # indent: ' ', # Two spaces
1035
+ # space: ' ', # One space
1036
+ # array_nl: "\n", # Newline
1037
+ # object_nl: "\n" # Newline
1038
+ # }
1039
+ #
1040
+ # Example:
1041
+ # obj = {foo: [:bar, :baz], bat: {bam: 0, bad: 1}}
1042
+ # json = JSON.pretty_generate(obj)
1043
+ # puts json
1044
+ # Output:
1045
+ # {
1046
+ # "foo": [
1047
+ # "bar",
1048
+ # "baz"
1049
+ # ],
1050
+ # "bat": {
1051
+ # "bam": 0,
1052
+ # "bad": 1
1053
+ # }
1054
+ # }
1055
+ #
1056
+ # source://json//lib/json/common.rb#518
1057
+ def pretty_generate(obj, opts = T.unsafe(nil)); end
1058
+
1059
+ # source://json//lib/json/common.rb#996
1060
+ def pretty_unparse(*_arg0, **_arg1, &_arg2); end
1061
+
1062
+ # source://json//lib/json/common.rb#1006
1063
+ def restore(*_arg0, **_arg1, &_arg2); end
1064
+
1065
+ # :stopdoc:
1066
+ # All these were meant to be deprecated circa 2009, but were just set as undocumented
1067
+ # so usage still exist in the wild.
1068
+ #
1069
+ # source://json//lib/json/common.rb#976
1070
+ def unparse(*_arg0, **_arg1, &_arg2); end
1071
+
1072
+ # :call-seq:
1073
+ # JSON.unsafe_load(source, options = {}) -> object
1074
+ # JSON.unsafe_load(source, proc = nil, options = {}) -> object
1075
+ #
1076
+ # Returns the Ruby objects created by parsing the given +source+.
1077
+ #
1078
+ # BEWARE: This method is meant to serialise data from trusted user input,
1079
+ # like from your own database server or clients under your control, it could
1080
+ # be dangerous to allow untrusted users to pass JSON sources into it.
1081
+ #
1082
+ # - Argument +source+ must be, or be convertible to, a \String:
1083
+ # - If +source+ responds to instance method +to_str+,
1084
+ # <tt>source.to_str</tt> becomes the source.
1085
+ # - If +source+ responds to instance method +to_io+,
1086
+ # <tt>source.to_io.read</tt> becomes the source.
1087
+ # - If +source+ responds to instance method +read+,
1088
+ # <tt>source.read</tt> becomes the source.
1089
+ # - If both of the following are true, source becomes the \String <tt>'null'</tt>:
1090
+ # - Option +allow_blank+ specifies a truthy value.
1091
+ # - The source, as defined above, is +nil+ or the empty \String <tt>''</tt>.
1092
+ # - Otherwise, +source+ remains the source.
1093
+ # - Argument +proc+, if given, must be a \Proc that accepts one argument.
1094
+ # It will be called recursively with each result (depth-first order).
1095
+ # See details below.
1096
+ # - Argument +opts+, if given, contains a \Hash of options for the parsing.
1097
+ # See {Parsing Options}[#module-JSON-label-Parsing+Options].
1098
+ # The default options can be changed via method JSON.unsafe_load_default_options=.
1099
+ #
1100
+ # ---
1101
+ #
1102
+ # When no +proc+ is given, modifies +source+ as above and returns the result of
1103
+ # <tt>parse(source, opts)</tt>; see #parse.
1104
+ #
1105
+ # Source for following examples:
1106
+ # source = <<~JSON
1107
+ # {
1108
+ # "name": "Dave",
1109
+ # "age" :40,
1110
+ # "hats": [
1111
+ # "Cattleman's",
1112
+ # "Panama",
1113
+ # "Tophat"
1114
+ # ]
1115
+ # }
1116
+ # JSON
1117
+ #
1118
+ # Load a \String:
1119
+ # ruby = JSON.unsafe_load(source)
1120
+ # ruby # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
1121
+ #
1122
+ # Load an \IO object:
1123
+ # require 'stringio'
1124
+ # object = JSON.unsafe_load(StringIO.new(source))
1125
+ # object # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
1126
+ #
1127
+ # Load a \File object:
1128
+ # path = 't.json'
1129
+ # File.write(path, source)
1130
+ # File.open(path) do |file|
1131
+ # JSON.unsafe_load(file)
1132
+ # end # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
1133
+ #
1134
+ # ---
1135
+ #
1136
+ # When +proc+ is given:
1137
+ # - Modifies +source+ as above.
1138
+ # - Gets the +result+ from calling <tt>parse(source, opts)</tt>.
1139
+ # - Recursively calls <tt>proc(result)</tt>.
1140
+ # - Returns the final result.
1141
+ #
1142
+ # Example:
1143
+ # require 'json'
1144
+ #
1145
+ # # Some classes for the example.
1146
+ # class Base
1147
+ # def initialize(attributes)
1148
+ # @attributes = attributes
1149
+ # end
1150
+ # end
1151
+ # class User < Base; end
1152
+ # class Account < Base; end
1153
+ # class Admin < Base; end
1154
+ # # The JSON source.
1155
+ # json = <<-EOF
1156
+ # {
1157
+ # "users": [
1158
+ # {"type": "User", "username": "jane", "email": "jane@example.com"},
1159
+ # {"type": "User", "username": "john", "email": "john@example.com"}
1160
+ # ],
1161
+ # "accounts": [
1162
+ # {"account": {"type": "Account", "paid": true, "account_id": "1234"}},
1163
+ # {"account": {"type": "Account", "paid": false, "account_id": "1235"}}
1164
+ # ],
1165
+ # "admins": {"type": "Admin", "password": "0wn3d"}
1166
+ # }
1167
+ # EOF
1168
+ # # Deserializer method.
1169
+ # def deserialize_obj(obj, safe_types = %w(User Account Admin))
1170
+ # type = obj.is_a?(Hash) && obj["type"]
1171
+ # safe_types.include?(type) ? Object.const_get(type).new(obj) : obj
1172
+ # end
1173
+ # # Call to JSON.unsafe_load
1174
+ # ruby = JSON.unsafe_load(json, proc {|obj|
1175
+ # case obj
1176
+ # when Hash
1177
+ # obj.each {|k, v| obj[k] = deserialize_obj v }
1178
+ # when Array
1179
+ # obj.map! {|v| deserialize_obj v }
1180
+ # end
1181
+ # obj
1182
+ # })
1183
+ # pp ruby
1184
+ # Output:
1185
+ # {"users"=>
1186
+ # [#<User:0x00000000064c4c98
1187
+ # @attributes=
1188
+ # {"type"=>"User", "username"=>"jane", "email"=>"jane@example.com"}>,
1189
+ # #<User:0x00000000064c4bd0
1190
+ # @attributes=
1191
+ # {"type"=>"User", "username"=>"john", "email"=>"john@example.com"}>],
1192
+ # "accounts"=>
1193
+ # [{"account"=>
1194
+ # #<Account:0x00000000064c4928
1195
+ # @attributes={"type"=>"Account", "paid"=>true, "account_id"=>"1234"}>},
1196
+ # {"account"=>
1197
+ # #<Account:0x00000000064c4680
1198
+ # @attributes={"type"=>"Account", "paid"=>false, "account_id"=>"1235"}>}],
1199
+ # "admins"=>
1200
+ # #<Admin:0x00000000064c41f8
1201
+ # @attributes={"type"=>"Admin", "password"=>"0wn3d"}>}
1202
+ #
1203
+ # source://json//lib/json/common.rb#694
1204
+ def unsafe_load(source, proc = T.unsafe(nil), options = T.unsafe(nil)); end
1205
+
1206
+ class << self
1207
+ # :call-seq:
1208
+ # JSON[object] -> new_array or new_string
1209
+ #
1210
+ # If +object+ is a \String,
1211
+ # calls JSON.parse with +object+ and +opts+ (see method #parse):
1212
+ # json = '[0, 1, null]'
1213
+ # JSON[json]# => [0, 1, nil]
1214
+ #
1215
+ # Otherwise, calls JSON.generate with +object+ and +opts+ (see method #generate):
1216
+ # ruby = [0, 1, nil]
1217
+ # JSON[ruby] # => '[0,1,null]'
1218
+ #
1219
+ # source://json//lib/json/common.rb#132
1220
+ def [](object, opts = T.unsafe(nil)); end
1221
+
1222
+ # Returns the current create identifier.
1223
+ # See also JSON.create_id=.
1224
+ #
1225
+ # source://json//lib/json/common.rb#245
1226
+ def create_id; end
1227
+
1228
+ # Sets create identifier, which is used to decide if the _json_create_
1229
+ # hook of a class should be called; initial value is +json_class+:
1230
+ # JSON.create_id # => 'json_class'
1231
+ #
1232
+ # source://json//lib/json/common.rb#239
1233
+ def create_id=(new_value); end
1234
+
1235
+ # source://json//lib/json/common.rb#104
1236
+ def deprecation_warning(message, uplevel = T.unsafe(nil)); end
1237
+
1238
+ # :call-seq:
1239
+ # JSON.dump(obj, io = nil, limit = nil)
1240
+ #
1241
+ # Dumps +obj+ as a \JSON string, i.e. calls generate on the object and returns the result.
1242
+ #
1243
+ # The default options can be changed via method JSON.dump_default_options.
1244
+ #
1245
+ # - Argument +io+, if given, should respond to method +write+;
1246
+ # the \JSON \String is written to +io+, and +io+ is returned.
1247
+ # If +io+ is not given, the \JSON \String is returned.
1248
+ # - Argument +limit+, if given, is passed to JSON.generate as option +max_nesting+.
1249
+ #
1250
+ # ---
1251
+ #
1252
+ # When argument +io+ is not given, returns the \JSON \String generated from +obj+:
1253
+ # obj = {foo: [0, 1], bar: {baz: 2, bat: 3}, bam: :bad}
1254
+ # json = JSON.dump(obj)
1255
+ # json # => "{\"foo\":[0,1],\"bar\":{\"baz\":2,\"bat\":3},\"bam\":\"bad\"}"
1256
+ #
1257
+ # When argument +io+ is given, writes the \JSON \String to +io+ and returns +io+:
1258
+ # path = 't.json'
1259
+ # File.open(path, 'w') do |file|
1260
+ # JSON.dump(obj, file)
1261
+ # end # => #<File:t.json (closed)>
1262
+ # puts File.read(path)
1263
+ # Output:
1264
+ # {"foo":[0,1],"bar":{"baz":2,"bat":3},"bam":"bad"}
1265
+ #
1266
+ # source://json//lib/json/common.rb#941
1267
+ def dump(obj, anIO = T.unsafe(nil), limit = T.unsafe(nil), kwargs = T.unsafe(nil)); end
1268
+
1269
+ # :call-seq:
1270
+ # JSON.fast_generate(obj, opts) -> new_string
1271
+ #
1272
+ # Arguments +obj+ and +opts+ here are the same as
1273
+ # arguments +obj+ and +opts+ in JSON.generate.
1274
+ #
1275
+ # By default, generates \JSON data without checking
1276
+ # for circular references in +obj+ (option +max_nesting+ set to +false+, disabled).
1277
+ #
1278
+ # Raises an exception if +obj+ contains circular references:
1279
+ # a = []; b = []; a.push(b); b.push(a)
1280
+ # # Raises SystemStackError (stack level too deep):
1281
+ # JSON.fast_generate(a)
1282
+ #
1283
+ # source://json//lib/json/common.rb#471
1284
+ def fast_generate(obj, opts = T.unsafe(nil)); end
1285
+
1286
+ # source://json//lib/json/common.rb#986
1287
+ def fast_unparse(*_arg0, **_arg1, &_arg2); end
1288
+
1289
+ # :call-seq:
1290
+ # JSON.generate(obj, opts = nil) -> new_string
1291
+ #
1292
+ # Returns a \String containing the generated \JSON data.
1293
+ #
1294
+ # See also JSON.pretty_generate.
1295
+ #
1296
+ # Argument +obj+ is the Ruby object to be converted to \JSON.
1297
+ #
1298
+ # Argument +opts+, if given, contains a \Hash of options for the generation.
1299
+ # See {Generating Options}[#module-JSON-label-Generating+Options].
1300
+ #
1301
+ # ---
1302
+ #
1303
+ # When +obj+ is an \Array, returns a \String containing a \JSON array:
1304
+ # obj = ["foo", 1.0, true, false, nil]
1305
+ # json = JSON.generate(obj)
1306
+ # json # => '["foo",1.0,true,false,null]'
1307
+ #
1308
+ # When +obj+ is a \Hash, returns a \String containing a \JSON object:
1309
+ # obj = {foo: 0, bar: 's', baz: :bat}
1310
+ # json = JSON.generate(obj)
1311
+ # json # => '{"foo":0,"bar":"s","baz":"bat"}'
1312
+ #
1313
+ # For examples of generating from other Ruby objects, see
1314
+ # {Generating \JSON from Other Objects}[#module-JSON-label-Generating+JSON+from+Other+Objects].
1315
+ #
1316
+ # ---
1317
+ #
1318
+ # Raises an exception if any formatting option is not a \String.
1319
+ #
1320
+ # Raises an exception if +obj+ contains circular references:
1321
+ # a = []; b = []; a.push(b); b.push(a)
1322
+ # # Raises JSON::NestingError (nesting of 100 is too deep):
1323
+ # JSON.generate(a)
1324
+ #
1325
+ # source://json//lib/json/common.rb#450
1326
+ def generate(obj, opts = T.unsafe(nil)); end
1327
+
1328
+ # Returns the JSON generator module that is used by JSON.
1329
+ #
1330
+ # source://json//lib/json/common.rb#188
1331
+ def generator; end
1332
+
1333
+ # Set the module _generator_ to be used by JSON.
1334
+ #
1335
+ # source://json//lib/json/common.rb#156
1336
+ def generator=(generator); end
1337
+
1338
+ # :call-seq:
1339
+ # JSON.load(source, options = {}) -> object
1340
+ # JSON.load(source, proc = nil, options = {}) -> object
1341
+ #
1342
+ # Returns the Ruby objects created by parsing the given +source+.
1343
+ #
1344
+ # BEWARE: This method is meant to serialise data from trusted user input,
1345
+ # like from your own database server or clients under your control, it could
1346
+ # be dangerous to allow untrusted users to pass JSON sources into it.
1347
+ # If you must use it, use JSON.unsafe_load instead to make it clear.
1348
+ #
1349
+ # Since JSON version 2.8.0, `load` emits a deprecation warning when a
1350
+ # non native type is deserialized, without `create_additions` being explicitly
1351
+ # enabled, and in JSON version 3.0, `load` will have `create_additions` disabled
1352
+ # by default.
1353
+ #
1354
+ # - Argument +source+ must be, or be convertible to, a \String:
1355
+ # - If +source+ responds to instance method +to_str+,
1356
+ # <tt>source.to_str</tt> becomes the source.
1357
+ # - If +source+ responds to instance method +to_io+,
1358
+ # <tt>source.to_io.read</tt> becomes the source.
1359
+ # - If +source+ responds to instance method +read+,
1360
+ # <tt>source.read</tt> becomes the source.
1361
+ # - If both of the following are true, source becomes the \String <tt>'null'</tt>:
1362
+ # - Option +allow_blank+ specifies a truthy value.
1363
+ # - The source, as defined above, is +nil+ or the empty \String <tt>''</tt>.
1364
+ # - Otherwise, +source+ remains the source.
1365
+ # - Argument +proc+, if given, must be a \Proc that accepts one argument.
1366
+ # It will be called recursively with each result (depth-first order).
1367
+ # See details below.
1368
+ # - Argument +opts+, if given, contains a \Hash of options for the parsing.
1369
+ # See {Parsing Options}[#module-JSON-label-Parsing+Options].
1370
+ # The default options can be changed via method JSON.load_default_options=.
1371
+ #
1372
+ # ---
1373
+ #
1374
+ # When no +proc+ is given, modifies +source+ as above and returns the result of
1375
+ # <tt>parse(source, opts)</tt>; see #parse.
1376
+ #
1377
+ # Source for following examples:
1378
+ # source = <<~JSON
1379
+ # {
1380
+ # "name": "Dave",
1381
+ # "age" :40,
1382
+ # "hats": [
1383
+ # "Cattleman's",
1384
+ # "Panama",
1385
+ # "Tophat"
1386
+ # ]
1387
+ # }
1388
+ # JSON
1389
+ #
1390
+ # Load a \String:
1391
+ # ruby = JSON.load(source)
1392
+ # ruby # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
1393
+ #
1394
+ # Load an \IO object:
1395
+ # require 'stringio'
1396
+ # object = JSON.load(StringIO.new(source))
1397
+ # object # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
1398
+ #
1399
+ # Load a \File object:
1400
+ # path = 't.json'
1401
+ # File.write(path, source)
1402
+ # File.open(path) do |file|
1403
+ # JSON.load(file)
1404
+ # end # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
1405
+ #
1406
+ # ---
1407
+ #
1408
+ # When +proc+ is given:
1409
+ # - Modifies +source+ as above.
1410
+ # - Gets the +result+ from calling <tt>parse(source, opts)</tt>.
1411
+ # - Recursively calls <tt>proc(result)</tt>.
1412
+ # - Returns the final result.
1413
+ #
1414
+ # Example:
1415
+ # require 'json'
1416
+ #
1417
+ # # Some classes for the example.
1418
+ # class Base
1419
+ # def initialize(attributes)
1420
+ # @attributes = attributes
1421
+ # end
1422
+ # end
1423
+ # class User < Base; end
1424
+ # class Account < Base; end
1425
+ # class Admin < Base; end
1426
+ # # The JSON source.
1427
+ # json = <<-EOF
1428
+ # {
1429
+ # "users": [
1430
+ # {"type": "User", "username": "jane", "email": "jane@example.com"},
1431
+ # {"type": "User", "username": "john", "email": "john@example.com"}
1432
+ # ],
1433
+ # "accounts": [
1434
+ # {"account": {"type": "Account", "paid": true, "account_id": "1234"}},
1435
+ # {"account": {"type": "Account", "paid": false, "account_id": "1235"}}
1436
+ # ],
1437
+ # "admins": {"type": "Admin", "password": "0wn3d"}
1438
+ # }
1439
+ # EOF
1440
+ # # Deserializer method.
1441
+ # def deserialize_obj(obj, safe_types = %w(User Account Admin))
1442
+ # type = obj.is_a?(Hash) && obj["type"]
1443
+ # safe_types.include?(type) ? Object.const_get(type).new(obj) : obj
1444
+ # end
1445
+ # # Call to JSON.load
1446
+ # ruby = JSON.load(json, proc {|obj|
1447
+ # case obj
1448
+ # when Hash
1449
+ # obj.each {|k, v| obj[k] = deserialize_obj v }
1450
+ # when Array
1451
+ # obj.map! {|v| deserialize_obj v }
1452
+ # end
1453
+ # obj
1454
+ # })
1455
+ # pp ruby
1456
+ # Output:
1457
+ # {"users"=>
1458
+ # [#<User:0x00000000064c4c98
1459
+ # @attributes=
1460
+ # {"type"=>"User", "username"=>"jane", "email"=>"jane@example.com"}>,
1461
+ # #<User:0x00000000064c4bd0
1462
+ # @attributes=
1463
+ # {"type"=>"User", "username"=>"john", "email"=>"john@example.com"}>],
1464
+ # "accounts"=>
1465
+ # [{"account"=>
1466
+ # #<Account:0x00000000064c4928
1467
+ # @attributes={"type"=>"Account", "paid"=>true, "account_id"=>"1234"}>},
1468
+ # {"account"=>
1469
+ # #<Account:0x00000000064c4680
1470
+ # @attributes={"type"=>"Account", "paid"=>false, "account_id"=>"1235"}>}],
1471
+ # "admins"=>
1472
+ # #<Admin:0x00000000064c41f8
1473
+ # @attributes={"type"=>"Admin", "password"=>"0wn3d"}>}
1474
+ #
1475
+ # source://json//lib/json/common.rb#865
1476
+ def load(source, proc = T.unsafe(nil), options = T.unsafe(nil)); end
1477
+
1478
+ # :call-seq:
1479
+ # JSON.load_file(path, opts={}) -> object
1480
+ #
1481
+ # Calls:
1482
+ # parse(File.read(path), opts)
1483
+ #
1484
+ # See method #parse.
1485
+ #
1486
+ # source://json//lib/json/common.rb#399
1487
+ def load_file(filespec, opts = T.unsafe(nil)); end
1488
+
1489
+ # :call-seq:
1490
+ # JSON.load_file!(path, opts = {})
1491
+ #
1492
+ # Calls:
1493
+ # JSON.parse!(File.read(path, opts))
1494
+ #
1495
+ # See method #parse!
1496
+ #
1497
+ # source://json//lib/json/common.rb#410
1498
+ def load_file!(filespec, opts = T.unsafe(nil)); end
1499
+
1500
+ # :call-seq:
1501
+ # JSON.parse(source, opts) -> object
1502
+ #
1503
+ # Returns the Ruby objects created by parsing the given +source+.
1504
+ #
1505
+ # Argument +source+ contains the \String to be parsed.
1506
+ #
1507
+ # Argument +opts+, if given, contains a \Hash of options for the parsing.
1508
+ # See {Parsing Options}[#module-JSON-label-Parsing+Options].
1509
+ #
1510
+ # ---
1511
+ #
1512
+ # When +source+ is a \JSON array, returns a Ruby \Array:
1513
+ # source = '["foo", 1.0, true, false, null]'
1514
+ # ruby = JSON.parse(source)
1515
+ # ruby # => ["foo", 1.0, true, false, nil]
1516
+ # ruby.class # => Array
1517
+ #
1518
+ # When +source+ is a \JSON object, returns a Ruby \Hash:
1519
+ # source = '{"a": "foo", "b": 1.0, "c": true, "d": false, "e": null}'
1520
+ # ruby = JSON.parse(source)
1521
+ # ruby # => {"a"=>"foo", "b"=>1.0, "c"=>true, "d"=>false, "e"=>nil}
1522
+ # ruby.class # => Hash
1523
+ #
1524
+ # For examples of parsing for all \JSON data types, see
1525
+ # {Parsing \JSON}[#module-JSON-label-Parsing+JSON].
1526
+ #
1527
+ # Parses nested JSON objects:
1528
+ # source = <<~JSON
1529
+ # {
1530
+ # "name": "Dave",
1531
+ # "age" :40,
1532
+ # "hats": [
1533
+ # "Cattleman's",
1534
+ # "Panama",
1535
+ # "Tophat"
1536
+ # ]
1537
+ # }
1538
+ # JSON
1539
+ # ruby = JSON.parse(source)
1540
+ # ruby # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
1541
+ #
1542
+ # ---
1543
+ #
1544
+ # Raises an exception if +source+ is not valid JSON:
1545
+ # # Raises JSON::ParserError (783: unexpected token at ''):
1546
+ # JSON.parse('')
1547
+ #
1548
+ # source://json//lib/json/common.rb#362
1549
+ def parse(source, opts = T.unsafe(nil)); end
1550
+
1551
+ # :call-seq:
1552
+ # JSON.parse!(source, opts) -> object
1553
+ #
1554
+ # Calls
1555
+ # parse(source, opts)
1556
+ # with +source+ and possibly modified +opts+.
1557
+ #
1558
+ # Differences from JSON.parse:
1559
+ # - Option +max_nesting+, if not provided, defaults to +false+,
1560
+ # which disables checking for nesting depth.
1561
+ # - Option +allow_nan+, if not provided, defaults to +true+.
1562
+ #
1563
+ # source://json//lib/json/common.rb#384
1564
+ def parse!(source, opts = T.unsafe(nil)); end
1565
+
1566
+ # Returns the JSON parser class that is used by JSON.
1567
+ #
1568
+ # source://json//lib/json/common.rb#146
1569
+ def parser; end
1570
+
1571
+ # Set the JSON parser class _parser_ to be used by JSON.
1572
+ #
1573
+ # source://json//lib/json/common.rb#149
1574
+ def parser=(parser); end
1575
+
1576
+ # :call-seq:
1577
+ # JSON.pretty_generate(obj, opts = nil) -> new_string
1578
+ #
1579
+ # Arguments +obj+ and +opts+ here are the same as
1580
+ # arguments +obj+ and +opts+ in JSON.generate.
1581
+ #
1582
+ # Default options are:
1583
+ # {
1584
+ # indent: ' ', # Two spaces
1585
+ # space: ' ', # One space
1586
+ # array_nl: "\n", # Newline
1587
+ # object_nl: "\n" # Newline
1588
+ # }
1589
+ #
1590
+ # Example:
1591
+ # obj = {foo: [:bar, :baz], bat: {bam: 0, bad: 1}}
1592
+ # json = JSON.pretty_generate(obj)
1593
+ # puts json
1594
+ # Output:
1595
+ # {
1596
+ # "foo": [
1597
+ # "bar",
1598
+ # "baz"
1599
+ # ],
1600
+ # "bat": {
1601
+ # "bam": 0,
1602
+ # "bad": 1
1603
+ # }
1604
+ # }
1605
+ #
1606
+ # source://json//lib/json/common.rb#518
1607
+ def pretty_generate(obj, opts = T.unsafe(nil)); end
1608
+
1609
+ # source://json//lib/json/common.rb#996
1610
+ def pretty_unparse(*_arg0, **_arg1, &_arg2); end
1611
+
1612
+ # source://json//lib/json/common.rb#1006
1613
+ def restore(*_arg0, **_arg1, &_arg2); end
1614
+
1615
+ # Sets or Returns the JSON generator state class that is used by JSON.
1616
+ #
1617
+ # source://json//lib/json/common.rb#191
1618
+ def state; end
1619
+
1620
+ # Sets or Returns the JSON generator state class that is used by JSON.
1621
+ #
1622
+ # source://json//lib/json/common.rb#191
1623
+ def state=(_arg0); end
1624
+
1625
+ # :stopdoc:
1626
+ # All these were meant to be deprecated circa 2009, but were just set as undocumented
1627
+ # so usage still exist in the wild.
1628
+ #
1629
+ # source://json//lib/json/common.rb#976
1630
+ def unparse(*_arg0, **_arg1, &_arg2); end
1631
+
1632
+ # :call-seq:
1633
+ # JSON.unsafe_load(source, options = {}) -> object
1634
+ # JSON.unsafe_load(source, proc = nil, options = {}) -> object
1635
+ #
1636
+ # Returns the Ruby objects created by parsing the given +source+.
1637
+ #
1638
+ # BEWARE: This method is meant to serialise data from trusted user input,
1639
+ # like from your own database server or clients under your control, it could
1640
+ # be dangerous to allow untrusted users to pass JSON sources into it.
1641
+ #
1642
+ # - Argument +source+ must be, or be convertible to, a \String:
1643
+ # - If +source+ responds to instance method +to_str+,
1644
+ # <tt>source.to_str</tt> becomes the source.
1645
+ # - If +source+ responds to instance method +to_io+,
1646
+ # <tt>source.to_io.read</tt> becomes the source.
1647
+ # - If +source+ responds to instance method +read+,
1648
+ # <tt>source.read</tt> becomes the source.
1649
+ # - If both of the following are true, source becomes the \String <tt>'null'</tt>:
1650
+ # - Option +allow_blank+ specifies a truthy value.
1651
+ # - The source, as defined above, is +nil+ or the empty \String <tt>''</tt>.
1652
+ # - Otherwise, +source+ remains the source.
1653
+ # - Argument +proc+, if given, must be a \Proc that accepts one argument.
1654
+ # It will be called recursively with each result (depth-first order).
1655
+ # See details below.
1656
+ # - Argument +opts+, if given, contains a \Hash of options for the parsing.
1657
+ # See {Parsing Options}[#module-JSON-label-Parsing+Options].
1658
+ # The default options can be changed via method JSON.unsafe_load_default_options=.
1659
+ #
1660
+ # ---
1661
+ #
1662
+ # When no +proc+ is given, modifies +source+ as above and returns the result of
1663
+ # <tt>parse(source, opts)</tt>; see #parse.
1664
+ #
1665
+ # Source for following examples:
1666
+ # source = <<~JSON
1667
+ # {
1668
+ # "name": "Dave",
1669
+ # "age" :40,
1670
+ # "hats": [
1671
+ # "Cattleman's",
1672
+ # "Panama",
1673
+ # "Tophat"
1674
+ # ]
1675
+ # }
1676
+ # JSON
1677
+ #
1678
+ # Load a \String:
1679
+ # ruby = JSON.unsafe_load(source)
1680
+ # ruby # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
1681
+ #
1682
+ # Load an \IO object:
1683
+ # require 'stringio'
1684
+ # object = JSON.unsafe_load(StringIO.new(source))
1685
+ # object # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
1686
+ #
1687
+ # Load a \File object:
1688
+ # path = 't.json'
1689
+ # File.write(path, source)
1690
+ # File.open(path) do |file|
1691
+ # JSON.unsafe_load(file)
1692
+ # end # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
1693
+ #
1694
+ # ---
1695
+ #
1696
+ # When +proc+ is given:
1697
+ # - Modifies +source+ as above.
1698
+ # - Gets the +result+ from calling <tt>parse(source, opts)</tt>.
1699
+ # - Recursively calls <tt>proc(result)</tt>.
1700
+ # - Returns the final result.
1701
+ #
1702
+ # Example:
1703
+ # require 'json'
1704
+ #
1705
+ # # Some classes for the example.
1706
+ # class Base
1707
+ # def initialize(attributes)
1708
+ # @attributes = attributes
1709
+ # end
1710
+ # end
1711
+ # class User < Base; end
1712
+ # class Account < Base; end
1713
+ # class Admin < Base; end
1714
+ # # The JSON source.
1715
+ # json = <<-EOF
1716
+ # {
1717
+ # "users": [
1718
+ # {"type": "User", "username": "jane", "email": "jane@example.com"},
1719
+ # {"type": "User", "username": "john", "email": "john@example.com"}
1720
+ # ],
1721
+ # "accounts": [
1722
+ # {"account": {"type": "Account", "paid": true, "account_id": "1234"}},
1723
+ # {"account": {"type": "Account", "paid": false, "account_id": "1235"}}
1724
+ # ],
1725
+ # "admins": {"type": "Admin", "password": "0wn3d"}
1726
+ # }
1727
+ # EOF
1728
+ # # Deserializer method.
1729
+ # def deserialize_obj(obj, safe_types = %w(User Account Admin))
1730
+ # type = obj.is_a?(Hash) && obj["type"]
1731
+ # safe_types.include?(type) ? Object.const_get(type).new(obj) : obj
1732
+ # end
1733
+ # # Call to JSON.unsafe_load
1734
+ # ruby = JSON.unsafe_load(json, proc {|obj|
1735
+ # case obj
1736
+ # when Hash
1737
+ # obj.each {|k, v| obj[k] = deserialize_obj v }
1738
+ # when Array
1739
+ # obj.map! {|v| deserialize_obj v }
1740
+ # end
1741
+ # obj
1742
+ # })
1743
+ # pp ruby
1744
+ # Output:
1745
+ # {"users"=>
1746
+ # [#<User:0x00000000064c4c98
1747
+ # @attributes=
1748
+ # {"type"=>"User", "username"=>"jane", "email"=>"jane@example.com"}>,
1749
+ # #<User:0x00000000064c4bd0
1750
+ # @attributes=
1751
+ # {"type"=>"User", "username"=>"john", "email"=>"john@example.com"}>],
1752
+ # "accounts"=>
1753
+ # [{"account"=>
1754
+ # #<Account:0x00000000064c4928
1755
+ # @attributes={"type"=>"Account", "paid"=>true, "account_id"=>"1234"}>},
1756
+ # {"account"=>
1757
+ # #<Account:0x00000000064c4680
1758
+ # @attributes={"type"=>"Account", "paid"=>false, "account_id"=>"1235"}>}],
1759
+ # "admins"=>
1760
+ # #<Admin:0x00000000064c41f8
1761
+ # @attributes={"type"=>"Admin", "password"=>"0wn3d"}>}
1762
+ #
1763
+ # source://json//lib/json/common.rb#694
1764
+ def unsafe_load(source, proc = T.unsafe(nil), options = T.unsafe(nil)); end
1765
+
1766
+ private
1767
+
1768
+ # source://json//lib/json/common.rb#1019
1769
+ def const_missing(const_name); end
1770
+
1771
+ # source://json//lib/json/common.rb#214
1772
+ def deprecated_singleton_attr_accessor(*attrs); end
1773
+
1774
+ # Called from the extension when a hash has both string and symbol keys
1775
+ #
1776
+ # source://json//lib/json/common.rb#196
1777
+ def on_mixed_keys_hash(hash, do_raise); end
1778
+ end
1779
+ end
1780
+
1781
+ # JSON::Coder holds a parser and generator configuration.
1782
+ #
1783
+ # module MyApp
1784
+ # JSONC_CODER = JSON::Coder.new(
1785
+ # allow_trailing_comma: true
1786
+ # )
1787
+ # end
1788
+ #
1789
+ # MyApp::JSONC_CODER.load(document)
1790
+ #
1791
+ # source://json//lib/json/common.rb#1045
1792
+ class JSON::Coder
1793
+ # :call-seq:
1794
+ # JSON.new(options = nil, &block)
1795
+ #
1796
+ # Argument +options+, if given, contains a \Hash of options for both parsing and generating.
1797
+ # See {Parsing Options}[rdoc-ref:JSON@Parsing+Options],
1798
+ # and {Generating Options}[rdoc-ref:JSON@Generating+Options].
1799
+ #
1800
+ # For generation, the <tt>strict: true</tt> option is always set. When a Ruby object with no native \JSON counterpart is
1801
+ # encountered, the block provided to the initialize method is invoked, and must return a Ruby object that has a native
1802
+ # \JSON counterpart:
1803
+ #
1804
+ # module MyApp
1805
+ # API_JSON_CODER = JSON::Coder.new do |object|
1806
+ # case object
1807
+ # when Time
1808
+ # object.iso8601(3)
1809
+ # else
1810
+ # object # Unknown type, will raise
1811
+ # end
1812
+ # end
1813
+ # end
1814
+ #
1815
+ # puts MyApp::API_JSON_CODER.dump(Time.now.utc) # => "2025-01-21T08:41:44.286Z"
1816
+ #
1817
+ # @return [Coder] a new instance of Coder
1818
+ #
1819
+ # source://json//lib/json/common.rb#1070
1820
+ def initialize(options = T.unsafe(nil), &as_json); end
1821
+
1822
+ # call-seq:
1823
+ # dump(object) -> String
1824
+ # dump(object, io) -> io
1825
+ #
1826
+ # Serialize the given object into a \JSON document.
1827
+ #
1828
+ # source://json//lib/json/common.rb#1088
1829
+ def dump(object, io = T.unsafe(nil)); end
1830
+
1831
+ # call-seq:
1832
+ # dump(object) -> String
1833
+ # dump(object, io) -> io
1834
+ #
1835
+ # Serialize the given object into a \JSON document.
1836
+ #
1837
+ # source://json//lib/json/common.rb#1088
1838
+ def generate(object, io = T.unsafe(nil)); end
1839
+
1840
+ # call-seq:
1841
+ # load(string) -> Object
1842
+ #
1843
+ # Parse the given \JSON document and return an equivalent Ruby object.
1844
+ #
1845
+ # source://json//lib/json/common.rb#1097
1846
+ def load(source); end
1847
+
1848
+ # call-seq:
1849
+ # load(path) -> Object
1850
+ #
1851
+ # Parse the given \JSON document and return an equivalent Ruby object.
1852
+ #
1853
+ # source://json//lib/json/common.rb#1106
1854
+ def load_file(path); end
1855
+
1856
+ # call-seq:
1857
+ # load(string) -> Object
1858
+ #
1859
+ # Parse the given \JSON document and return an equivalent Ruby object.
1860
+ #
1861
+ # source://json//lib/json/common.rb#1097
1862
+ def parse(source); end
1863
+ end
1864
+
1865
+ # source://json//lib/json/ext/generator/state.rb#6
1866
+ class JSON::Ext::Generator::State
1867
+ # call-seq: new(opts = {})
1868
+ #
1869
+ # Instantiates a new State object, configured by _opts_.
1870
+ #
1871
+ # Argument +opts+, if given, contains a \Hash of options for the generation.
1872
+ # See {Generating Options}[rdoc-ref:JSON@Generating+Options].
1873
+ #
1874
+ # @return [State] a new instance of State
1875
+ #
1876
+ # source://json//lib/json/ext/generator/state.rb#13
1877
+ def initialize(opts = T.unsafe(nil)); end
1878
+
1879
+ # call-seq: [](name)
1880
+ #
1881
+ # Returns the value returned by method +name+.
1882
+ #
1883
+ # source://json//lib/json/ext/generator/state.rb#78
1884
+ def [](name); end
1885
+
1886
+ # call-seq: []=(name, value)
1887
+ #
1888
+ # Sets the attribute name to value.
1889
+ #
1890
+ # source://json//lib/json/ext/generator/state.rb#92
1891
+ def []=(name, value); end
1892
+
1893
+ # call-seq: configure(opts)
1894
+ #
1895
+ # Configure this State instance with the Hash _opts_, and return
1896
+ # itself.
1897
+ #
1898
+ # source://json//lib/json/ext/generator/state.rb#23
1899
+ def configure(opts); end
1900
+
1901
+ # call-seq: configure(opts)
1902
+ #
1903
+ # Configure this State instance with the Hash _opts_, and return
1904
+ # itself.
1905
+ #
1906
+ # source://json//lib/json/ext/generator/state.rb#23
1907
+ def merge(opts); end
1908
+
1909
+ # call-seq: to_h
1910
+ #
1911
+ # Returns the configuration instance variables as a hash, that can be
1912
+ # passed to the configure method.
1913
+ #
1914
+ # source://json//lib/json/ext/generator/state.rb#42
1915
+ def to_h; end
1916
+
1917
+ # call-seq: to_h
1918
+ #
1919
+ # Returns the configuration instance variables as a hash, that can be
1920
+ # passed to the configure method.
1921
+ #
1922
+ # source://json//lib/json/ext/generator/state.rb#42
1923
+ def to_hash; end
1924
+ end
1925
+
1926
+ # source://json//lib/json/ext.rb#9
1927
+ class JSON::Ext::Parser
1928
+ # @return [Parser] a new instance of Parser
1929
+ #
1930
+ # source://json//lib/json/ext.rb#17
1931
+ def initialize(source, opts = T.unsafe(nil)); end
1932
+
1933
+ # source://json//lib/json/ext.rb#26
1934
+ def parse; end
1935
+
1936
+ # source://json//lib/json/ext.rb#22
1937
+ def source; end
1938
+ end
1939
+
1940
+ # source://json//lib/json/ext.rb#32
1941
+ JSON::Ext::Parser::Config = JSON::Ext::ParserConfig
1942
+
1943
+ # Fragment of JSON document that is to be included as is:
1944
+ # fragment = JSON::Fragment.new("[1, 2, 3]")
1945
+ # JSON.generate({ count: 3, items: fragments })
1946
+ #
1947
+ # This allows to easily assemble multiple JSON fragments that have
1948
+ # been persisted somewhere without having to parse them nor resorting
1949
+ # to string interpolation.
1950
+ #
1951
+ # Note: no validation is performed on the provided string. It is the
1952
+ # responsibility of the caller to ensure the string contains valid JSON.
1953
+ #
1954
+ # source://json//lib/json/common.rb#298
1955
+ class JSON::Fragment < ::Struct
1956
+ # @return [Fragment] a new instance of Fragment
1957
+ #
1958
+ # source://json//lib/json/common.rb#299
1959
+ def initialize(json); end
1960
+
1961
+ # Returns the value of attribute json
1962
+ #
1963
+ # @return [Object] the current value of json
1964
+ def json; end
1965
+
1966
+ # Sets the attribute json
1967
+ #
1968
+ # @param value [Object] the value to set the attribute json to.
1969
+ # @return [Object] the newly set value
1970
+ def json=(_); end
1971
+
1972
+ # source://json//lib/json/common.rb#307
1973
+ def to_json(state = T.unsafe(nil), *_arg1); end
1974
+
1975
+ class << self
1976
+ def [](*_arg0); end
1977
+ def inspect; end
1978
+ def keyword_init?; end
1979
+ def members; end
1980
+ def new(*_arg0); end
1981
+ end
1982
+ end
1983
+
1984
+ # This exception is raised if a generator or unparser error occurs.
1985
+ #
1986
+ # source://json//lib/json/common.rb#268
1987
+ class JSON::GeneratorError < ::JSON::JSONError
1988
+ # @return [GeneratorError] a new instance of GeneratorError
1989
+ #
1990
+ # source://json//lib/json/common.rb#271
1991
+ def initialize(message, invalid_object = T.unsafe(nil)); end
1992
+
1993
+ # source://json//lib/json/common.rb#276
1994
+ def detailed_message(*_arg0, **_arg1, &_arg2); end
1995
+
1996
+ # Returns the value of attribute invalid_object.
1997
+ #
1998
+ # source://json//lib/json/common.rb#269
1999
+ def invalid_object; end
2000
+ end
2001
+
2002
+ # source://json//lib/json/common.rb#1111
2003
+ module JSON::GeneratorMethods
2004
+ # call-seq: to_json(*)
2005
+ #
2006
+ # Converts this object into a JSON string.
2007
+ # If this object doesn't directly maps to a JSON native type,
2008
+ # first convert it to a string (calling #to_s), then converts
2009
+ # it to a JSON string, and returns the result.
2010
+ # This is a fallback, if no special method #to_json was defined for some object.
2011
+ #
2012
+ # source://json//lib/json/common.rb#1119
2013
+ def to_json(state = T.unsafe(nil), *_arg1); end
2014
+ end
2015
+
2016
+ # source://json//lib/json/common.rb#367
2017
+ JSON::PARSE_L_OPTIONS = T.let(T.unsafe(nil), Hash)
2018
+
2019
+ # source://json//lib/json/common.rb#480
2020
+ JSON::PRETTY_GENERATE_OPTIONS = T.let(T.unsafe(nil), Hash)
2021
+
2022
+ # source://json//lib/json/common.rb#152
2023
+ JSON::Parser = JSON::Ext::Parser
2024
+
2025
+ # This exception is raised if a parser error occurs.
2026
+ #
2027
+ # source://json//lib/json/common.rb#259
2028
+ class JSON::ParserError < ::JSON::JSONError
2029
+ # Returns the value of attribute column.
2030
+ #
2031
+ # source://json//lib/json/common.rb#260
2032
+ def column; end
2033
+
2034
+ # Returns the value of attribute line.
2035
+ #
2036
+ # source://json//lib/json/common.rb#260
2037
+ def line; end
2038
+ end
2039
+
2040
+ # source://json//lib/json/common.rb#8
2041
+ module JSON::ParserOptions
2042
+ class << self
2043
+ # source://json//lib/json/common.rb#10
2044
+ def prepare(opts); end
2045
+
2046
+ private
2047
+
2048
+ # source://json//lib/json/common.rb#40
2049
+ def array_class_proc(array_class, on_load); end
2050
+
2051
+ # TODO: extract :create_additions support to another gem for version 3.0
2052
+ #
2053
+ # source://json//lib/json/common.rb#52
2054
+ def create_additions_proc(opts); end
2055
+
2056
+ # source://json//lib/json/common.rb#95
2057
+ def create_additions_warning; end
2058
+
2059
+ # source://json//lib/json/common.rb#29
2060
+ def object_class_proc(object_class, on_load); end
2061
+ end
2062
+ end
2063
+
2064
+ # source://json//lib/json/ext.rb#45
2065
+ class JSON::ResumableParser
2066
+ def initialize(*_arg0); end
2067
+
2068
+ def <<(_arg0); end
2069
+ def clear; end
2070
+
2071
+ # Returns whether the parser is entirely done: no unconsumed bytes in
2072
+ # the buffer, no document under construction and no parsed value
2073
+ # awaiting retrieval.
2074
+ #
2075
+ # The main use case is detecting a truncated stream once the input is
2076
+ # exhausted:
2077
+ #
2078
+ # loop do
2079
+ # begin
2080
+ # parser << socket.readpartial(4096)
2081
+ # rescue EOFError
2082
+ # break
2083
+ # end
2084
+ # while parser.parse
2085
+ # process(parser.value)
2086
+ # end
2087
+ # end
2088
+ # warn "stream was truncated" unless parser.empty?
2089
+ #
2090
+ # @return [Boolean]
2091
+ #
2092
+ # source://json//lib/json/ext.rb#64
2093
+ def empty?; end
2094
+
2095
+ def eos?; end
2096
+ def parse; end
2097
+ def parsed_bytes; end
2098
+ def partial_value; end
2099
+ def partial_value?; end
2100
+ def rest; end
2101
+ def value; end
2102
+ def value?; end
2103
+ end
2104
+
2105
+ # source://json//lib/json/common.rb#182
2106
+ JSON::State = JSON::Ext::Generator::State
2107
+
2108
+ # source://json//lib/json/common.rb#1136
2109
+ module Kernel
2110
+ private
2111
+
2112
+ # If _object_ is string-like, parse the string and return the parsed result as
2113
+ # a Ruby data structure. Otherwise, generate a JSON text from the Ruby data
2114
+ # structure object and return it.
2115
+ #
2116
+ # The _opts_ argument is passed through to generate/parse respectively. See
2117
+ # generate and parse for their documentation.
2118
+ #
2119
+ # source://json//lib/json/common.rb#1175
2120
+ def JSON(object, opts = T.unsafe(nil)); end
2121
+
2122
+ # Outputs _objs_ to STDOUT as JSON strings in the shortest form, that is in
2123
+ # one line.
2124
+ #
2125
+ # source://json//lib/json/common.rb#1141
2126
+ def j(*objs); end
2127
+
2128
+ # Outputs _objs_ to STDOUT as JSON strings in a pretty format, with
2129
+ # indentation and over many lines.
2130
+ #
2131
+ # source://json//lib/json/common.rb#1156
2132
+ def jj(*objs); end
2133
+ end
2134
+
2135
+ # source://json//lib/json/common.rb#1180
2136
+ class Object < ::BasicObject
2137
+ include ::Kernel
2138
+ include ::PP::ObjectMixin
2139
+ include ::JSON::GeneratorMethods
2140
+ end