csd 0.0.15 → 0.0.16
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.
- data/.gitignore +1 -0
- data/COPYING +367 -0
- data/Rakefile +10 -10
- data/VERSION +1 -1
- data/bin/ai +19 -0
- data/csd.gemspec +257 -35
- data/lib/active_support.rb +75 -0
- data/lib/active_support/all.rb +3 -0
- data/lib/active_support/backtrace_cleaner.rb +94 -0
- data/lib/active_support/base64.rb +42 -0
- data/lib/active_support/basic_object.rb +21 -0
- data/lib/active_support/benchmarkable.rb +60 -0
- data/lib/active_support/buffered_logger.rb +132 -0
- data/lib/active_support/builder.rb +6 -0
- data/lib/active_support/cache.rb +626 -0
- data/lib/active_support/cache/compressed_mem_cache_store.rb +13 -0
- data/lib/active_support/cache/file_store.rb +188 -0
- data/lib/active_support/cache/mem_cache_store.rb +191 -0
- data/lib/active_support/cache/memory_store.rb +159 -0
- data/lib/active_support/cache/strategy/local_cache.rb +164 -0
- data/lib/active_support/cache/synchronized_memory_store.rb +11 -0
- data/lib/active_support/callbacks.rb +600 -0
- data/lib/active_support/concern.rb +29 -0
- data/lib/active_support/configurable.rb +36 -0
- data/lib/active_support/core_ext.rb +3 -0
- data/lib/active_support/core_ext/array.rb +7 -0
- data/lib/active_support/core_ext/array/access.rb +46 -0
- data/lib/active_support/core_ext/array/conversions.rb +164 -0
- data/lib/active_support/core_ext/array/extract_options.rb +29 -0
- data/lib/active_support/core_ext/array/grouping.rb +100 -0
- data/lib/active_support/core_ext/array/random_access.rb +20 -0
- data/lib/active_support/core_ext/array/uniq_by.rb +17 -0
- data/lib/active_support/core_ext/array/wrap.rb +22 -0
- data/lib/active_support/core_ext/benchmark.rb +7 -0
- data/lib/active_support/core_ext/big_decimal.rb +1 -0
- data/lib/active_support/core_ext/big_decimal/conversions.rb +27 -0
- data/lib/active_support/core_ext/cgi.rb +1 -0
- data/lib/active_support/core_ext/cgi/escape_skipping_slashes.rb +19 -0
- data/lib/active_support/core_ext/class.rb +4 -0
- data/lib/active_support/core_ext/class/attribute.rb +67 -0
- data/lib/active_support/core_ext/class/attribute_accessors.rb +63 -0
- data/lib/active_support/core_ext/class/delegating_attributes.rb +44 -0
- data/lib/active_support/core_ext/class/inheritable_attributes.rb +232 -0
- data/lib/active_support/core_ext/class/subclasses.rb +55 -0
- data/lib/active_support/core_ext/date/acts_like.rb +8 -0
- data/lib/active_support/core_ext/date/calculations.rb +240 -0
- data/lib/active_support/core_ext/date/conversions.rb +99 -0
- data/lib/active_support/core_ext/date/freeze.rb +31 -0
- data/lib/active_support/core_ext/date_time/acts_like.rb +13 -0
- data/lib/active_support/core_ext/date_time/calculations.rb +113 -0
- data/lib/active_support/core_ext/date_time/conversions.rb +102 -0
- data/lib/active_support/core_ext/date_time/zones.rb +17 -0
- data/lib/active_support/core_ext/enumerable.rb +119 -0
- data/lib/active_support/core_ext/exception.rb +3 -0
- data/lib/active_support/core_ext/file.rb +2 -0
- data/lib/active_support/core_ext/file/atomic.rb +41 -0
- data/lib/active_support/core_ext/file/path.rb +5 -0
- data/lib/active_support/core_ext/float.rb +1 -0
- data/lib/active_support/core_ext/float/rounding.rb +19 -0
- data/lib/active_support/core_ext/hash.rb +8 -0
- data/lib/active_support/core_ext/hash/conversions.rb +150 -0
- data/lib/active_support/core_ext/hash/deep_merge.rb +16 -0
- data/lib/active_support/core_ext/hash/diff.rb +13 -0
- data/lib/active_support/core_ext/hash/except.rb +24 -0
- data/lib/active_support/core_ext/hash/indifferent_access.rb +14 -0
- data/lib/active_support/core_ext/hash/keys.rb +45 -0
- data/lib/active_support/core_ext/hash/reverse_merge.rb +28 -0
- data/lib/active_support/core_ext/hash/slice.rb +38 -0
- data/lib/active_support/core_ext/integer.rb +3 -0
- data/lib/active_support/core_ext/integer/inflections.rb +14 -0
- data/lib/active_support/core_ext/integer/multiple.rb +6 -0
- data/lib/active_support/core_ext/integer/time.rb +39 -0
- data/lib/active_support/core_ext/kernel.rb +5 -0
- data/lib/active_support/core_ext/kernel/agnostics.rb +11 -0
- data/lib/active_support/core_ext/kernel/debugger.rb +16 -0
- data/lib/active_support/core_ext/kernel/reporting.rb +62 -0
- data/lib/active_support/core_ext/kernel/requires.rb +26 -0
- data/lib/active_support/core_ext/kernel/singleton_class.rb +13 -0
- data/lib/active_support/core_ext/load_error.rb +23 -0
- data/lib/active_support/core_ext/logger.rb +146 -0
- data/lib/active_support/core_ext/module.rb +12 -0
- data/lib/active_support/core_ext/module/aliasing.rb +70 -0
- data/lib/active_support/core_ext/module/anonymous.rb +24 -0
- data/lib/active_support/core_ext/module/attr_accessor_with_default.rb +31 -0
- data/lib/active_support/core_ext/module/attr_internal.rb +32 -0
- data/lib/active_support/core_ext/module/attribute_accessors.rb +66 -0
- data/lib/active_support/core_ext/module/delegation.rb +146 -0
- data/lib/active_support/core_ext/module/deprecation.rb +9 -0
- data/lib/active_support/core_ext/module/introspection.rb +88 -0
- data/lib/active_support/core_ext/module/method_names.rb +14 -0
- data/lib/active_support/core_ext/module/reachable.rb +10 -0
- data/lib/active_support/core_ext/module/remove_method.rb +6 -0
- data/lib/active_support/core_ext/module/synchronization.rb +42 -0
- data/lib/active_support/core_ext/name_error.rb +18 -0
- data/lib/active_support/core_ext/numeric.rb +2 -0
- data/lib/active_support/core_ext/numeric/bytes.rb +44 -0
- data/lib/active_support/core_ext/numeric/time.rb +77 -0
- data/lib/active_support/core_ext/object.rb +14 -0
- data/lib/active_support/core_ext/object/acts_like.rb +10 -0
- data/lib/active_support/core_ext/object/blank.rb +76 -0
- data/lib/active_support/core_ext/object/conversions.rb +4 -0
- data/lib/active_support/core_ext/object/duplicable.rb +65 -0
- data/lib/active_support/core_ext/object/extending.rb +11 -0
- data/lib/active_support/core_ext/object/instance_variables.rb +67 -0
- data/lib/active_support/core_ext/object/misc.rb +2 -0
- data/lib/active_support/core_ext/object/returning.rb +42 -0
- data/lib/active_support/core_ext/object/to_param.rb +49 -0
- data/lib/active_support/core_ext/object/to_query.rb +27 -0
- data/lib/active_support/core_ext/object/try.rb +36 -0
- data/lib/active_support/core_ext/object/with_options.rb +26 -0
- data/lib/active_support/core_ext/proc.rb +14 -0
- data/lib/active_support/core_ext/process.rb +1 -0
- data/lib/active_support/core_ext/process/daemon.rb +23 -0
- data/lib/active_support/core_ext/range.rb +4 -0
- data/lib/active_support/core_ext/range/blockless_step.rb +29 -0
- data/lib/active_support/core_ext/range/conversions.rb +21 -0
- data/lib/active_support/core_ext/range/include_range.rb +21 -0
- data/lib/active_support/core_ext/range/overlaps.rb +8 -0
- data/lib/active_support/core_ext/regexp.rb +5 -0
- data/lib/active_support/core_ext/rexml.rb +46 -0
- data/lib/active_support/core_ext/string.rb +12 -0
- data/lib/active_support/core_ext/string/access.rb +99 -0
- data/lib/active_support/core_ext/string/behavior.rb +7 -0
- data/lib/active_support/core_ext/string/conversions.rb +61 -0
- data/lib/active_support/core_ext/string/encoding.rb +11 -0
- data/lib/active_support/core_ext/string/exclude.rb +6 -0
- data/lib/active_support/core_ext/string/filters.rb +49 -0
- data/lib/active_support/core_ext/string/inflections.rb +149 -0
- data/lib/active_support/core_ext/string/interpolation.rb +2 -0
- data/lib/active_support/core_ext/string/multibyte.rb +72 -0
- data/lib/active_support/core_ext/string/output_safety.rb +109 -0
- data/lib/active_support/core_ext/string/starts_ends_with.rb +4 -0
- data/lib/active_support/core_ext/string/xchar.rb +18 -0
- data/lib/active_support/core_ext/time/acts_like.rb +8 -0
- data/lib/active_support/core_ext/time/calculations.rb +282 -0
- data/lib/active_support/core_ext/time/conversions.rb +85 -0
- data/lib/active_support/core_ext/time/marshal.rb +56 -0
- data/lib/active_support/core_ext/time/publicize_conversion_methods.rb +10 -0
- data/lib/active_support/core_ext/time/zones.rb +78 -0
- data/lib/active_support/core_ext/uri.rb +22 -0
- data/lib/active_support/dependencies.rb +628 -0
- data/lib/active_support/dependencies/autoload.rb +50 -0
- data/lib/active_support/deprecation.rb +18 -0
- data/lib/active_support/deprecation/behaviors.rb +38 -0
- data/lib/active_support/deprecation/method_wrappers.rb +29 -0
- data/lib/active_support/deprecation/proxy_wrappers.rb +74 -0
- data/lib/active_support/deprecation/reporting.rb +56 -0
- data/lib/active_support/duration.rb +105 -0
- data/lib/active_support/gzip.rb +25 -0
- data/lib/active_support/hash_with_indifferent_access.rb +145 -0
- data/lib/active_support/i18n.rb +8 -0
- data/lib/active_support/inflections.rb +56 -0
- data/lib/active_support/inflector.rb +7 -0
- data/lib/active_support/inflector/inflections.rb +211 -0
- data/lib/active_support/inflector/methods.rb +141 -0
- data/lib/active_support/inflector/transliterate.rb +97 -0
- data/lib/active_support/json.rb +2 -0
- data/lib/active_support/json/backends/jsongem.rb +43 -0
- data/lib/active_support/json/backends/yajl.rb +40 -0
- data/lib/active_support/json/backends/yaml.rb +90 -0
- data/lib/active_support/json/decoding.rb +51 -0
- data/lib/active_support/json/encoding.rb +254 -0
- data/lib/active_support/json/variable.rb +11 -0
- data/lib/active_support/lazy_load_hooks.rb +27 -0
- data/lib/active_support/locale/en.yml +36 -0
- data/lib/active_support/memoizable.rb +103 -0
- data/lib/active_support/message_encryptor.rb +71 -0
- data/lib/active_support/message_verifier.rb +62 -0
- data/lib/active_support/multibyte.rb +44 -0
- data/lib/active_support/multibyte/chars.rb +480 -0
- data/lib/active_support/multibyte/exceptions.rb +8 -0
- data/lib/active_support/multibyte/unicode.rb +393 -0
- data/lib/active_support/multibyte/utils.rb +60 -0
- data/lib/active_support/notifications.rb +81 -0
- data/lib/active_support/notifications/fanout.rb +93 -0
- data/lib/active_support/notifications/instrumenter.rb +56 -0
- data/lib/active_support/option_merger.rb +25 -0
- data/lib/active_support/ordered_hash.rb +158 -0
- data/lib/active_support/ordered_options.rb +27 -0
- data/lib/active_support/railtie.rb +100 -0
- data/lib/active_support/rescuable.rb +114 -0
- data/lib/active_support/ruby/shim.rb +22 -0
- data/lib/active_support/secure_random.rb +199 -0
- data/lib/active_support/string_inquirer.rb +21 -0
- data/lib/active_support/test_case.rb +42 -0
- data/lib/active_support/testing/assertions.rb +82 -0
- data/lib/active_support/testing/declarative.rb +40 -0
- data/lib/active_support/testing/default.rb +9 -0
- data/lib/active_support/testing/deprecation.rb +55 -0
- data/lib/active_support/testing/isolation.rb +154 -0
- data/lib/active_support/testing/pending.rb +48 -0
- data/lib/active_support/testing/performance.rb +455 -0
- data/lib/active_support/testing/setup_and_teardown.rb +111 -0
- data/lib/active_support/time.rb +34 -0
- data/lib/active_support/time/autoload.rb +5 -0
- data/lib/active_support/time_with_zone.rb +341 -0
- data/lib/active_support/values/time_zone.rb +377 -0
- data/lib/active_support/values/unicode_tables.dat +0 -0
- data/lib/active_support/version.rb +10 -0
- data/lib/active_support/whiny_nil.rb +60 -0
- data/lib/active_support/xml_mini.rb +158 -0
- data/lib/active_support/xml_mini/jdom.rb +168 -0
- data/lib/active_support/xml_mini/libxml.rb +80 -0
- data/lib/active_support/xml_mini/libxmlsax.rb +85 -0
- data/lib/active_support/xml_mini/nokogiri.rb +78 -0
- data/lib/active_support/xml_mini/nokogirisax.rb +83 -0
- data/lib/active_support/xml_mini/rexml.rb +129 -0
- data/lib/csd.rb +82 -2
- data/lib/csd/application.rb +2 -0
- data/lib/csd/application/default.rb +51 -0
- data/lib/csd/application/default/base.rb +15 -0
- data/lib/csd/application/minisip.rb +25 -0
- data/lib/csd/application/minisip/about.yml +14 -0
- data/lib/csd/application/minisip/base.rb +161 -0
- data/lib/csd/application/minisip/error.rb +11 -0
- data/lib/csd/application/minisip/options/common.rb +0 -0
- data/lib/csd/application/minisip/options/compile.rb +59 -0
- data/lib/csd/{applications → application}/minisip/unix/base.rb +10 -11
- data/lib/csd/application/opensips/about.yml +2 -0
- data/lib/csd/applications.rb +55 -0
- data/lib/csd/commands.rb +88 -65
- data/lib/csd/error.rb +31 -0
- data/lib/csd/extensions.rb +1 -0
- data/lib/{extensions → csd/extensions}/core/array.rb +2 -2
- data/lib/csd/extensions/core/dir.rb +46 -0
- data/lib/{extensions → csd/extensions}/core/file.rb +2 -2
- data/lib/{extensions → csd/extensions}/core/object.rb +2 -2
- data/lib/csd/extensions/core/option_parser.rb +33 -0
- data/lib/{extensions → csd/extensions}/core/pathname.rb +12 -3
- data/lib/{extensions → csd/extensions}/core/string.rb +2 -2
- data/lib/{extensions → csd/extensions}/gem/platform.rb +6 -2
- data/lib/csd/global_open_struct.rb +18 -0
- data/lib/csd/options.rb +124 -95
- data/lib/csd/path.rb +31 -0
- data/lib/csd/ui.rb +1 -0
- data/lib/csd/ui/cli.rb +7 -0
- data/lib/csd/ui/ui.rb +46 -0
- data/lib/csd/version.rb +9 -0
- data/lib/term/ansicolor.rb +102 -0
- data/lib/term/ansicolor/.keep +0 -0
- data/lib/term/ansicolor/version.rb +10 -0
- data/test/functional/test_applications.rb +86 -0
- data/test/functional/test_commands.rb +42 -29
- data/test/functional/test_options.rb +98 -0
- data/test/helper.rb +14 -0
- data/test/unit/test_dir.rb +38 -0
- data/test/unit/test_pathname.rb +32 -0
- metadata +253 -40
- data/LICENSE +0 -20
- data/bin/csd +0 -8
- data/lib/csd/applications/base.rb +0 -33
- data/lib/csd/applications/minisip/base.rb +0 -125
- data/lib/csd/applications/minisip/init.rb +0 -20
- data/lib/csd/init.rb +0 -69
- data/lib/csd/path_container.rb +0 -15
- data/publish +0 -29
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'json' unless defined?(JSON)
|
2
|
+
|
3
|
+
module ActiveSupport
|
4
|
+
module JSON
|
5
|
+
module Backends
|
6
|
+
module JSONGem
|
7
|
+
ParseError = ::JSON::ParserError
|
8
|
+
extend self
|
9
|
+
|
10
|
+
# Parses a JSON string or IO and convert it into an object
|
11
|
+
def decode(json)
|
12
|
+
if json.respond_to?(:read)
|
13
|
+
json = json.read
|
14
|
+
end
|
15
|
+
data = ::JSON.parse(json)
|
16
|
+
if ActiveSupport.parse_json_times
|
17
|
+
convert_dates_from(data)
|
18
|
+
else
|
19
|
+
data
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
def convert_dates_from(data)
|
25
|
+
case data
|
26
|
+
when nil
|
27
|
+
nil
|
28
|
+
when DATE_REGEX
|
29
|
+
DateTime.parse(data)
|
30
|
+
when Array
|
31
|
+
data.map! { |d| convert_dates_from(d) }
|
32
|
+
when Hash
|
33
|
+
data.each do |key, value|
|
34
|
+
data[key] = convert_dates_from(value)
|
35
|
+
end
|
36
|
+
else
|
37
|
+
data
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'yajl-ruby' unless defined?(Yajl)
|
2
|
+
|
3
|
+
module ActiveSupport
|
4
|
+
module JSON
|
5
|
+
module Backends
|
6
|
+
module Yajl
|
7
|
+
ParseError = ::Yajl::ParseError
|
8
|
+
extend self
|
9
|
+
|
10
|
+
# Parses a JSON string or IO and convert it into an object
|
11
|
+
def decode(json)
|
12
|
+
data = ::Yajl::Parser.new.parse(json)
|
13
|
+
if ActiveSupport.parse_json_times
|
14
|
+
convert_dates_from(data)
|
15
|
+
else
|
16
|
+
data
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
def convert_dates_from(data)
|
22
|
+
case data
|
23
|
+
when nil
|
24
|
+
nil
|
25
|
+
when DATE_REGEX
|
26
|
+
DateTime.parse(data)
|
27
|
+
when Array
|
28
|
+
data.map! { |d| convert_dates_from(d) }
|
29
|
+
when Hash
|
30
|
+
data.each do |key, value|
|
31
|
+
data[key] = convert_dates_from(value)
|
32
|
+
end
|
33
|
+
else
|
34
|
+
data
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'active_support/core_ext/string/starts_ends_with'
|
2
|
+
|
3
|
+
module ActiveSupport
|
4
|
+
module JSON
|
5
|
+
module Backends
|
6
|
+
module Yaml
|
7
|
+
ParseError = ::StandardError
|
8
|
+
extend self
|
9
|
+
|
10
|
+
# Parses a JSON string or IO and converts it into an object
|
11
|
+
def decode(json)
|
12
|
+
if json.respond_to?(:read)
|
13
|
+
json = json.read
|
14
|
+
end
|
15
|
+
YAML.load(convert_json_to_yaml(json))
|
16
|
+
rescue ArgumentError => e
|
17
|
+
raise ParseError, "Invalid JSON string"
|
18
|
+
end
|
19
|
+
|
20
|
+
protected
|
21
|
+
# Ensure that ":" and "," are always followed by a space
|
22
|
+
def convert_json_to_yaml(json) #:nodoc:
|
23
|
+
require 'strscan' unless defined? ::StringScanner
|
24
|
+
scanner, quoting, marks, pos, times = ::StringScanner.new(json), false, [], nil, []
|
25
|
+
while scanner.scan_until(/(\\['"]|['":,\\]|\\.)/)
|
26
|
+
case char = scanner[1]
|
27
|
+
when '"', "'"
|
28
|
+
if !quoting
|
29
|
+
quoting = char
|
30
|
+
pos = scanner.pos
|
31
|
+
elsif quoting == char
|
32
|
+
if json[pos..scanner.pos-2] =~ DATE_REGEX
|
33
|
+
# found a date, track the exact positions of the quotes so we can
|
34
|
+
# overwrite them with spaces later.
|
35
|
+
times << pos << scanner.pos
|
36
|
+
end
|
37
|
+
quoting = false
|
38
|
+
end
|
39
|
+
when ":",","
|
40
|
+
marks << scanner.pos - 1 unless quoting
|
41
|
+
when "\\"
|
42
|
+
scanner.skip(/\\/)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
if marks.empty?
|
47
|
+
json.gsub(/\\([\\\/]|u[[:xdigit:]]{4})/) do
|
48
|
+
ustr = $1
|
49
|
+
if ustr.start_with?('u')
|
50
|
+
[ustr[1..-1].to_i(16)].pack("U")
|
51
|
+
elsif ustr == '\\'
|
52
|
+
'\\\\'
|
53
|
+
else
|
54
|
+
ustr
|
55
|
+
end
|
56
|
+
end
|
57
|
+
else
|
58
|
+
left_pos = [-1].push(*marks)
|
59
|
+
right_pos = marks << scanner.pos + scanner.rest_size
|
60
|
+
output = []
|
61
|
+
left_pos.each_with_index do |left, i|
|
62
|
+
scanner.pos = left.succ
|
63
|
+
chunk = scanner.peek(right_pos[i] - scanner.pos + 1)
|
64
|
+
# overwrite the quotes found around the dates with spaces
|
65
|
+
while times.size > 0 && times[0] <= right_pos[i]
|
66
|
+
chunk[times.shift - scanner.pos - 1] = ' '
|
67
|
+
end
|
68
|
+
chunk.gsub!(/\\([\\\/]|u[[:xdigit:]]{4})/) do
|
69
|
+
ustr = $1
|
70
|
+
if ustr.start_with?('u')
|
71
|
+
[ustr[1..-1].to_i(16)].pack("U")
|
72
|
+
elsif ustr == '\\'
|
73
|
+
'\\\\'
|
74
|
+
else
|
75
|
+
ustr
|
76
|
+
end
|
77
|
+
end
|
78
|
+
output << chunk
|
79
|
+
end
|
80
|
+
output = output * " "
|
81
|
+
|
82
|
+
output.gsub!(/\\\//, '/')
|
83
|
+
output
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'active_support/core_ext/module/attribute_accessors'
|
2
|
+
require 'active_support/core_ext/module/delegation'
|
3
|
+
|
4
|
+
module ActiveSupport
|
5
|
+
# Look for and parse json strings that look like ISO 8601 times.
|
6
|
+
mattr_accessor :parse_json_times
|
7
|
+
|
8
|
+
module JSON
|
9
|
+
# Listed in order of preference.
|
10
|
+
DECODERS = %w(Yajl Yaml)
|
11
|
+
|
12
|
+
class << self
|
13
|
+
attr_reader :parse_error
|
14
|
+
delegate :decode, :to => :backend
|
15
|
+
|
16
|
+
def backend
|
17
|
+
set_default_backend unless defined?(@backend)
|
18
|
+
@backend
|
19
|
+
end
|
20
|
+
|
21
|
+
def backend=(name)
|
22
|
+
if name.is_a?(Module)
|
23
|
+
@backend = name
|
24
|
+
else
|
25
|
+
require "active_support/json/backends/#{name.to_s.downcase}.rb"
|
26
|
+
@backend = ActiveSupport::JSON::Backends::const_get(name)
|
27
|
+
end
|
28
|
+
@parse_error = @backend::ParseError
|
29
|
+
end
|
30
|
+
|
31
|
+
def with_backend(name)
|
32
|
+
old_backend, self.backend = backend, name
|
33
|
+
yield
|
34
|
+
ensure
|
35
|
+
self.backend = old_backend
|
36
|
+
end
|
37
|
+
|
38
|
+
def set_default_backend
|
39
|
+
DECODERS.find do |name|
|
40
|
+
begin
|
41
|
+
self.backend = name
|
42
|
+
true
|
43
|
+
rescue LoadError
|
44
|
+
# Try next decoder.
|
45
|
+
false
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,254 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'bigdecimal'
|
3
|
+
require 'active_support/core_ext/array/wrap'
|
4
|
+
require 'active_support/core_ext/big_decimal/conversions' # for #to_s
|
5
|
+
require 'active_support/core_ext/hash/except'
|
6
|
+
require 'active_support/core_ext/hash/slice'
|
7
|
+
require 'active_support/core_ext/module/delegation'
|
8
|
+
require 'active_support/core_ext/object/instance_variables'
|
9
|
+
require 'active_support/deprecation'
|
10
|
+
|
11
|
+
require 'active_support/time'
|
12
|
+
|
13
|
+
# Hack to load json gem first so we can overwrite its to_json.
|
14
|
+
begin
|
15
|
+
require 'json'
|
16
|
+
rescue LoadError
|
17
|
+
end
|
18
|
+
|
19
|
+
module ActiveSupport
|
20
|
+
class << self
|
21
|
+
delegate :use_standard_json_time_format, :use_standard_json_time_format=,
|
22
|
+
:escape_html_entities_in_json, :escape_html_entities_in_json=,
|
23
|
+
:to => :'ActiveSupport::JSON::Encoding'
|
24
|
+
end
|
25
|
+
|
26
|
+
module JSON
|
27
|
+
# matches YAML-formatted dates
|
28
|
+
DATE_REGEX = /^(?:\d{4}-\d{2}-\d{2}|\d{4}-\d{1,2}-\d{1,2}[ \t]+\d{1,2}:\d{2}:\d{2}(\.[0-9]*)?(([ \t]*)Z|[-+]\d{2}?(:\d{2})?))$/
|
29
|
+
|
30
|
+
# Dumps object in JSON (JavaScript Object Notation). See www.json.org for more info.
|
31
|
+
def self.encode(value, options = nil)
|
32
|
+
Encoding::Encoder.new(options).encode(value)
|
33
|
+
end
|
34
|
+
|
35
|
+
module Encoding #:nodoc:
|
36
|
+
class CircularReferenceError < StandardError; end
|
37
|
+
|
38
|
+
class Encoder
|
39
|
+
attr_reader :options
|
40
|
+
|
41
|
+
def initialize(options = nil)
|
42
|
+
@options = options
|
43
|
+
@seen = []
|
44
|
+
end
|
45
|
+
|
46
|
+
def encode(value)
|
47
|
+
check_for_circular_references(value) do
|
48
|
+
value.as_json(options).encode_json(self)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def escape(string)
|
53
|
+
Encoding.escape(string)
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
def check_for_circular_references(value)
|
58
|
+
if @seen.any? { |object| object.equal?(value) }
|
59
|
+
raise CircularReferenceError, 'object references itself'
|
60
|
+
end
|
61
|
+
@seen.unshift value
|
62
|
+
yield
|
63
|
+
ensure
|
64
|
+
@seen.shift
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
|
69
|
+
ESCAPED_CHARS = {
|
70
|
+
"\x00" => '\u0000', "\x01" => '\u0001', "\x02" => '\u0002',
|
71
|
+
"\x03" => '\u0003', "\x04" => '\u0004', "\x05" => '\u0005',
|
72
|
+
"\x06" => '\u0006', "\x07" => '\u0007', "\x0B" => '\u000B',
|
73
|
+
"\x0E" => '\u000E', "\x0F" => '\u000F', "\x10" => '\u0010',
|
74
|
+
"\x11" => '\u0011', "\x12" => '\u0012', "\x13" => '\u0013',
|
75
|
+
"\x14" => '\u0014', "\x15" => '\u0015', "\x16" => '\u0016',
|
76
|
+
"\x17" => '\u0017', "\x18" => '\u0018', "\x19" => '\u0019',
|
77
|
+
"\x1A" => '\u001A', "\x1B" => '\u001B', "\x1C" => '\u001C',
|
78
|
+
"\x1D" => '\u001D', "\x1E" => '\u001E', "\x1F" => '\u001F',
|
79
|
+
"\010" => '\b',
|
80
|
+
"\f" => '\f',
|
81
|
+
"\n" => '\n',
|
82
|
+
"\r" => '\r',
|
83
|
+
"\t" => '\t',
|
84
|
+
'"' => '\"',
|
85
|
+
'\\' => '\\\\',
|
86
|
+
'>' => '\u003E',
|
87
|
+
'<' => '\u003C',
|
88
|
+
'&' => '\u0026' }
|
89
|
+
|
90
|
+
class << self
|
91
|
+
# If true, use ISO 8601 format for dates and times. Otherwise, fall back to the Active Support legacy format.
|
92
|
+
attr_accessor :use_standard_json_time_format
|
93
|
+
|
94
|
+
attr_accessor :escape_regex
|
95
|
+
attr_reader :escape_html_entities_in_json
|
96
|
+
|
97
|
+
def escape_html_entities_in_json=(value)
|
98
|
+
self.escape_regex = \
|
99
|
+
if @escape_html_entities_in_json = value
|
100
|
+
/[\x00-\x1F"\\><&]/
|
101
|
+
else
|
102
|
+
/[\x00-\x1F"\\]/
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def escape(string)
|
107
|
+
if string.respond_to?(:force_encoding)
|
108
|
+
string = string.encode(::Encoding::UTF_8, :undef => :replace).force_encoding(::Encoding::BINARY)
|
109
|
+
end
|
110
|
+
json = string.
|
111
|
+
gsub(escape_regex) { |s| ESCAPED_CHARS[s] }.
|
112
|
+
gsub(/([\xC0-\xDF][\x80-\xBF]|
|
113
|
+
[\xE0-\xEF][\x80-\xBF]{2}|
|
114
|
+
[\xF0-\xF7][\x80-\xBF]{3})+/nx) { |s|
|
115
|
+
s.unpack("U*").pack("n*").unpack("H*")[0].gsub(/.{4}/n, '\\\\u\&')
|
116
|
+
}
|
117
|
+
json = %("#{json}")
|
118
|
+
json.force_encoding(::Encoding::UTF_8) if json.respond_to?(:force_encoding)
|
119
|
+
json
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
self.use_standard_json_time_format = true
|
124
|
+
self.escape_html_entities_in_json = false
|
125
|
+
end
|
126
|
+
|
127
|
+
CircularReferenceError = Deprecation::DeprecatedConstantProxy.new('ActiveSupport::JSON::CircularReferenceError', Encoding::CircularReferenceError)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
class Object
|
132
|
+
# Dumps object in JSON (JavaScript Object Notation). See www.json.org for more info.
|
133
|
+
def to_json(options = nil)
|
134
|
+
ActiveSupport::JSON.encode(self, options)
|
135
|
+
end
|
136
|
+
|
137
|
+
def as_json(options = nil) #:nodoc:
|
138
|
+
if respond_to?(:to_hash)
|
139
|
+
to_hash
|
140
|
+
else
|
141
|
+
instance_values
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
# A string that returns itself as its JSON-encoded form.
|
147
|
+
class ActiveSupport::JSON::Variable < String
|
148
|
+
def as_json(options = nil) self end #:nodoc:
|
149
|
+
def encode_json(encoder) self end #:nodoc:
|
150
|
+
end
|
151
|
+
|
152
|
+
class TrueClass
|
153
|
+
AS_JSON = ActiveSupport::JSON::Variable.new('true').freeze
|
154
|
+
def as_json(options = nil) AS_JSON end #:nodoc:
|
155
|
+
end
|
156
|
+
|
157
|
+
class FalseClass
|
158
|
+
AS_JSON = ActiveSupport::JSON::Variable.new('false').freeze
|
159
|
+
def as_json(options = nil) AS_JSON end #:nodoc:
|
160
|
+
end
|
161
|
+
|
162
|
+
class NilClass
|
163
|
+
AS_JSON = ActiveSupport::JSON::Variable.new('null').freeze
|
164
|
+
def as_json(options = nil) AS_JSON end #:nodoc:
|
165
|
+
end
|
166
|
+
|
167
|
+
class String
|
168
|
+
def as_json(options = nil) self end #:nodoc:
|
169
|
+
def encode_json(encoder) encoder.escape(self) end #:nodoc:
|
170
|
+
end
|
171
|
+
|
172
|
+
class Symbol
|
173
|
+
def as_json(options = nil) to_s end #:nodoc:
|
174
|
+
end
|
175
|
+
|
176
|
+
class Numeric
|
177
|
+
def as_json(options = nil) self end #:nodoc:
|
178
|
+
def encode_json(encoder) to_s end #:nodoc:
|
179
|
+
end
|
180
|
+
|
181
|
+
class BigDecimal
|
182
|
+
# A BigDecimal would be naturally represented as a JSON number. Most libraries,
|
183
|
+
# however, parse non-integer JSON numbers directly as floats. Clients using
|
184
|
+
# those libraries would get in general a wrong number and no way to recover
|
185
|
+
# other than manually inspecting the string with the JSON code itself.
|
186
|
+
#
|
187
|
+
# That's why a JSON string is returned. The JSON literal is not numeric, but if
|
188
|
+
# the other end knows by contract that the data is supposed to be a BigDecimal,
|
189
|
+
# it still has the chance to post-process the string and get the real value.
|
190
|
+
def as_json(options = nil) to_s end #:nodoc:
|
191
|
+
end
|
192
|
+
|
193
|
+
class Regexp
|
194
|
+
def as_json(options = nil) to_s end #:nodoc:
|
195
|
+
end
|
196
|
+
|
197
|
+
module Enumerable
|
198
|
+
def as_json(options = nil) to_a end #:nodoc:
|
199
|
+
end
|
200
|
+
|
201
|
+
class Array
|
202
|
+
def as_json(options = nil) self end #:nodoc:
|
203
|
+
def encode_json(encoder) "[#{map { |v| encoder.encode(v) } * ','}]" end #:nodoc:
|
204
|
+
end
|
205
|
+
|
206
|
+
class Hash
|
207
|
+
def as_json(options = nil) #:nodoc:
|
208
|
+
if options
|
209
|
+
if attrs = options[:only]
|
210
|
+
slice(*Array.wrap(attrs))
|
211
|
+
elsif attrs = options[:except]
|
212
|
+
except(*Array.wrap(attrs))
|
213
|
+
else
|
214
|
+
self
|
215
|
+
end
|
216
|
+
else
|
217
|
+
self
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
def encode_json(encoder)
|
222
|
+
"{#{map { |k,v| "#{encoder.encode(k.to_s)}:#{encoder.encode(v)}" } * ','}}"
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
class Time
|
227
|
+
def as_json(options = nil) #:nodoc:
|
228
|
+
if ActiveSupport.use_standard_json_time_format
|
229
|
+
xmlschema
|
230
|
+
else
|
231
|
+
%(#{strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)})
|
232
|
+
end
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
class Date
|
237
|
+
def as_json(options = nil) #:nodoc:
|
238
|
+
if ActiveSupport.use_standard_json_time_format
|
239
|
+
strftime("%Y-%m-%d")
|
240
|
+
else
|
241
|
+
strftime("%Y/%m/%d")
|
242
|
+
end
|
243
|
+
end
|
244
|
+
end
|
245
|
+
|
246
|
+
class DateTime
|
247
|
+
def as_json(options = nil) #:nodoc:
|
248
|
+
if ActiveSupport.use_standard_json_time_format
|
249
|
+
xmlschema
|
250
|
+
else
|
251
|
+
strftime('%Y/%m/%d %H:%M:%S %z')
|
252
|
+
end
|
253
|
+
end
|
254
|
+
end
|