mack-javascript 0.8.1 → 0.8.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/gems/json_pure-1.1.3/VERSION +1 -0
- data/lib/gems/json_pure-1.1.3/bin/edit_json.rb +10 -0
- data/lib/gems/json_pure-1.1.3/bin/prettify_json.rb +76 -0
- data/lib/gems/json_pure-1.1.3/lib/json/Array.xpm +21 -0
- data/lib/gems/json_pure-1.1.3/lib/json/FalseClass.xpm +21 -0
- data/lib/gems/json_pure-1.1.3/lib/json/Hash.xpm +21 -0
- data/lib/gems/json_pure-1.1.3/lib/json/Key.xpm +73 -0
- data/lib/gems/json_pure-1.1.3/lib/json/NilClass.xpm +21 -0
- data/lib/gems/json_pure-1.1.3/lib/json/Numeric.xpm +28 -0
- data/lib/gems/json_pure-1.1.3/lib/json/String.xpm +96 -0
- data/lib/gems/json_pure-1.1.3/lib/json/TrueClass.xpm +21 -0
- data/lib/gems/json_pure-1.1.3/lib/json/add/core.rb +135 -0
- data/lib/gems/json_pure-1.1.3/lib/json/add/rails.rb +58 -0
- data/lib/gems/json_pure-1.1.3/lib/json/common.rb +354 -0
- data/lib/gems/json_pure-1.1.3/lib/json/editor.rb +1362 -0
- data/lib/gems/json_pure-1.1.3/lib/json/ext.rb +13 -0
- data/lib/gems/json_pure-1.1.3/lib/json/json.xpm +1499 -0
- data/lib/gems/json_pure-1.1.3/lib/json/pure/generator.rb +394 -0
- data/lib/gems/json_pure-1.1.3/lib/json/pure/parser.rb +259 -0
- data/lib/gems/json_pure-1.1.3/lib/json/pure.rb +75 -0
- data/lib/gems/json_pure-1.1.3/lib/json/version.rb +9 -0
- data/lib/gems/json_pure-1.1.3/lib/json.rb +235 -0
- data/lib/gems.rb +13 -0
- data/lib/mack-javascript/generators/templates/javascripts/prototype.js.template +1 -1
- data/lib/mack-javascript/rendering/engine/rjs.rb +3 -0
- data/lib/mack-javascript/rendering/type/js.rb +7 -3
- data/lib/mack-javascript/view_helpers/html_helpers.rb +11 -0
- data/lib/mack-javascript.rb +2 -0
- metadata +50 -16
@@ -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
|