json 1.1.5-x86-linux

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of json might be problematic. Click here for more details.

Files changed (117) hide show
  1. data/CHANGES +106 -0
  2. data/GPL +340 -0
  3. data/README +78 -0
  4. data/RUBY +58 -0
  5. data/Rakefile +268 -0
  6. data/TODO +1 -0
  7. data/VERSION +1 -0
  8. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkComparison.log +52 -0
  9. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast-autocorrelation.dat +1000 -0
  10. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast.dat +1001 -0
  11. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty-autocorrelation.dat +900 -0
  12. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty.dat +901 -0
  13. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe-autocorrelation.dat +1000 -0
  14. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe.dat +1001 -0
  15. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt.log +261 -0
  16. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast-autocorrelation.dat +1000 -0
  17. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast.dat +1001 -0
  18. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty-autocorrelation.dat +1000 -0
  19. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty.dat +1001 -0
  20. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe-autocorrelation.dat +1000 -0
  21. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe.dat +1001 -0
  22. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure.log +262 -0
  23. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator-autocorrelation.dat +1000 -0
  24. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator.dat +1001 -0
  25. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails.log +82 -0
  26. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkComparison.log +34 -0
  27. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser-autocorrelation.dat +900 -0
  28. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser.dat +901 -0
  29. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt.log +81 -0
  30. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser-autocorrelation.dat +1000 -0
  31. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser.dat +1001 -0
  32. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure.log +82 -0
  33. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser-autocorrelation.dat +1000 -0
  34. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser.dat +1001 -0
  35. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails.log +82 -0
  36. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser-autocorrelation.dat +1000 -0
  37. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser.dat +1001 -0
  38. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML.log +82 -0
  39. data/benchmarks/generator_benchmark.rb +165 -0
  40. data/benchmarks/parser_benchmark.rb +197 -0
  41. data/bin/edit_json.rb +9 -0
  42. data/bin/prettify_json.rb +75 -0
  43. data/data/example.json +1 -0
  44. data/data/index.html +38 -0
  45. data/data/prototype.js +4184 -0
  46. data/doc-templates/main.txt +283 -0
  47. data/ext/json/ext/generator/extconf.rb +11 -0
  48. data/ext/json/ext/generator/generator.c +919 -0
  49. data/ext/json/ext/generator/unicode.c +182 -0
  50. data/ext/json/ext/generator/unicode.h +53 -0
  51. data/ext/json/ext/parser/extconf.rb +11 -0
  52. data/ext/json/ext/parser/parser.c +1829 -0
  53. data/ext/json/ext/parser/parser.rl +686 -0
  54. data/ext/json/ext/parser/unicode.c +154 -0
  55. data/ext/json/ext/parser/unicode.h +58 -0
  56. data/install.rb +26 -0
  57. data/lib/json.rb +10 -0
  58. data/lib/json/Array.xpm +21 -0
  59. data/lib/json/FalseClass.xpm +21 -0
  60. data/lib/json/Hash.xpm +21 -0
  61. data/lib/json/Key.xpm +73 -0
  62. data/lib/json/NilClass.xpm +21 -0
  63. data/lib/json/Numeric.xpm +28 -0
  64. data/lib/json/String.xpm +96 -0
  65. data/lib/json/TrueClass.xpm +21 -0
  66. data/lib/json/add/core.rb +135 -0
  67. data/lib/json/add/rails.rb +58 -0
  68. data/lib/json/common.rb +354 -0
  69. data/lib/json/editor.rb +1371 -0
  70. data/lib/json/ext.rb +15 -0
  71. data/lib/json/ext/generator.so +0 -0
  72. data/lib/json/ext/parser.so +0 -0
  73. data/lib/json/json.xpm +1499 -0
  74. data/lib/json/pure.rb +77 -0
  75. data/lib/json/pure/generator.rb +430 -0
  76. data/lib/json/pure/parser.rb +267 -0
  77. data/lib/json/version.rb +8 -0
  78. data/tests/fixtures/fail1.json +1 -0
  79. data/tests/fixtures/fail10.json +1 -0
  80. data/tests/fixtures/fail11.json +1 -0
  81. data/tests/fixtures/fail12.json +1 -0
  82. data/tests/fixtures/fail13.json +1 -0
  83. data/tests/fixtures/fail14.json +1 -0
  84. data/tests/fixtures/fail18.json +1 -0
  85. data/tests/fixtures/fail19.json +1 -0
  86. data/tests/fixtures/fail2.json +1 -0
  87. data/tests/fixtures/fail20.json +1 -0
  88. data/tests/fixtures/fail21.json +1 -0
  89. data/tests/fixtures/fail22.json +1 -0
  90. data/tests/fixtures/fail23.json +1 -0
  91. data/tests/fixtures/fail24.json +1 -0
  92. data/tests/fixtures/fail25.json +1 -0
  93. data/tests/fixtures/fail27.json +2 -0
  94. data/tests/fixtures/fail28.json +2 -0
  95. data/tests/fixtures/fail3.json +1 -0
  96. data/tests/fixtures/fail4.json +1 -0
  97. data/tests/fixtures/fail5.json +1 -0
  98. data/tests/fixtures/fail6.json +1 -0
  99. data/tests/fixtures/fail7.json +1 -0
  100. data/tests/fixtures/fail8.json +1 -0
  101. data/tests/fixtures/fail9.json +1 -0
  102. data/tests/fixtures/pass1.json +56 -0
  103. data/tests/fixtures/pass15.json +1 -0
  104. data/tests/fixtures/pass16.json +1 -0
  105. data/tests/fixtures/pass17.json +1 -0
  106. data/tests/fixtures/pass2.json +1 -0
  107. data/tests/fixtures/pass26.json +1 -0
  108. data/tests/fixtures/pass3.json +6 -0
  109. data/tests/test_json.rb +312 -0
  110. data/tests/test_json_addition.rb +164 -0
  111. data/tests/test_json_fixtures.rb +34 -0
  112. data/tests/test_json_generate.rb +106 -0
  113. data/tests/test_json_rails.rb +146 -0
  114. data/tests/test_json_unicode.rb +62 -0
  115. data/tools/fuzz.rb +139 -0
  116. data/tools/server.rb +61 -0
  117. metadata +200 -0
@@ -0,0 +1,21 @@
1
+ /* XPM */
2
+ static char * TrueClass_xpm[] = {
3
+ "16 16 2 1",
4
+ " c None",
5
+ ". c #0BF311",
6
+ " ",
7
+ " ",
8
+ " ",
9
+ " ......... ",
10
+ " . ",
11
+ " . ",
12
+ " . ",
13
+ " . ",
14
+ " . ",
15
+ " . ",
16
+ " . ",
17
+ " . ",
18
+ " . ",
19
+ " ",
20
+ " ",
21
+ " "};
@@ -0,0 +1,135 @@
1
+ # This file contains implementations of ruby core's custom objects for
2
+ # serialisation/deserialisation.
3
+
4
+ unless Object.const_defined?(:JSON) and ::JSON.const_defined?(:JSON_LOADED) and
5
+ ::JSON::JSON_LOADED
6
+ require 'json'
7
+ end
8
+ require 'date'
9
+
10
+ class Time
11
+ def self.json_create(object)
12
+ if usec = object.delete('u') # used to be tv_usec -> tv_nsec
13
+ object['n'] = usec * 1000
14
+ end
15
+ if respond_to?(:tv_nsec)
16
+ at(*object.values_at('s', 'n'))
17
+ else
18
+ at(object['s'], object['n'] / 1000)
19
+ end
20
+ end
21
+
22
+ def to_json(*args)
23
+ {
24
+ 'json_class' => self.class.name,
25
+ 's' => tv_sec,
26
+ 'n' => respond_to?(:tv_nsec) ? tv_nsec : tv_usec * 1000
27
+ }.to_json(*args)
28
+ end
29
+ end
30
+
31
+ class Date
32
+ def self.json_create(object)
33
+ civil(*object.values_at('y', 'm', 'd', 'sg'))
34
+ end
35
+
36
+ alias start sg unless method_defined?(:start)
37
+
38
+ def to_json(*args)
39
+ {
40
+ 'json_class' => self.class.name,
41
+ 'y' => year,
42
+ 'm' => month,
43
+ 'd' => day,
44
+ 'sg' => start,
45
+ }.to_json(*args)
46
+ end
47
+ end
48
+
49
+ class DateTime
50
+ def self.json_create(object)
51
+ args = object.values_at('y', 'm', 'd', 'H', 'M', 'S')
52
+ of_a, of_b = object['of'].split('/')
53
+ if of_b and of_b != '0'
54
+ args << Rational(of_a.to_i, of_b.to_i)
55
+ else
56
+ args << of_a
57
+ end
58
+ args << object['sg']
59
+ civil(*args)
60
+ end
61
+
62
+ alias start sg unless method_defined?(:start)
63
+
64
+ def to_json(*args)
65
+ {
66
+ 'json_class' => self.class.name,
67
+ 'y' => year,
68
+ 'm' => month,
69
+ 'd' => day,
70
+ 'H' => hour,
71
+ 'M' => min,
72
+ 'S' => sec,
73
+ 'of' => offset.to_s,
74
+ 'sg' => start,
75
+ }.to_json(*args)
76
+ end
77
+ end
78
+
79
+ class Range
80
+ def self.json_create(object)
81
+ new(*object['a'])
82
+ end
83
+
84
+ def to_json(*args)
85
+ {
86
+ 'json_class' => self.class.name,
87
+ 'a' => [ first, last, exclude_end? ]
88
+ }.to_json(*args)
89
+ end
90
+ end
91
+
92
+ class Struct
93
+ def self.json_create(object)
94
+ new(*object['v'])
95
+ end
96
+
97
+ def to_json(*args)
98
+ klass = self.class.name
99
+ klass.to_s.empty? and raise JSON::JSONError, "Only named structs are supported!"
100
+ {
101
+ 'json_class' => klass,
102
+ 'v' => values,
103
+ }.to_json(*args)
104
+ end
105
+ end
106
+
107
+ class Exception
108
+ def self.json_create(object)
109
+ result = new(object['m'])
110
+ result.set_backtrace object['b']
111
+ result
112
+ end
113
+
114
+ def to_json(*args)
115
+ {
116
+ 'json_class' => self.class.name,
117
+ 'm' => message,
118
+ 'b' => backtrace,
119
+ }.to_json(*args)
120
+ end
121
+ end
122
+
123
+ class Regexp
124
+ def self.json_create(object)
125
+ new(object['s'], object['o'])
126
+ end
127
+
128
+ def to_json(*)
129
+ {
130
+ 'json_class' => self.class.name,
131
+ 'o' => options,
132
+ 's' => source,
133
+ }.to_json
134
+ end
135
+ end
@@ -0,0 +1,58 @@
1
+ # This file contains implementations of rails custom objects for
2
+ # serialisation/deserialisation.
3
+
4
+ unless Object.const_defined?(:JSON) and ::JSON.const_defined?(:JSON_LOADED) and
5
+ ::JSON::JSON_LOADED
6
+ require 'json'
7
+ end
8
+
9
+ class Object
10
+ def self.json_create(object)
11
+ obj = new
12
+ for key, value in object
13
+ next if key == 'json_class'
14
+ instance_variable_set "@#{key}", value
15
+ end
16
+ obj
17
+ end
18
+
19
+ def to_json(*a)
20
+ result = {
21
+ 'json_class' => self.class.name
22
+ }
23
+ instance_variables.inject(result) do |r, name|
24
+ r[name[1..-1]] = instance_variable_get name
25
+ r
26
+ end
27
+ result.to_json(*a)
28
+ end
29
+ end
30
+
31
+ class Symbol
32
+ def to_json(*a)
33
+ to_s.to_json(*a)
34
+ end
35
+ end
36
+
37
+ module Enumerable
38
+ def to_json(*a)
39
+ to_a.to_json(*a)
40
+ end
41
+ end
42
+
43
+ # class Regexp
44
+ # def to_json(*)
45
+ # inspect
46
+ # end
47
+ # end
48
+ #
49
+ # The above rails definition has some problems:
50
+ #
51
+ # 1. { 'foo' => /bar/ }.to_json # => "{foo: /bar/}"
52
+ # This isn't valid JSON, because the regular expression syntax is not
53
+ # defined in RFC 4627. (And unquoted strings are disallowed there, too.)
54
+ # Though it is valid Javascript.
55
+ #
56
+ # 2. { 'foo' => /bar/mix }.to_json # => "{foo: /bar/mix}"
57
+ # This isn't even valid Javascript.
58
+
@@ -0,0 +1,354 @@
1
+ require 'json/version'
2
+
3
+ module JSON
4
+ class << self
5
+ # If _object_ is string-like parse the string and return the parsed result
6
+ # as a Ruby data structure. Otherwise generate a JSON text from the Ruby
7
+ # data structure object and return it.
8
+ #
9
+ # The _opts_ argument is passed through to generate/parse respectively, see
10
+ # generate and parse for their documentation.
11
+ def [](object, opts = {})
12
+ if object.respond_to? :to_str
13
+ JSON.parse(object.to_str, opts => {})
14
+ else
15
+ JSON.generate(object, opts => {})
16
+ end
17
+ end
18
+
19
+ # Returns the JSON parser class, that is used by JSON. This might be either
20
+ # JSON::Ext::Parser or JSON::Pure::Parser.
21
+ attr_reader :parser
22
+
23
+ # Set the JSON parser class _parser_ to be used by JSON.
24
+ def parser=(parser) # :nodoc:
25
+ @parser = parser
26
+ remove_const :Parser if const_defined? :Parser
27
+ const_set :Parser, parser
28
+ end
29
+
30
+ # Return the constant located at _path_. The format of _path_ has to be
31
+ # either ::A::B::C or A::B::C. In any case A has to be located at the top
32
+ # level (absolute namespace path?). If there doesn't exist a constant at
33
+ # the given path, an ArgumentError is raised.
34
+ def deep_const_get(path) # :nodoc:
35
+ path = path.to_s
36
+ path.split(/::/).inject(Object) do |p, c|
37
+ case
38
+ when c.empty? then p
39
+ when p.const_defined?(c) then p.const_get(c)
40
+ else raise ArgumentError, "can't find const #{path}"
41
+ end
42
+ end
43
+ end
44
+
45
+ # Set the module _generator_ to be used by JSON.
46
+ def generator=(generator) # :nodoc:
47
+ @generator = generator
48
+ generator_methods = generator::GeneratorMethods
49
+ for const in generator_methods.constants
50
+ klass = deep_const_get(const)
51
+ modul = generator_methods.const_get(const)
52
+ klass.class_eval do
53
+ instance_methods(false).each do |m|
54
+ m.to_s == 'to_json' and remove_method m
55
+ end
56
+ include modul
57
+ end
58
+ end
59
+ self.state = generator::State
60
+ const_set :State, self.state
61
+ end
62
+
63
+ # Returns the JSON generator modul, that is used by JSON. This might be
64
+ # either JSON::Ext::Generator or JSON::Pure::Generator.
65
+ attr_reader :generator
66
+
67
+ # Returns the JSON generator state class, that is used by JSON. This might
68
+ # be either JSON::Ext::Generator::State or JSON::Pure::Generator::State.
69
+ attr_accessor :state
70
+
71
+ # This is create identifier, that is used to decide, if the _json_create_
72
+ # hook of a class should be called. It defaults to 'json_class'.
73
+ attr_accessor :create_id
74
+ end
75
+ self.create_id = 'json_class'
76
+
77
+ NaN = (-1.0) ** 0.5
78
+
79
+ Infinity = 1.0/0
80
+
81
+ MinusInfinity = -Infinity
82
+
83
+ # The base exception for JSON errors.
84
+ class JSONError < StandardError; end
85
+
86
+ # This exception is raised, if a parser error occurs.
87
+ class ParserError < JSONError; end
88
+
89
+ # This exception is raised, if the nesting of parsed datastructures is too
90
+ # deep.
91
+ class NestingError < ParserError; end
92
+
93
+ # This exception is raised, if a generator or unparser error occurs.
94
+ class GeneratorError < JSONError; end
95
+ # For backwards compatibility
96
+ UnparserError = GeneratorError
97
+
98
+ # If a circular data structure is encountered while unparsing
99
+ # this exception is raised.
100
+ class CircularDatastructure < GeneratorError; end
101
+
102
+ # This exception is raised, if the required unicode support is missing on the
103
+ # system. Usually this means, that the iconv library is not installed.
104
+ class MissingUnicodeSupport < JSONError; end
105
+
106
+ module_function
107
+
108
+ # Parse the JSON string _source_ into a Ruby data structure and return it.
109
+ #
110
+ # _opts_ can have the following
111
+ # keys:
112
+ # * *max_nesting*: The maximum depth of nesting allowed in the parsed data
113
+ # structures. Disable depth checking with :max_nesting => false, it defaults
114
+ # to 19.
115
+ # * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
116
+ # defiance of RFC 4627 to be parsed by the Parser. This option defaults
117
+ # to false.
118
+ # * *create_additions*: If set to false, the Parser doesn't create
119
+ # additions even if a matchin class and create_id was found. This option
120
+ # defaults to true.
121
+ def parse(source, opts = {})
122
+ JSON.parser.new(source, opts).parse
123
+ end
124
+
125
+ # Parse the JSON string _source_ into a Ruby data structure and return it.
126
+ # The bang version of the parse method, defaults to the more dangerous values
127
+ # for the _opts_ hash, so be sure only to parse trusted _source_ strings.
128
+ #
129
+ # _opts_ can have the following keys:
130
+ # * *max_nesting*: The maximum depth of nesting allowed in the parsed data
131
+ # structures. Enable depth checking with :max_nesting => anInteger. The parse!
132
+ # methods defaults to not doing max depth checking: This can be dangerous,
133
+ # if someone wants to fill up your stack.
134
+ # * *allow_nan*: If set to true, allow NaN, Infinity, and -Infinity in
135
+ # defiance of RFC 4627 to be parsed by the Parser. This option defaults
136
+ # to true.
137
+ # * *create_additions*: If set to false, the Parser doesn't create
138
+ # additions even if a matchin class and create_id was found. This option
139
+ # defaults to true.
140
+ def parse!(source, opts = {})
141
+ opts = {
142
+ :max_nesting => false,
143
+ :allow_nan => true
144
+ }.update(opts)
145
+ JSON.parser.new(source, opts).parse
146
+ end
147
+
148
+ # Unparse the Ruby data structure _obj_ into a single line JSON string and
149
+ # return it. _state_ is
150
+ # * a JSON::State object,
151
+ # * or a Hash like object (responding to to_hash),
152
+ # * an object convertible into a hash by a to_h method,
153
+ # that is used as or to configure a State object.
154
+ #
155
+ # It defaults to a state object, that creates the shortest possible JSON text
156
+ # in one line, checks for circular data structures and doesn't allow NaN,
157
+ # Infinity, and -Infinity.
158
+ #
159
+ # A _state_ hash can have the following keys:
160
+ # * *indent*: a string used to indent levels (default: ''),
161
+ # * *space*: a string that is put after, a : or , delimiter (default: ''),
162
+ # * *space_before*: a string that is put before a : pair delimiter (default: ''),
163
+ # * *object_nl*: a string that is put at the end of a JSON object (default: ''),
164
+ # * *array_nl*: a string that is put at the end of a JSON array (default: ''),
165
+ # * *check_circular*: true if checking for circular data structures
166
+ # should be done (the default), false otherwise.
167
+ # * *allow_nan*: true if NaN, Infinity, and -Infinity should be
168
+ # generated, otherwise an exception is thrown, if these values are
169
+ # encountered. This options defaults to false.
170
+ # * *max_nesting*: The maximum depth of nesting allowed in the data
171
+ # structures from which JSON is to be generated. Disable depth checking
172
+ # with :max_nesting => false, it defaults to 19.
173
+ #
174
+ # See also the fast_generate for the fastest creation method with the least
175
+ # amount of sanity checks, and the pretty_generate method for some
176
+ # defaults for a pretty output.
177
+ def generate(obj, state = nil)
178
+ if state
179
+ state = State.from_state(state)
180
+ else
181
+ state = State.new
182
+ end
183
+ obj.to_json(state)
184
+ end
185
+
186
+ # :stopdoc:
187
+ # I want to deprecate these later, so I'll first be silent about them, and
188
+ # later delete them.
189
+ alias unparse generate
190
+ module_function :unparse
191
+ # :startdoc:
192
+
193
+ # Unparse the Ruby data structure _obj_ into a single line JSON string and
194
+ # return it. This method disables the checks for circles in Ruby objects, and
195
+ # also generates NaN, Infinity, and, -Infinity float values.
196
+ #
197
+ # *WARNING*: Be careful not to pass any Ruby data structures with circles as
198
+ # _obj_ argument, because this will cause JSON to go into an infinite loop.
199
+ def fast_generate(obj)
200
+ obj.to_json(nil)
201
+ end
202
+
203
+ # :stopdoc:
204
+ # I want to deprecate these later, so I'll first be silent about them, and later delete them.
205
+ alias fast_unparse fast_generate
206
+ module_function :fast_unparse
207
+ # :startdoc:
208
+
209
+ # Unparse the Ruby data structure _obj_ into a JSON string and return it. The
210
+ # returned string is a prettier form of the string returned by #unparse.
211
+ #
212
+ # The _opts_ argument can be used to configure the generator, see the
213
+ # generate method for a more detailed explanation.
214
+ def pretty_generate(obj, opts = nil)
215
+ state = JSON.state.new(
216
+ :indent => ' ',
217
+ :space => ' ',
218
+ :object_nl => "\n",
219
+ :array_nl => "\n",
220
+ :check_circular => true
221
+ )
222
+ if opts
223
+ if opts.respond_to? :to_hash
224
+ opts = opts.to_hash
225
+ elsif opts.respond_to? :to_h
226
+ opts = opts.to_h
227
+ else
228
+ raise TypeError, "can't convert #{opts.class} into Hash"
229
+ end
230
+ state.configure(opts)
231
+ end
232
+ obj.to_json(state)
233
+ end
234
+
235
+ # :stopdoc:
236
+ # I want to deprecate these later, so I'll first be silent about them, and later delete them.
237
+ alias pretty_unparse pretty_generate
238
+ module_function :pretty_unparse
239
+ # :startdoc:
240
+
241
+ # Load a ruby data structure from a JSON _source_ and return it. A source can
242
+ # either be a string-like object, an IO like object, or an object responding
243
+ # to the read method. If _proc_ was given, it will be called with any nested
244
+ # Ruby object as an argument recursively in depth first order.
245
+ #
246
+ # This method is part of the implementation of the load/dump interface of
247
+ # Marshal and YAML.
248
+ def load(source, proc = nil)
249
+ if source.respond_to? :to_str
250
+ source = source.to_str
251
+ elsif source.respond_to? :to_io
252
+ source = source.to_io.read
253
+ else
254
+ source = source.read
255
+ end
256
+ result = parse(source, :max_nesting => false, :allow_nan => true)
257
+ recurse_proc(result, &proc) if proc
258
+ result
259
+ end
260
+
261
+ def recurse_proc(result, &proc)
262
+ case result
263
+ when Array
264
+ result.each { |x| recurse_proc x, &proc }
265
+ proc.call result
266
+ when Hash
267
+ result.each { |x, y| recurse_proc x, &proc; recurse_proc y, &proc }
268
+ proc.call result
269
+ else
270
+ proc.call result
271
+ end
272
+ end
273
+ private :recurse_proc
274
+ module_function :recurse_proc
275
+
276
+ alias restore load
277
+ module_function :restore
278
+
279
+ # Dumps _obj_ as a JSON string, i.e. calls generate on the object and returns
280
+ # the result.
281
+ #
282
+ # If anIO (an IO like object or an object that responds to the write method)
283
+ # was given, the resulting JSON is written to it.
284
+ #
285
+ # If the number of nested arrays or objects exceeds _limit_ an ArgumentError
286
+ # exception is raised. This argument is similar (but not exactly the
287
+ # same!) to the _limit_ argument in Marshal.dump.
288
+ #
289
+ # This method is part of the implementation of the load/dump interface of
290
+ # Marshal and YAML.
291
+ def dump(obj, anIO = nil, limit = nil)
292
+ if anIO and limit.nil?
293
+ anIO = anIO.to_io if anIO.respond_to?(:to_io)
294
+ unless anIO.respond_to?(:write)
295
+ limit = anIO
296
+ anIO = nil
297
+ end
298
+ end
299
+ limit ||= 0
300
+ result = generate(obj, :allow_nan => true, :max_nesting => limit)
301
+ if anIO
302
+ anIO.write result
303
+ anIO
304
+ else
305
+ result
306
+ end
307
+ rescue JSON::NestingError
308
+ raise ArgumentError, "exceed depth limit"
309
+ end
310
+ end
311
+
312
+ module ::Kernel
313
+ # Outputs _objs_ to STDOUT as JSON strings in the shortest form, that is in
314
+ # one line.
315
+ def j(*objs)
316
+ objs.each do |obj|
317
+ puts JSON::generate(obj, :allow_nan => true, :max_nesting => false)
318
+ end
319
+ nil
320
+ end
321
+
322
+ # Ouputs _objs_ to STDOUT as JSON strings in a pretty format, with
323
+ # indentation and over many lines.
324
+ def jj(*objs)
325
+ objs.each do |obj|
326
+ puts JSON::pretty_generate(obj, :allow_nan => true, :max_nesting => false)
327
+ end
328
+ nil
329
+ end
330
+
331
+ # If _object_ is string-like parse the string and return the parsed result as
332
+ # a Ruby data structure. Otherwise generate a JSON text from the Ruby data
333
+ # structure object and return it.
334
+ #
335
+ # The _opts_ argument is passed through to generate/parse respectively, see
336
+ # generate and parse for their documentation.
337
+ def JSON(object, opts = {})
338
+ if object.respond_to? :to_str
339
+ JSON.parse(object.to_str, opts)
340
+ else
341
+ JSON.generate(object, opts)
342
+ end
343
+ end
344
+ end
345
+
346
+ class ::Class
347
+ # Returns true, if this class can be used to create an instance
348
+ # from a serialised JSON string. The class has to implement a class
349
+ # method _json_create_ that expects a hash as first parameter, which includes
350
+ # the required data.
351
+ def json_creatable?
352
+ respond_to?(:json_create)
353
+ end
354
+ end