ghi 0.3.1 → 0.9.0.dev

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,295 @@
1
+ module GHI
2
+ module Formatting
3
+ module Colors
4
+ class << self
5
+ attr_accessor :colorize
6
+ def colorize?
7
+ return @colorize if defined? @colorize
8
+ @colorize = STDOUT.tty?
9
+ end
10
+ end
11
+
12
+ def colorize?
13
+ Colors.colorize?
14
+ end
15
+
16
+ def fg color, &block
17
+ escape color, 3, &block
18
+ end
19
+
20
+ def bg color, &block
21
+ fg(offset(color)) { escape color, 4, &block }
22
+ end
23
+
24
+ def bright &block
25
+ escape :bright, &block
26
+ end
27
+
28
+ def underline &block
29
+ escape :underline, &block
30
+ end
31
+
32
+ def blink &block
33
+ escape :blink, &block
34
+ end
35
+
36
+ def inverse &block
37
+ escape :inverse, &block
38
+ end
39
+
40
+ def no_color
41
+ old_colorize, Colors.colorize = colorize?, false
42
+ yield
43
+ ensure
44
+ Colors.colorize = old_colorize
45
+ end
46
+
47
+ def to_hex string
48
+ WEB[string] || string.downcase.sub(/^(#|0x)/, '').
49
+ sub(/^([0-f])([0-f])([0-f])$/, '\1\1\2\2\3\3')
50
+ end
51
+
52
+ ANSI = {
53
+ :bright => 1,
54
+ :underline => 4,
55
+ :blink => 5,
56
+ :inverse => 7,
57
+
58
+ :black => 0,
59
+ :red => 1,
60
+ :green => 2,
61
+ :yellow => 3,
62
+ :blue => 4,
63
+ :magenta => 5,
64
+ :cyan => 6,
65
+ :white => 7
66
+ }
67
+
68
+ WEB = {
69
+ 'aliceblue' => 'f0f8ff',
70
+ 'antiquewhite' => 'faebd7',
71
+ 'aqua' => '00ffff',
72
+ 'aquamarine' => '7fffd4',
73
+ 'azure' => 'f0ffff',
74
+ 'beige' => 'f5f5dc',
75
+ 'bisque' => 'ffe4c4',
76
+ 'black' => '000000',
77
+ 'blanchedalmond' => 'ffebcd',
78
+ 'blue' => '0000ff',
79
+ 'blueviolet' => '8a2be2',
80
+ 'brown' => 'a52a2a',
81
+ 'burlywood' => 'deb887',
82
+ 'cadetblue' => '5f9ea0',
83
+ 'chartreuse' => '7fff00',
84
+ 'chocolate' => 'd2691e',
85
+ 'coral' => 'ff7f50',
86
+ 'cornflowerblue' => '6495ed',
87
+ 'cornsilk' => 'fff8dc',
88
+ 'crimson' => 'dc143c',
89
+ 'cyan' => '00ffff',
90
+ 'darkblue' => '00008b',
91
+ 'darkcyan' => '008b8b',
92
+ 'darkgoldenrod' => 'b8860b',
93
+ 'darkgray' => 'a9a9a9',
94
+ 'darkgrey' => 'a9a9a9',
95
+ 'darkgreen' => '006400',
96
+ 'darkkhaki' => 'bdb76b',
97
+ 'darkmagenta' => '8b008b',
98
+ 'darkolivegreen' => '556b2f',
99
+ 'darkorange' => 'ff8c00',
100
+ 'darkorchid' => '9932cc',
101
+ 'darkred' => '8b0000',
102
+ 'darksalmon' => 'e9967a',
103
+ 'darkseagreen' => '8fbc8f',
104
+ 'darkslateblue' => '483d8b',
105
+ 'darkslategray' => '2f4f4f',
106
+ 'darkslategrey' => '2f4f4f',
107
+ 'darkturquoise' => '00ced1',
108
+ 'darkviolet' => '9400d3',
109
+ 'deeppink' => 'ff1493',
110
+ 'deepskyblue' => '00bfff',
111
+ 'dimgray' => '696969',
112
+ 'dimgrey' => '696969',
113
+ 'dodgerblue' => '1e90ff',
114
+ 'firebrick' => 'b22222',
115
+ 'floralwhite' => 'fffaf0',
116
+ 'forestgreen' => '228b22',
117
+ 'fuchsia' => 'ff00ff',
118
+ 'gainsboro' => 'dcdcdc',
119
+ 'ghostwhite' => 'f8f8ff',
120
+ 'gold' => 'ffd700',
121
+ 'goldenrod' => 'daa520',
122
+ 'gray' => '808080',
123
+ 'green' => '008000',
124
+ 'greenyellow' => 'adff2f',
125
+ 'honeydew' => 'f0fff0',
126
+ 'hotpink' => 'ff69b4',
127
+ 'indianred' => 'cd5c5c',
128
+ 'indigo' => '4b0082',
129
+ 'ivory' => 'fffff0',
130
+ 'khaki' => 'f0e68c',
131
+ 'lavender' => 'e6e6fa',
132
+ 'lavenderblush' => 'fff0f5',
133
+ 'lawngreen' => '7cfc00',
134
+ 'lemonchiffon' => 'fffacd',
135
+ 'lightblue' => 'add8e6',
136
+ 'lightcoral' => 'f08080',
137
+ 'lightcyan' => 'e0ffff',
138
+ 'lightgoldenrodyellow' => 'fafad2',
139
+ 'lightgreen' => '90ee90',
140
+ 'lightgray' => 'd3d3d3',
141
+ 'lightgrey' => 'd3d3d3',
142
+ 'lightpink' => 'ffb6c1',
143
+ 'lightsalmon' => 'ffa07a',
144
+ 'lightseagreen' => '20b2aa',
145
+ 'lightskyblue' => '87cefa',
146
+ 'lightslategray' => '778899',
147
+ 'lightslategrey' => '778899',
148
+ 'lightsteelblue' => 'b0c4de',
149
+ 'lightyellow' => 'ffffe0',
150
+ 'lime' => '00ff00',
151
+ 'limegreen' => '32cd32',
152
+ 'linen' => 'faf0e6',
153
+ 'magenta' => 'ff00ff',
154
+ 'maroon' => '800000',
155
+ 'mediumaquamarine' => '66cdaa',
156
+ 'mediumblue' => '0000cd',
157
+ 'mediumorchid' => 'ba55d3',
158
+ 'mediumpurple' => '9370db',
159
+ 'mediumseagreen' => '3cb371',
160
+ 'mediumslateblue' => '7b68ee',
161
+ 'mediumspringgreen' => '00fa9a',
162
+ 'mediumturquoise' => '48d1cc',
163
+ 'mediumvioletred' => 'c71585',
164
+ 'midnightblue' => '191970',
165
+ 'mintcream' => 'f5fffa',
166
+ 'mistyrose' => 'ffe4e1',
167
+ 'moccasin' => 'ffe4b5',
168
+ 'navajowhite' => 'ffdead',
169
+ 'navy' => '000080',
170
+ 'oldlace' => 'fdf5e6',
171
+ 'olive' => '808000',
172
+ 'olivedrab' => '6b8e23',
173
+ 'orange' => 'ffa500',
174
+ 'orangered' => 'ff4500',
175
+ 'orchid' => 'da70d6',
176
+ 'palegoldenrod' => 'eee8aa',
177
+ 'palegreen' => '98fb98',
178
+ 'paleturquoise' => 'afeeee',
179
+ 'palevioletred' => 'db7093',
180
+ 'papayawhip' => 'ffefd5',
181
+ 'peachpuff' => 'ffdab9',
182
+ 'peru' => 'cd853f',
183
+ 'pink' => 'ffc0cb',
184
+ 'plum' => 'dda0dd',
185
+ 'powderblue' => 'b0e0e6',
186
+ 'purple' => '800080',
187
+ 'red' => 'ff0000',
188
+ 'rosybrown' => 'bc8f8f',
189
+ 'royalblue' => '4169e1',
190
+ 'saddlebrown' => '8b4513',
191
+ 'salmon' => 'fa8072',
192
+ 'sandybrown' => 'f4a460',
193
+ 'seagreen' => '2e8b57',
194
+ 'seashell' => 'fff5ee',
195
+ 'sienna' => 'a0522d',
196
+ 'silver' => 'c0c0c0',
197
+ 'skyblue' => '87ceeb',
198
+ 'slateblue' => '6a5acd',
199
+ 'slategray' => '708090',
200
+ 'slategrey' => '708090',
201
+ 'snow' => 'fffafa',
202
+ 'springgreen' => '00ff7f',
203
+ 'steelblue' => '4682b4',
204
+ 'tan' => 'd2b48c',
205
+ 'teal' => '008080',
206
+ 'thistle' => 'd8bfd8',
207
+ 'tomato' => 'ff6347',
208
+ 'turquoise' => '40e0d0',
209
+ 'violet' => 'ee82ee',
210
+ 'wheat' => 'f5deb3',
211
+ 'white' => 'ffffff',
212
+ 'whitesmoke' => 'f5f5f5',
213
+ 'yellow' => 'ffff00',
214
+ 'yellowgreen' => '9acd32'
215
+ }
216
+
217
+ private
218
+
219
+ def escape color = :black, layer = nil
220
+ return yield unless color && colorize?
221
+ previous_escape = Thread.current[:escape] || "\e[0m"
222
+ escape = Thread.current[:escape] = "\e[%s%sm" % [
223
+ layer, ANSI[color] || "8;5;#{to_256(*to_rgb(color))}"
224
+ ]
225
+ [escape, yield, previous_escape].join
226
+ ensure
227
+ Thread.current[:escape] = previous_escape
228
+ end
229
+
230
+ def to_256 r, g, b
231
+ r, g, b = [r, g, b].map { |c| c / 10 }
232
+ return 232 + g if r == g && g == b && g != 0 && g != 25
233
+ 16 + ((r / 5) * 36) + ((g / 5) * 6) + (b / 5)
234
+ end
235
+
236
+ def to_rgb hex
237
+ n = (WEB[hex.to_s] || hex).to_i(16)
238
+ [2, 1, 0].map { |m| n >> (m << 3) & 0xff }
239
+ end
240
+
241
+ def offset hex
242
+ h, s, l = rgb_to_hsl(to_rgb(WEB[hex.to_s] || hex))
243
+ l < 55 && !(40..80).include?(h) ? l *= 1.75 : l /= 2
244
+ hsl_to_rgb([h, s, l]).map { |c| '%02x' % c }.join
245
+ end
246
+
247
+ def rgb_to_hsl rgb
248
+ r, g, b = rgb.map { |c| c / 255.0 }
249
+ max = [r, g, b].max
250
+ min = [r, g, b].min
251
+ d = max - min
252
+ h = case max
253
+ when min then 0
254
+ when r then 60 * (g - b) / d
255
+ when g then 60 * (b - r) / d + 120
256
+ when b then 60 * (r - g) / d + 240
257
+ end
258
+ l = (max + min) / 2.0
259
+ s = if max == min then 0
260
+ elsif l < 0.5 then d / (2 * l)
261
+ else d / (2 - 2 * l)
262
+ end
263
+ [h % 360, s * 100, l * 100]
264
+ end
265
+
266
+ def hsl_to_rgb hsl
267
+ h, s, l = hsl
268
+ h /= 360.0
269
+ s /= 100.0
270
+ l /= 100.0
271
+ m2 = l <= 0.5 ? l * (s + 1) : l + s - l * s
272
+ m1 = l * 2 - m2
273
+ rgb = [[m1, m2, h + 1.0 / 3], [m1, m2, h], [m1, m2, h - 1.0 / 3]]
274
+ rgb.map { |c|
275
+ m1, m2, h = c
276
+ h += 1 if h < 0
277
+ h -= 1 if h > 1
278
+ next m1 + (m2 - m1) * h * 6 if h * 6 < 1
279
+ next m2 if h * 2 < 1
280
+ next m1 + (m2 - m1) * (2.0/3 - h) * 6 if h * 3 < 2
281
+ m1
282
+ }.map { |c| c * 255 }
283
+ end
284
+
285
+ def hue_to_rgb m1, m2, h
286
+ h += 1 if h < 0
287
+ h -= 1 if h > 1
288
+ return m1 + (m2 - m1) * h * 6 if h * 6 < 1
289
+ return m2 if h * 2 < 1
290
+ return m1 + (m2 - m1) * (2.0/3 - h) * 6 if h * 3 < 2
291
+ return m1
292
+ end
293
+ end
294
+ end
295
+ end
data/lib/ghi/json.rb ADDED
@@ -0,0 +1,1304 @@
1
+ require 'strscan'
2
+
3
+ module JSON
4
+ module Pure
5
+ # This class implements the JSON parser that is used to parse a JSON string
6
+ # into a Ruby data structure.
7
+ class Parser < StringScanner
8
+ STRING = /" ((?:[^\x0-\x1f"\\] |
9
+ # escaped special characters:
10
+ \\["\\\/bfnrt] |
11
+ \\u[0-9a-fA-F]{4} |
12
+ # match all but escaped special characters:
13
+ \\[\x20-\x21\x23-\x2e\x30-\x5b\x5d-\x61\x63-\x65\x67-\x6d\x6f-\x71\x73\x75-\xff])*)
14
+ "/nx
15
+ INTEGER = /(-?0|-?[1-9]\d*)/
16
+ FLOAT = /(-?
17
+ (?:0|[1-9]\d*)
18
+ (?:
19
+ \.\d+(?i:e[+-]?\d+) |
20
+ \.\d+ |
21
+ (?i:e[+-]?\d+)
22
+ )
23
+ )/x
24
+ NAN = /NaN/
25
+ INFINITY = /Infinity/
26
+ MINUS_INFINITY = /-Infinity/
27
+ OBJECT_OPEN = /\{/
28
+ OBJECT_CLOSE = /\}/
29
+ ARRAY_OPEN = /\[/
30
+ ARRAY_CLOSE = /\]/
31
+ PAIR_DELIMITER = /:/
32
+ COLLECTION_DELIMITER = /,/
33
+ TRUE = /true/
34
+ FALSE = /false/
35
+ NULL = /null/
36
+ IGNORE = %r(
37
+ (?:
38
+ //[^\n\r]*[\n\r]| # line comments
39
+ /\* # c-style comments
40
+ (?:
41
+ [^*/]| # normal chars
42
+ /[^*]| # slashes that do not start a nested comment
43
+ \*[^/]| # asterisks that do not end this comment
44
+ /(?=\*/) # single slash before this comment's end
45
+ )*
46
+ \*/ # the End of this comment
47
+ |[ \t\r\n]+ # whitespaces: space, horicontal tab, lf, cr
48
+ )+
49
+ )mx
50
+
51
+ UNPARSED = Object.new
52
+
53
+ # Creates a new JSON::Pure::Parser instance for the string _source_.
54
+ #
55
+ # It will be configured by the _opts_ hash. _opts_ can have the following
56
+ # keys:
57
+ # * *max_nesting*: The maximum depth of nesting allowed in the parsed data
58
+ # structures. Disable depth checking with :max_nesting => false|nil|0,
59
+ # it defaults to 19.
60
+ # * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
61
+ # defiance of RFC 4627 to be parsed by the Parser. This option defaults
62
+ # to false.
63
+ # * *symbolize_names*: If set to true, returns symbols for the names
64
+ # (keys) in a JSON object. Otherwise strings are returned, which is also
65
+ # the default.
66
+ # * *create_additions*: If set to false, the Parser doesn't create
67
+ # additions even if a matchin class and create_id was found. This option
68
+ # defaults to true.
69
+ # * *object_class*: Defaults to Hash
70
+ # * *array_class*: Defaults to Array
71
+ # * *quirks_mode*: Enables quirks_mode for parser, that is for example
72
+ # parsing single JSON values instead of documents is possible.
73
+ def initialize(source, opts = {})
74
+ opts ||= {}
75
+ unless @quirks_mode = opts[:quirks_mode]
76
+ source = convert_encoding source
77
+ end
78
+ super source
79
+ if !opts.key?(:max_nesting) # defaults to 19
80
+ @max_nesting = 19
81
+ elsif opts[:max_nesting]
82
+ @max_nesting = opts[:max_nesting]
83
+ else
84
+ @max_nesting = 0
85
+ end
86
+ @allow_nan = !!opts[:allow_nan]
87
+ @symbolize_names = !!opts[:symbolize_names]
88
+ if opts.key?(:create_additions)
89
+ @create_additions = !!opts[:create_additions]
90
+ else
91
+ @create_additions = true
92
+ end
93
+ @create_id = @create_additions ? JSON.create_id : nil
94
+ @object_class = opts[:object_class] || Hash
95
+ @array_class = opts[:array_class] || Array
96
+ @match_string = opts[:match_string]
97
+ end
98
+
99
+ alias source string
100
+
101
+ def quirks_mode?
102
+ !!@quirks_mode
103
+ end
104
+
105
+ def reset
106
+ super
107
+ @current_nesting = 0
108
+ end
109
+
110
+ # Parses the current JSON string _source_ and returns the complete data
111
+ # structure as a result.
112
+ def parse
113
+ reset
114
+ obj = nil
115
+ if @quirks_mode
116
+ while !eos? && skip(IGNORE)
117
+ end
118
+ if eos?
119
+ raise ParserError, "source did not contain any JSON!"
120
+ else
121
+ obj = parse_value
122
+ obj == UNPARSED and raise ParserError, "source did not contain any JSON!"
123
+ end
124
+ else
125
+ until eos?
126
+ case
127
+ when scan(OBJECT_OPEN)
128
+ obj and raise ParserError, "source '#{peek(20)}' not in JSON!"
129
+ @current_nesting = 1
130
+ obj = parse_object
131
+ when scan(ARRAY_OPEN)
132
+ obj and raise ParserError, "source '#{peek(20)}' not in JSON!"
133
+ @current_nesting = 1
134
+ obj = parse_array
135
+ when skip(IGNORE)
136
+ ;
137
+ else
138
+ raise ParserError, "source '#{peek(20)}' not in JSON!"
139
+ end
140
+ end
141
+ obj or raise ParserError, "source did not contain any JSON!"
142
+ end
143
+ obj
144
+ end
145
+
146
+ private
147
+
148
+ def convert_encoding(source)
149
+ if source.respond_to?(:to_str)
150
+ source = source.to_str
151
+ else
152
+ raise TypeError, "#{source.inspect} is not like a string"
153
+ end
154
+ if defined?(::Encoding)
155
+ if source.encoding == ::Encoding::ASCII_8BIT
156
+ b = source[0, 4].bytes.to_a
157
+ source =
158
+ case
159
+ when b.size >= 4 && b[0] == 0 && b[1] == 0 && b[2] == 0
160
+ source.dup.force_encoding(::Encoding::UTF_32BE).encode!(::Encoding::UTF_8)
161
+ when b.size >= 4 && b[0] == 0 && b[2] == 0
162
+ source.dup.force_encoding(::Encoding::UTF_16BE).encode!(::Encoding::UTF_8)
163
+ when b.size >= 4 && b[1] == 0 && b[2] == 0 && b[3] == 0
164
+ source.dup.force_encoding(::Encoding::UTF_32LE).encode!(::Encoding::UTF_8)
165
+ when b.size >= 4 && b[1] == 0 && b[3] == 0
166
+ source.dup.force_encoding(::Encoding::UTF_16LE).encode!(::Encoding::UTF_8)
167
+ else
168
+ source.dup
169
+ end
170
+ else
171
+ source = source.encode(::Encoding::UTF_8)
172
+ end
173
+ source.force_encoding(::Encoding::ASCII_8BIT)
174
+ else
175
+ b = source
176
+ source =
177
+ case
178
+ when b.size >= 4 && b[0] == 0 && b[1] == 0 && b[2] == 0
179
+ JSON.iconv('utf-8', 'utf-32be', b)
180
+ when b.size >= 4 && b[0] == 0 && b[2] == 0
181
+ JSON.iconv('utf-8', 'utf-16be', b)
182
+ when b.size >= 4 && b[1] == 0 && b[2] == 0 && b[3] == 0
183
+ JSON.iconv('utf-8', 'utf-32le', b)
184
+ when b.size >= 4 && b[1] == 0 && b[3] == 0
185
+ JSON.iconv('utf-8', 'utf-16le', b)
186
+ else
187
+ b
188
+ end
189
+ end
190
+ source
191
+ end
192
+
193
+ # Unescape characters in strings.
194
+ UNESCAPE_MAP = Hash.new { |h, k| h[k] = k.chr }
195
+ UNESCAPE_MAP.update({
196
+ ?" => '"',
197
+ ?\\ => '\\',
198
+ ?/ => '/',
199
+ ?b => "\b",
200
+ ?f => "\f",
201
+ ?n => "\n",
202
+ ?r => "\r",
203
+ ?t => "\t",
204
+ ?u => nil,
205
+ })
206
+
207
+ EMPTY_8BIT_STRING = ''
208
+ if ::String.method_defined?(:encode)
209
+ EMPTY_8BIT_STRING.force_encoding Encoding::ASCII_8BIT
210
+ end
211
+
212
+ def parse_string
213
+ if scan(STRING)
214
+ return '' if self[1].empty?
215
+ string = self[1].gsub(%r((?:\\[\\bfnrt"/]|(?:\\u(?:[A-Fa-f\d]{4}))+|\\[\x20-\xff]))n) do |c|
216
+ if u = UNESCAPE_MAP[$&[1]]
217
+ u
218
+ else # \uXXXX
219
+ bytes = EMPTY_8BIT_STRING.dup
220
+ i = 0
221
+ while c[6 * i] == ?\\ && c[6 * i + 1] == ?u
222
+ bytes << c[6 * i + 2, 2].to_i(16) << c[6 * i + 4, 2].to_i(16)
223
+ i += 1
224
+ end
225
+ JSON.iconv('utf-8', 'utf-16be', bytes)
226
+ end
227
+ end
228
+ if string.respond_to?(:force_encoding)
229
+ string.force_encoding(::Encoding::UTF_8)
230
+ end
231
+ if @create_additions and @match_string
232
+ for (regexp, klass) in @match_string
233
+ klass.json_creatable? or next
234
+ string =~ regexp and return klass.json_create(string)
235
+ end
236
+ end
237
+ string
238
+ else
239
+ UNPARSED
240
+ end
241
+ rescue => e
242
+ raise ParserError, "Caught #{e.class} at '#{peek(20)}': #{e}"
243
+ end
244
+
245
+ def parse_value
246
+ case
247
+ when scan(FLOAT)
248
+ Float(self[1])
249
+ when scan(INTEGER)
250
+ Integer(self[1])
251
+ when scan(TRUE)
252
+ true
253
+ when scan(FALSE)
254
+ false
255
+ when scan(NULL)
256
+ nil
257
+ when (string = parse_string) != UNPARSED
258
+ string
259
+ when scan(ARRAY_OPEN)
260
+ @current_nesting += 1
261
+ ary = parse_array
262
+ @current_nesting -= 1
263
+ ary
264
+ when scan(OBJECT_OPEN)
265
+ @current_nesting += 1
266
+ obj = parse_object
267
+ @current_nesting -= 1
268
+ obj
269
+ when @allow_nan && scan(NAN)
270
+ NaN
271
+ when @allow_nan && scan(INFINITY)
272
+ Infinity
273
+ when @allow_nan && scan(MINUS_INFINITY)
274
+ MinusInfinity
275
+ else
276
+ UNPARSED
277
+ end
278
+ end
279
+
280
+ def parse_array
281
+ raise NestingError, "nesting of #@current_nesting is too deep" if
282
+ @max_nesting.nonzero? && @current_nesting > @max_nesting
283
+ result = @array_class.new
284
+ delim = false
285
+ until eos?
286
+ case
287
+ when (value = parse_value) != UNPARSED
288
+ delim = false
289
+ result << value
290
+ skip(IGNORE)
291
+ if scan(COLLECTION_DELIMITER)
292
+ delim = true
293
+ elsif match?(ARRAY_CLOSE)
294
+ ;
295
+ else
296
+ raise ParserError, "expected ',' or ']' in array at '#{peek(20)}'!"
297
+ end
298
+ when scan(ARRAY_CLOSE)
299
+ if delim
300
+ raise ParserError, "expected next element in array at '#{peek(20)}'!"
301
+ end
302
+ break
303
+ when skip(IGNORE)
304
+ ;
305
+ else
306
+ raise ParserError, "unexpected token in array at '#{peek(20)}'!"
307
+ end
308
+ end
309
+ result
310
+ end
311
+
312
+ def parse_object
313
+ raise NestingError, "nesting of #@current_nesting is too deep" if
314
+ @max_nesting.nonzero? && @current_nesting > @max_nesting
315
+ result = @object_class.new
316
+ delim = false
317
+ until eos?
318
+ case
319
+ when (string = parse_string) != UNPARSED
320
+ skip(IGNORE)
321
+ unless scan(PAIR_DELIMITER)
322
+ raise ParserError, "expected ':' in object at '#{peek(20)}'!"
323
+ end
324
+ skip(IGNORE)
325
+ unless (value = parse_value).equal? UNPARSED
326
+ result[@symbolize_names ? string.to_sym : string] = value
327
+ delim = false
328
+ skip(IGNORE)
329
+ if scan(COLLECTION_DELIMITER)
330
+ delim = true
331
+ elsif match?(OBJECT_CLOSE)
332
+ ;
333
+ else
334
+ raise ParserError, "expected ',' or '}' in object at '#{peek(20)}'!"
335
+ end
336
+ else
337
+ raise ParserError, "expected value in object at '#{peek(20)}'!"
338
+ end
339
+ when scan(OBJECT_CLOSE)
340
+ if delim
341
+ raise ParserError, "expected next name, value pair in object at '#{peek(20)}'!"
342
+ end
343
+ if @create_additions and klassname = result[@create_id]
344
+ klass = JSON.deep_const_get klassname
345
+ break unless klass and klass.json_creatable?
346
+ result = klass.json_create(result)
347
+ end
348
+ break
349
+ when skip(IGNORE)
350
+ ;
351
+ else
352
+ raise ParserError, "unexpected token in object at '#{peek(20)}'!"
353
+ end
354
+ end
355
+ result
356
+ end
357
+ end
358
+ end
359
+ end
360
+
361
+ module JSON
362
+ MAP = {
363
+ "\x0" => '\u0000',
364
+ "\x1" => '\u0001',
365
+ "\x2" => '\u0002',
366
+ "\x3" => '\u0003',
367
+ "\x4" => '\u0004',
368
+ "\x5" => '\u0005',
369
+ "\x6" => '\u0006',
370
+ "\x7" => '\u0007',
371
+ "\b" => '\b',
372
+ "\t" => '\t',
373
+ "\n" => '\n',
374
+ "\xb" => '\u000b',
375
+ "\f" => '\f',
376
+ "\r" => '\r',
377
+ "\xe" => '\u000e',
378
+ "\xf" => '\u000f',
379
+ "\x10" => '\u0010',
380
+ "\x11" => '\u0011',
381
+ "\x12" => '\u0012',
382
+ "\x13" => '\u0013',
383
+ "\x14" => '\u0014',
384
+ "\x15" => '\u0015',
385
+ "\x16" => '\u0016',
386
+ "\x17" => '\u0017',
387
+ "\x18" => '\u0018',
388
+ "\x19" => '\u0019',
389
+ "\x1a" => '\u001a',
390
+ "\x1b" => '\u001b',
391
+ "\x1c" => '\u001c',
392
+ "\x1d" => '\u001d',
393
+ "\x1e" => '\u001e',
394
+ "\x1f" => '\u001f',
395
+ '"' => '\"',
396
+ '\\' => '\\\\',
397
+ } # :nodoc:
398
+
399
+ # Convert a UTF8 encoded Ruby string _string_ to a JSON string, encoded with
400
+ # UTF16 big endian characters as \u????, and return it.
401
+ if defined?(::Encoding)
402
+ def utf8_to_json(string) # :nodoc:
403
+ string = string.dup
404
+ string << '' # XXX workaround: avoid buffer sharing
405
+ string.force_encoding(::Encoding::ASCII_8BIT)
406
+ string.gsub!(/["\\\x0-\x1f]/) { MAP[$&] }
407
+ string.force_encoding(::Encoding::UTF_8)
408
+ string
409
+ end
410
+
411
+ def utf8_to_json_ascii(string) # :nodoc:
412
+ string = string.dup
413
+ string << '' # XXX workaround: avoid buffer sharing
414
+ string.force_encoding(::Encoding::ASCII_8BIT)
415
+ string.gsub!(/["\\\x0-\x1f]/) { MAP[$&] }
416
+ string.gsub!(/(
417
+ (?:
418
+ [\xc2-\xdf][\x80-\xbf] |
419
+ [\xe0-\xef][\x80-\xbf]{2} |
420
+ [\xf0-\xf4][\x80-\xbf]{3}
421
+ )+ |
422
+ [\x80-\xc1\xf5-\xff] # invalid
423
+ )/nx) { |c|
424
+ c.size == 1 and raise GeneratorError, "invalid utf8 byte: '#{c}'"
425
+ s = JSON.iconv('utf-16be', 'utf-8', c).unpack('H*')[0]
426
+ s.gsub!(/.{4}/n, '\\\\u\&')
427
+ }
428
+ string.force_encoding(::Encoding::UTF_8)
429
+ string
430
+ rescue => e
431
+ raise GeneratorError, "Caught #{e.class}: #{e}"
432
+ end
433
+ else
434
+ def utf8_to_json(string) # :nodoc:
435
+ string.gsub(/["\\\x0-\x1f]/) { MAP[$&] }
436
+ end
437
+
438
+ def utf8_to_json_ascii(string) # :nodoc:
439
+ string = string.gsub(/["\\\x0-\x1f]/) { MAP[$&] }
440
+ string.gsub!(/(
441
+ (?:
442
+ [\xc2-\xdf][\x80-\xbf] |
443
+ [\xe0-\xef][\x80-\xbf]{2} |
444
+ [\xf0-\xf4][\x80-\xbf]{3}
445
+ )+ |
446
+ [\x80-\xc1\xf5-\xff] # invalid
447
+ )/nx) { |c|
448
+ c.size == 1 and raise GeneratorError, "invalid utf8 byte: '#{c}'"
449
+ s = JSON.iconv('utf-16be', 'utf-8', c).unpack('H*')[0]
450
+ s.gsub!(/.{4}/n, '\\\\u\&')
451
+ }
452
+ string
453
+ rescue => e
454
+ raise GeneratorError, "Caught #{e.class}: #{e}"
455
+ end
456
+ end
457
+ module_function :utf8_to_json, :utf8_to_json_ascii
458
+
459
+ module Pure
460
+ module Generator
461
+ # This class is used to create State instances, that are use to hold data
462
+ # while generating a JSON text from a Ruby data structure.
463
+ class State
464
+ # Creates a State object from _opts_, which ought to be Hash to create
465
+ # a new State instance configured by _opts_, something else to create
466
+ # an unconfigured instance. If _opts_ is a State object, it is just
467
+ # returned.
468
+ def self.from_state(opts)
469
+ case
470
+ when self === opts
471
+ opts
472
+ when opts.respond_to?(:to_hash)
473
+ new(opts.to_hash)
474
+ when opts.respond_to?(:to_h)
475
+ new(opts.to_h)
476
+ else
477
+ SAFE_STATE_PROTOTYPE.dup
478
+ end
479
+ end
480
+
481
+ # Instantiates a new State object, configured by _opts_.
482
+ #
483
+ # _opts_ can have the following keys:
484
+ #
485
+ # * *indent*: a string used to indent levels (default: ''),
486
+ # * *space*: a string that is put after, a : or , delimiter (default: ''),
487
+ # * *space_before*: a string that is put before a : pair delimiter (default: ''),
488
+ # * *object_nl*: a string that is put at the end of a JSON object (default: ''),
489
+ # * *array_nl*: a string that is put at the end of a JSON array (default: ''),
490
+ # * *check_circular*: is deprecated now, use the :max_nesting option instead,
491
+ # * *max_nesting*: sets the maximum level of data structure nesting in
492
+ # the generated JSON, max_nesting = 0 if no maximum should be checked.
493
+ # * *allow_nan*: true if NaN, Infinity, and -Infinity should be
494
+ # generated, otherwise an exception is thrown, if these values are
495
+ # encountered. This options defaults to false.
496
+ # * *quirks_mode*: Enables quirks_mode for parser, that is for example
497
+ # generating single JSON values instead of documents is possible.
498
+ def initialize(opts = {})
499
+ @indent = ''
500
+ @space = ''
501
+ @space_before = ''
502
+ @object_nl = ''
503
+ @array_nl = ''
504
+ @allow_nan = false
505
+ @ascii_only = false
506
+ @quirks_mode = false
507
+ @buffer_initial_length = 1024
508
+ configure opts
509
+ end
510
+
511
+ # This string is used to indent levels in the JSON text.
512
+ attr_accessor :indent
513
+
514
+ # This string is used to insert a space between the tokens in a JSON
515
+ # string.
516
+ attr_accessor :space
517
+
518
+ # This string is used to insert a space before the ':' in JSON objects.
519
+ attr_accessor :space_before
520
+
521
+ # This string is put at the end of a line that holds a JSON object (or
522
+ # Hash).
523
+ attr_accessor :object_nl
524
+
525
+ # This string is put at the end of a line that holds a JSON array.
526
+ attr_accessor :array_nl
527
+
528
+ # This integer returns the maximum level of data structure nesting in
529
+ # the generated JSON, max_nesting = 0 if no maximum is checked.
530
+ attr_accessor :max_nesting
531
+
532
+ # If this attribute is set to true, quirks mode is enabled, otherwise
533
+ # it's disabled.
534
+ attr_accessor :quirks_mode
535
+
536
+ # :stopdoc:
537
+ attr_reader :buffer_initial_length
538
+
539
+ def buffer_initial_length=(length)
540
+ if length > 0
541
+ @buffer_initial_length = length
542
+ end
543
+ end
544
+ # :startdoc:
545
+
546
+ # This integer returns the current depth data structure nesting in the
547
+ # generated JSON.
548
+ attr_accessor :depth
549
+
550
+ def check_max_nesting # :nodoc:
551
+ return if @max_nesting.zero?
552
+ current_nesting = depth + 1
553
+ current_nesting > @max_nesting and
554
+ raise NestingError, "nesting of #{current_nesting} is too deep"
555
+ end
556
+
557
+ # Returns true, if circular data structures are checked,
558
+ # otherwise returns false.
559
+ def check_circular?
560
+ !@max_nesting.zero?
561
+ end
562
+
563
+ # Returns true if NaN, Infinity, and -Infinity should be considered as
564
+ # valid JSON and output.
565
+ def allow_nan?
566
+ @allow_nan
567
+ end
568
+
569
+ # Returns true, if only ASCII characters should be generated. Otherwise
570
+ # returns false.
571
+ def ascii_only?
572
+ @ascii_only
573
+ end
574
+
575
+ # Returns true, if quirks mode is enabled. Otherwise returns false.
576
+ def quirks_mode?
577
+ @quirks_mode
578
+ end
579
+
580
+ # Configure this State instance with the Hash _opts_, and return
581
+ # itself.
582
+ def configure(opts)
583
+ @indent = opts[:indent] if opts.key?(:indent)
584
+ @space = opts[:space] if opts.key?(:space)
585
+ @space_before = opts[:space_before] if opts.key?(:space_before)
586
+ @object_nl = opts[:object_nl] if opts.key?(:object_nl)
587
+ @array_nl = opts[:array_nl] if opts.key?(:array_nl)
588
+ @allow_nan = !!opts[:allow_nan] if opts.key?(:allow_nan)
589
+ @ascii_only = opts[:ascii_only] if opts.key?(:ascii_only)
590
+ @depth = opts[:depth] || 0
591
+ @quirks_mode = opts[:quirks_mode] if opts.key?(:quirks_mode)
592
+ if !opts.key?(:max_nesting) # defaults to 19
593
+ @max_nesting = 19
594
+ elsif opts[:max_nesting]
595
+ @max_nesting = opts[:max_nesting]
596
+ else
597
+ @max_nesting = 0
598
+ end
599
+ self
600
+ end
601
+ alias merge configure
602
+
603
+ # Returns the configuration instance variables as a hash, that can be
604
+ # passed to the configure method.
605
+ def to_h
606
+ result = {}
607
+ for iv in %w[indent space space_before object_nl array_nl allow_nan max_nesting ascii_only quirks_mode buffer_initial_length depth]
608
+ result[iv.intern] = instance_variable_get("@#{iv}")
609
+ end
610
+ result
611
+ end
612
+
613
+ # Generates a valid JSON document from object +obj+ and returns the
614
+ # result. If no valid JSON document can be created this method raises a
615
+ # GeneratorError exception.
616
+ def generate(obj)
617
+ result = obj.to_json(self)
618
+ unless @quirks_mode
619
+ unless result =~ /\A\s*\[/ && result =~ /\]\s*\Z/ ||
620
+ result =~ /\A\s*\{/ && result =~ /\}\s*\Z/
621
+ then
622
+ raise GeneratorError, "only generation of JSON objects or arrays allowed"
623
+ end
624
+ end
625
+ result
626
+ end
627
+
628
+ # Return the value returned by method +name+.
629
+ def [](name)
630
+ __send__ name
631
+ end
632
+ end
633
+
634
+ module GeneratorMethods
635
+ module Object
636
+ # Converts this object to a string (calling #to_s), converts
637
+ # it to a JSON string, and returns the result. This is a fallback, if no
638
+ # special method #to_json was defined for some object.
639
+ def to_json(*) to_s.to_json end
640
+ end
641
+
642
+ module Hash
643
+ # Returns a JSON string containing a JSON object, that is unparsed from
644
+ # this Hash instance.
645
+ # _state_ is a JSON::State object, that can also be used to configure the
646
+ # produced JSON string output further.
647
+ # _depth_ is used to find out nesting depth, to indent accordingly.
648
+ def to_json(state = nil, *)
649
+ state = State.from_state(state)
650
+ state.check_max_nesting
651
+ json_transform(state)
652
+ end
653
+
654
+ private
655
+
656
+ def json_shift(state)
657
+ state.object_nl.empty? or return ''
658
+ state.indent * state.depth
659
+ end
660
+
661
+ def json_transform(state)
662
+ delim = ','
663
+ delim << state.object_nl
664
+ result = '{'
665
+ result << state.object_nl
666
+ depth = state.depth += 1
667
+ first = true
668
+ indent = !state.object_nl.empty?
669
+ each { |key,value|
670
+ result << delim unless first
671
+ result << state.indent * depth if indent
672
+ result << key.to_s.to_json(state)
673
+ result << state.space_before
674
+ result << ':'
675
+ result << state.space
676
+ result << value.to_json(state)
677
+ first = false
678
+ }
679
+ depth = state.depth -= 1
680
+ result << state.object_nl
681
+ result << state.indent * depth if indent if indent
682
+ result << '}'
683
+ result
684
+ end
685
+ end
686
+
687
+ module Array
688
+ # Returns a JSON string containing a JSON array, that is unparsed from
689
+ # this Array instance.
690
+ # _state_ is a JSON::State object, that can also be used to configure the
691
+ # produced JSON string output further.
692
+ def to_json(state = nil, *)
693
+ state = State.from_state(state)
694
+ state.check_max_nesting
695
+ json_transform(state)
696
+ end
697
+
698
+ private
699
+
700
+ def json_transform(state)
701
+ delim = ','
702
+ delim << state.array_nl
703
+ result = '['
704
+ result << state.array_nl
705
+ depth = state.depth += 1
706
+ first = true
707
+ indent = !state.array_nl.empty?
708
+ each { |value|
709
+ result << delim unless first
710
+ result << state.indent * depth if indent
711
+ result << value.to_json(state)
712
+ first = false
713
+ }
714
+ depth = state.depth -= 1
715
+ result << state.array_nl
716
+ result << state.indent * depth if indent
717
+ result << ']'
718
+ end
719
+ end
720
+
721
+ module Integer
722
+ # Returns a JSON string representation for this Integer number.
723
+ def to_json(*) to_s end
724
+ end
725
+
726
+ module Float
727
+ # Returns a JSON string representation for this Float number.
728
+ def to_json(state = nil, *)
729
+ state = State.from_state(state)
730
+ case
731
+ when infinite?
732
+ if state.allow_nan?
733
+ to_s
734
+ else
735
+ raise GeneratorError, "#{self} not allowed in JSON"
736
+ end
737
+ when nan?
738
+ if state.allow_nan?
739
+ to_s
740
+ else
741
+ raise GeneratorError, "#{self} not allowed in JSON"
742
+ end
743
+ else
744
+ to_s
745
+ end
746
+ end
747
+ end
748
+
749
+ module String
750
+ if defined?(::Encoding)
751
+ # This string should be encoded with UTF-8 A call to this method
752
+ # returns a JSON string encoded with UTF16 big endian characters as
753
+ # \u????.
754
+ def to_json(state = nil, *args)
755
+ state = State.from_state(state)
756
+ if encoding == ::Encoding::UTF_8
757
+ string = self
758
+ else
759
+ string = encode(::Encoding::UTF_8)
760
+ end
761
+ if state.ascii_only?
762
+ '"' << JSON.utf8_to_json_ascii(string) << '"'
763
+ else
764
+ '"' << JSON.utf8_to_json(string) << '"'
765
+ end
766
+ end
767
+ else
768
+ # This string should be encoded with UTF-8 A call to this method
769
+ # returns a JSON string encoded with UTF16 big endian characters as
770
+ # \u????.
771
+ def to_json(state = nil, *args)
772
+ state = State.from_state(state)
773
+ if state.ascii_only?
774
+ '"' << JSON.utf8_to_json_ascii(self) << '"'
775
+ else
776
+ '"' << JSON.utf8_to_json(self) << '"'
777
+ end
778
+ end
779
+ end
780
+
781
+ # Module that holds the extinding methods if, the String module is
782
+ # included.
783
+ module Extend
784
+ # Raw Strings are JSON Objects (the raw bytes are stored in an
785
+ # array for the key "raw"). The Ruby String can be created by this
786
+ # module method.
787
+ def json_create(o)
788
+ o['raw'].pack('C*')
789
+ end
790
+ end
791
+
792
+ # Extends _modul_ with the String::Extend module.
793
+ def self.included(modul)
794
+ modul.extend Extend
795
+ end
796
+
797
+ # This method creates a raw object hash, that can be nested into
798
+ # other data structures and will be unparsed as a raw string. This
799
+ # method should be used, if you want to convert raw strings to JSON
800
+ # instead of UTF-8 strings, e. g. binary data.
801
+ def to_json_raw_object
802
+ {
803
+ JSON.create_id => self.class.name,
804
+ 'raw' => self.unpack('C*'),
805
+ }
806
+ end
807
+
808
+ # This method creates a JSON text from the result of
809
+ # a call to to_json_raw_object of this String.
810
+ def to_json_raw(*args)
811
+ to_json_raw_object.to_json(*args)
812
+ end
813
+ end
814
+
815
+ module TrueClass
816
+ # Returns a JSON string for true: 'true'.
817
+ def to_json(*) 'true' end
818
+ end
819
+
820
+ module FalseClass
821
+ # Returns a JSON string for false: 'false'.
822
+ def to_json(*) 'false' end
823
+ end
824
+
825
+ module NilClass
826
+ # Returns a JSON string for nil: 'null'.
827
+ def to_json(*) 'null' end
828
+ end
829
+ end
830
+ end
831
+ end
832
+ end
833
+
834
+ module JSON
835
+ class << self
836
+ # If _object_ is string-like, parse the string and return the parsed result
837
+ # as a Ruby data structure. Otherwise generate a JSON text from the Ruby
838
+ # data structure object and return it.
839
+ #
840
+ # The _opts_ argument is passed through to generate/parse respectively. See
841
+ # generate and parse for their documentation.
842
+ def [](object, opts = {})
843
+ if object.respond_to? :to_str
844
+ JSON.parse(object.to_str, opts)
845
+ else
846
+ JSON.generate(object, opts)
847
+ end
848
+ end
849
+
850
+ # Returns the JSON parser class that is used by JSON. This is either
851
+ # JSON::Ext::Parser or JSON::Pure::Parser.
852
+ attr_reader :parser
853
+
854
+ # Set the JSON parser class _parser_ to be used by JSON.
855
+ def parser=(parser) # :nodoc:
856
+ @parser = parser
857
+ remove_const :Parser if JSON.const_defined_in?(self, :Parser)
858
+ const_set :Parser, parser
859
+ end
860
+
861
+ # Return the constant located at _path_. The format of _path_ has to be
862
+ # either ::A::B::C or A::B::C. In any case, A has to be located at the top
863
+ # level (absolute namespace path?). If there doesn't exist a constant at
864
+ # the given path, an ArgumentError is raised.
865
+ def deep_const_get(path) # :nodoc:
866
+ path.to_s.split(/::/).inject(Object) do |p, c|
867
+ case
868
+ when c.empty? then p
869
+ when JSON.const_defined_in?(p, c) then p.const_get(c)
870
+ else
871
+ begin
872
+ p.const_missing(c)
873
+ rescue NameError => e
874
+ raise ArgumentError, "can't get const #{path}: #{e}"
875
+ end
876
+ end
877
+ end
878
+ end
879
+
880
+ # Set the module _generator_ to be used by JSON.
881
+ def generator=(generator) # :nodoc:
882
+ old, $VERBOSE = $VERBOSE, nil
883
+ @generator = generator
884
+ generator_methods = generator::GeneratorMethods
885
+ for const in generator_methods.constants
886
+ klass = deep_const_get(const)
887
+ modul = generator_methods.const_get(const)
888
+ klass.class_eval do
889
+ instance_methods(false).each do |m|
890
+ m.to_s == 'to_json' and remove_method m
891
+ end
892
+ include modul
893
+ end
894
+ end
895
+ self.state = generator::State
896
+ const_set :State, self.state
897
+ const_set :SAFE_STATE_PROTOTYPE, State.new
898
+ const_set :FAST_STATE_PROTOTYPE, State.new(
899
+ :indent => '',
900
+ :space => '',
901
+ :object_nl => "",
902
+ :array_nl => "",
903
+ :max_nesting => false
904
+ )
905
+ const_set :PRETTY_STATE_PROTOTYPE, State.new(
906
+ :indent => ' ',
907
+ :space => ' ',
908
+ :object_nl => "\n",
909
+ :array_nl => "\n"
910
+ )
911
+ ensure
912
+ $VERBOSE = old
913
+ end
914
+
915
+ # Returns the JSON generator module that is used by JSON. This is
916
+ # either JSON::Ext::Generator or JSON::Pure::Generator.
917
+ attr_reader :generator
918
+
919
+ # Returns the JSON generator state class that is used by JSON. This is
920
+ # either JSON::Ext::Generator::State or JSON::Pure::Generator::State.
921
+ attr_accessor :state
922
+
923
+ # This is create identifier, which is used to decide if the _json_create_
924
+ # hook of a class should be called. It defaults to 'json_class'.
925
+ attr_accessor :create_id
926
+ end
927
+ self.create_id = 'json_class'
928
+
929
+ NaN = 0.0/0
930
+
931
+ Infinity = 1.0/0
932
+
933
+ MinusInfinity = -Infinity
934
+
935
+ # The base exception for JSON errors.
936
+ class JSONError < StandardError; end
937
+
938
+ # This exception is raised if a parser error occurs.
939
+ class ParserError < JSONError; end
940
+
941
+ # This exception is raised if the nesting of parsed data structures is too
942
+ # deep.
943
+ class NestingError < ParserError; end
944
+
945
+ # :stopdoc:
946
+ class CircularDatastructure < NestingError; end
947
+ # :startdoc:
948
+
949
+ # This exception is raised if a generator or unparser error occurs.
950
+ class GeneratorError < JSONError; end
951
+ # For backwards compatibility
952
+ UnparserError = GeneratorError
953
+
954
+ # This exception is raised if the required unicode support is missing on the
955
+ # system. Usually this means that the iconv library is not installed.
956
+ class MissingUnicodeSupport < JSONError; end
957
+
958
+ module_function
959
+
960
+ # Parse the JSON document _source_ into a Ruby data structure and return it.
961
+ #
962
+ # _opts_ can have the following
963
+ # keys:
964
+ # * *max_nesting*: The maximum depth of nesting allowed in the parsed data
965
+ # structures. Disable depth checking with :max_nesting => false. It defaults
966
+ # to 19.
967
+ # * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
968
+ # defiance of RFC 4627 to be parsed by the Parser. This option defaults
969
+ # to false.
970
+ # * *symbolize_names*: If set to true, returns symbols for the names
971
+ # (keys) in a JSON object. Otherwise strings are returned. Strings are
972
+ # the default.
973
+ # * *create_additions*: If set to false, the Parser doesn't create
974
+ # additions even if a matching class and create_id was found. This option
975
+ # defaults to true.
976
+ # * *object_class*: Defaults to Hash
977
+ # * *array_class*: Defaults to Array
978
+ def parse(source, opts = {})
979
+ Parser.new(source, opts).parse
980
+ end
981
+
982
+ # Parse the JSON document _source_ into a Ruby data structure and return it.
983
+ # The bang version of the parse method defaults to the more dangerous values
984
+ # for the _opts_ hash, so be sure only to parse trusted _source_ documents.
985
+ #
986
+ # _opts_ can have the following keys:
987
+ # * *max_nesting*: The maximum depth of nesting allowed in the parsed data
988
+ # structures. Enable depth checking with :max_nesting => anInteger. The parse!
989
+ # methods defaults to not doing max depth checking: This can be dangerous
990
+ # if someone wants to fill up your stack.
991
+ # * *allow_nan*: If set to true, allow NaN, Infinity, and -Infinity in
992
+ # defiance of RFC 4627 to be parsed by the Parser. This option defaults
993
+ # to true.
994
+ # * *create_additions*: If set to false, the Parser doesn't create
995
+ # additions even if a matching class and create_id was found. This option
996
+ # defaults to true.
997
+ def parse!(source, opts = {})
998
+ opts = {
999
+ :max_nesting => false,
1000
+ :allow_nan => true
1001
+ }.update(opts)
1002
+ Parser.new(source, opts).parse
1003
+ end
1004
+
1005
+ # Generate a JSON document from the Ruby data structure _obj_ and return
1006
+ # it. _state_ is * a JSON::State object,
1007
+ # * or a Hash like object (responding to to_hash),
1008
+ # * an object convertible into a hash by a to_h method,
1009
+ # that is used as or to configure a State object.
1010
+ #
1011
+ # It defaults to a state object, that creates the shortest possible JSON text
1012
+ # in one line, checks for circular data structures and doesn't allow NaN,
1013
+ # Infinity, and -Infinity.
1014
+ #
1015
+ # A _state_ hash can have the following keys:
1016
+ # * *indent*: a string used to indent levels (default: ''),
1017
+ # * *space*: a string that is put after, a : or , delimiter (default: ''),
1018
+ # * *space_before*: a string that is put before a : pair delimiter (default: ''),
1019
+ # * *object_nl*: a string that is put at the end of a JSON object (default: ''),
1020
+ # * *array_nl*: a string that is put at the end of a JSON array (default: ''),
1021
+ # * *allow_nan*: true if NaN, Infinity, and -Infinity should be
1022
+ # generated, otherwise an exception is thrown if these values are
1023
+ # encountered. This options defaults to false.
1024
+ # * *max_nesting*: The maximum depth of nesting allowed in the data
1025
+ # structures from which JSON is to be generated. Disable depth checking
1026
+ # with :max_nesting => false, it defaults to 19.
1027
+ #
1028
+ # See also the fast_generate for the fastest creation method with the least
1029
+ # amount of sanity checks, and the pretty_generate method for some
1030
+ # defaults for pretty output.
1031
+ def generate(obj, opts = nil)
1032
+ if State === opts
1033
+ state, opts = opts, nil
1034
+ else
1035
+ state = SAFE_STATE_PROTOTYPE.dup
1036
+ end
1037
+ if opts
1038
+ if opts.respond_to? :to_hash
1039
+ opts = opts.to_hash
1040
+ elsif opts.respond_to? :to_h
1041
+ opts = opts.to_h
1042
+ else
1043
+ raise TypeError, "can't convert #{opts.class} into Hash"
1044
+ end
1045
+ state = state.configure(opts)
1046
+ end
1047
+ state.generate(obj)
1048
+ end
1049
+
1050
+ # :stopdoc:
1051
+ # I want to deprecate these later, so I'll first be silent about them, and
1052
+ # later delete them.
1053
+ alias unparse generate
1054
+ module_function :unparse
1055
+ # :startdoc:
1056
+
1057
+ # Generate a JSON document from the Ruby data structure _obj_ and return it.
1058
+ # This method disables the checks for circles in Ruby objects.
1059
+ #
1060
+ # *WARNING*: Be careful not to pass any Ruby data structures with circles as
1061
+ # _obj_ argument because this will cause JSON to go into an infinite loop.
1062
+ def fast_generate(obj, opts = nil)
1063
+ if State === opts
1064
+ state, opts = opts, nil
1065
+ else
1066
+ state = FAST_STATE_PROTOTYPE.dup
1067
+ end
1068
+ if opts
1069
+ if opts.respond_to? :to_hash
1070
+ opts = opts.to_hash
1071
+ elsif opts.respond_to? :to_h
1072
+ opts = opts.to_h
1073
+ else
1074
+ raise TypeError, "can't convert #{opts.class} into Hash"
1075
+ end
1076
+ state.configure(opts)
1077
+ end
1078
+ state.generate(obj)
1079
+ end
1080
+
1081
+ # :stopdoc:
1082
+ # I want to deprecate these later, so I'll first be silent about them, and later delete them.
1083
+ alias fast_unparse fast_generate
1084
+ module_function :fast_unparse
1085
+ # :startdoc:
1086
+
1087
+ # Generate a JSON document from the Ruby data structure _obj_ and return it.
1088
+ # The returned document is a prettier form of the document returned by
1089
+ # #unparse.
1090
+ #
1091
+ # The _opts_ argument can be used to configure the generator. See the
1092
+ # generate method for a more detailed explanation.
1093
+ def pretty_generate(obj, opts = nil)
1094
+ if State === opts
1095
+ state, opts = opts, nil
1096
+ else
1097
+ state = PRETTY_STATE_PROTOTYPE.dup
1098
+ end
1099
+ if opts
1100
+ if opts.respond_to? :to_hash
1101
+ opts = opts.to_hash
1102
+ elsif opts.respond_to? :to_h
1103
+ opts = opts.to_h
1104
+ else
1105
+ raise TypeError, "can't convert #{opts.class} into Hash"
1106
+ end
1107
+ state.configure(opts)
1108
+ end
1109
+ state.generate(obj)
1110
+ end
1111
+
1112
+ # :stopdoc:
1113
+ # I want to deprecate these later, so I'll first be silent about them, and later delete them.
1114
+ alias pretty_unparse pretty_generate
1115
+ module_function :pretty_unparse
1116
+ # :startdoc:
1117
+
1118
+ class << self
1119
+ # The global default options for the JSON.load method:
1120
+ # :max_nesting: false
1121
+ # :allow_nan: true
1122
+ # :quirks_mode: true
1123
+ attr_accessor :load_default_options
1124
+ end
1125
+ self.load_default_options = {
1126
+ :max_nesting => false,
1127
+ :allow_nan => true,
1128
+ :quirks_mode => true,
1129
+ }
1130
+
1131
+ # Load a ruby data structure from a JSON _source_ and return it. A source can
1132
+ # either be a string-like object, an IO-like object, or an object responding
1133
+ # to the read method. If _proc_ was given, it will be called with any nested
1134
+ # Ruby object as an argument recursively in depth first order. The default
1135
+ # options for the parser can be changed via the load_default_options method.
1136
+ #
1137
+ # This method is part of the implementation of the load/dump interface of
1138
+ # Marshal and YAML.
1139
+ def load(source, proc = nil)
1140
+ opts = load_default_options
1141
+ if source.respond_to? :to_str
1142
+ source = source.to_str
1143
+ elsif source.respond_to? :to_io
1144
+ source = source.to_io.read
1145
+ elsif source.respond_to?(:read)
1146
+ source = source.read
1147
+ end
1148
+ if opts[:quirks_mode] && (source.nil? || source.empty?)
1149
+ source = 'null'
1150
+ end
1151
+ result = parse(source, opts)
1152
+ recurse_proc(result, &proc) if proc
1153
+ result
1154
+ end
1155
+
1156
+ # Recursively calls passed _Proc_ if the parsed data structure is an _Array_ or _Hash_
1157
+ def recurse_proc(result, &proc)
1158
+ case result
1159
+ when Array
1160
+ result.each { |x| recurse_proc x, &proc }
1161
+ proc.call result
1162
+ when Hash
1163
+ result.each { |x, y| recurse_proc x, &proc; recurse_proc y, &proc }
1164
+ proc.call result
1165
+ else
1166
+ proc.call result
1167
+ end
1168
+ end
1169
+
1170
+ alias restore load
1171
+ module_function :restore
1172
+
1173
+ class << self
1174
+ # The global default options for the JSON.dump method:
1175
+ # :max_nesting: false
1176
+ # :allow_nan: true
1177
+ # :quirks_mode: true
1178
+ attr_accessor :dump_default_options
1179
+ end
1180
+ self.dump_default_options = {
1181
+ :max_nesting => false,
1182
+ :allow_nan => true,
1183
+ :quirks_mode => true,
1184
+ }
1185
+
1186
+ # Dumps _obj_ as a JSON string, i.e. calls generate on the object and returns
1187
+ # the result.
1188
+ #
1189
+ # If anIO (an IO-like object or an object that responds to the write method)
1190
+ # was given, the resulting JSON is written to it.
1191
+ #
1192
+ # If the number of nested arrays or objects exceeds _limit_, an ArgumentError
1193
+ # exception is raised. This argument is similar (but not exactly the
1194
+ # same!) to the _limit_ argument in Marshal.dump.
1195
+ #
1196
+ # The default options for the generator can be changed via the
1197
+ # dump_default_options method.
1198
+ #
1199
+ # This method is part of the implementation of the load/dump interface of
1200
+ # Marshal and YAML.
1201
+ def dump(obj, anIO = nil, limit = nil)
1202
+ if anIO and limit.nil?
1203
+ anIO = anIO.to_io if anIO.respond_to?(:to_io)
1204
+ unless anIO.respond_to?(:write)
1205
+ limit = anIO
1206
+ anIO = nil
1207
+ end
1208
+ end
1209
+ opts = JSON.dump_default_options
1210
+ limit and opts.update(:max_nesting => limit)
1211
+ result = generate(obj, opts)
1212
+ if anIO
1213
+ anIO.write result
1214
+ anIO
1215
+ else
1216
+ result
1217
+ end
1218
+ rescue JSON::NestingError
1219
+ raise ArgumentError, "exceed depth limit"
1220
+ end
1221
+
1222
+ # Swap consecutive bytes of _string_ in place.
1223
+ def self.swap!(string) # :nodoc:
1224
+ 0.upto(string.size / 2) do |i|
1225
+ break unless string[2 * i + 1]
1226
+ string[2 * i], string[2 * i + 1] = string[2 * i + 1], string[2 * i]
1227
+ end
1228
+ string
1229
+ end
1230
+
1231
+ # Shortuct for iconv.
1232
+ if ::String.method_defined?(:encode)
1233
+ # Encodes string using Ruby's _String.encode_
1234
+ def self.iconv(to, from, string)
1235
+ string.encode(to, from)
1236
+ end
1237
+ else
1238
+ require 'iconv'
1239
+ # Encodes string using _iconv_ library
1240
+ def self.iconv(to, from, string)
1241
+ Iconv.conv(to, from, string)
1242
+ end
1243
+ end
1244
+
1245
+ if ::Object.method(:const_defined?).arity == 1
1246
+ def self.const_defined_in?(modul, constant)
1247
+ modul.const_defined?(constant)
1248
+ end
1249
+ else
1250
+ def self.const_defined_in?(modul, constant)
1251
+ modul.const_defined?(constant, false)
1252
+ end
1253
+ end
1254
+ end
1255
+
1256
+ module ::Kernel
1257
+ private
1258
+
1259
+ # Outputs _objs_ to STDOUT as JSON strings in the shortest form, that is in
1260
+ # one line.
1261
+ def j(*objs)
1262
+ objs.each do |obj|
1263
+ puts JSON::generate(obj, :allow_nan => true, :max_nesting => false)
1264
+ end
1265
+ nil
1266
+ end
1267
+
1268
+ # Ouputs _objs_ to STDOUT as JSON strings in a pretty format, with
1269
+ # indentation and over many lines.
1270
+ def jj(*objs)
1271
+ objs.each do |obj|
1272
+ puts JSON::pretty_generate(obj, :allow_nan => true, :max_nesting => false)
1273
+ end
1274
+ nil
1275
+ end
1276
+
1277
+ # If _object_ is string-like, parse the string and return the parsed result as
1278
+ # a Ruby data structure. Otherwise, generate a JSON text from the Ruby data
1279
+ # structure object and return it.
1280
+ #
1281
+ # The _opts_ argument is passed through to generate/parse respectively. See
1282
+ # generate and parse for their documentation.
1283
+ def JSON(object, *args)
1284
+ if object.respond_to? :to_str
1285
+ JSON.parse(object.to_str, args.first)
1286
+ else
1287
+ JSON.generate(object, args.first)
1288
+ end
1289
+ end
1290
+ end
1291
+
1292
+ # Extends any Class to include _json_creatable?_ method.
1293
+ class ::Class
1294
+ # Returns true if this class can be used to create an instance
1295
+ # from a serialised JSON string. The class has to implement a class
1296
+ # method _json_create_ that expects a hash as first parameter. The hash
1297
+ # should include the required data.
1298
+ def json_creatable?
1299
+ respond_to?(:json_create)
1300
+ end
1301
+ end
1302
+
1303
+ JSON.generator = JSON::Pure::Generator
1304
+ JSON.parser = JSON::Pure::Parser