engineyard-serverside 2.0.5 → 2.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (146) hide show
  1. data/lib/engineyard-serverside.rb +3 -3
  2. data/lib/engineyard-serverside/configuration.rb +6 -6
  3. data/lib/engineyard-serverside/deploy.rb +11 -7
  4. data/lib/engineyard-serverside/version.rb +1 -1
  5. data/lib/vendor/multi_json/CHANGELOG.md +121 -0
  6. data/lib/vendor/multi_json/CONTRIBUTING.md +46 -0
  7. data/lib/vendor/multi_json/Gemfile +31 -0
  8. data/lib/vendor/multi_json/LICENSE.md +20 -0
  9. data/lib/vendor/multi_json/README.md +105 -0
  10. data/lib/vendor/multi_json/Rakefile +12 -0
  11. data/lib/vendor/multi_json/lib/multi_json.rb +137 -0
  12. data/lib/vendor/multi_json/lib/multi_json/adapters/gson.rb +20 -0
  13. data/lib/vendor/multi_json/lib/multi_json/adapters/json_common.rb +35 -0
  14. data/lib/vendor/multi_json/lib/multi_json/adapters/json_gem.rb +12 -0
  15. data/lib/vendor/multi_json/lib/multi_json/adapters/json_pure.rb +12 -0
  16. data/lib/vendor/multi_json/lib/multi_json/adapters/nsjsonserialization.rb +35 -0
  17. data/lib/vendor/multi_json/lib/multi_json/adapters/oj.rb +29 -0
  18. data/lib/vendor/multi_json/lib/multi_json/adapters/ok_json.rb +58 -0
  19. data/lib/vendor/multi_json/lib/multi_json/adapters/yajl.rb +20 -0
  20. data/lib/vendor/multi_json/lib/multi_json/vendor/okjson.rb +602 -0
  21. data/lib/vendor/multi_json/lib/multi_json/version.rb +3 -0
  22. data/lib/vendor/multi_json/multi_json.gemspec +22 -0
  23. data/lib/vendor/multi_json/spec/adapter_shared_example.rb +162 -0
  24. data/lib/vendor/multi_json/spec/helper.rb +45 -0
  25. data/lib/vendor/multi_json/spec/json_common_shared_example.rb +36 -0
  26. data/lib/vendor/multi_json/spec/multi_json_spec.rb +151 -0
  27. data/spec/configuration_spec.rb +79 -1
  28. data/spec/deploy_hook_spec.rb +2 -2
  29. data/spec/spec_helper.rb +1 -1
  30. metadata +26 -120
  31. data/lib/vendor/json_pure/CHANGES +0 -166
  32. data/lib/vendor/json_pure/COPYING +0 -58
  33. data/lib/vendor/json_pure/GPL +0 -340
  34. data/lib/vendor/json_pure/README +0 -358
  35. data/lib/vendor/json_pure/Rakefile +0 -292
  36. data/lib/vendor/json_pure/TODO +0 -1
  37. data/lib/vendor/json_pure/VERSION +0 -1
  38. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkComparison.log +0 -52
  39. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast-autocorrelation.dat +0 -1000
  40. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast.dat +0 -1001
  41. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty-autocorrelation.dat +0 -900
  42. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty.dat +0 -901
  43. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe-autocorrelation.dat +0 -1000
  44. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe.dat +0 -1001
  45. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt.log +0 -261
  46. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast-autocorrelation.dat +0 -1000
  47. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast.dat +0 -1001
  48. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty-autocorrelation.dat +0 -1000
  49. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty.dat +0 -1001
  50. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe-autocorrelation.dat +0 -1000
  51. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe.dat +0 -1001
  52. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure.log +0 -262
  53. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator-autocorrelation.dat +0 -1000
  54. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator.dat +0 -1001
  55. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails.log +0 -82
  56. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkComparison.log +0 -34
  57. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser-autocorrelation.dat +0 -900
  58. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser.dat +0 -901
  59. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt.log +0 -81
  60. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser-autocorrelation.dat +0 -1000
  61. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser.dat +0 -1001
  62. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure.log +0 -82
  63. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser-autocorrelation.dat +0 -1000
  64. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser.dat +0 -1001
  65. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails.log +0 -82
  66. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser-autocorrelation.dat +0 -1000
  67. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser.dat +0 -1001
  68. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML.log +0 -82
  69. data/lib/vendor/json_pure/benchmarks/generator2_benchmark.rb +0 -222
  70. data/lib/vendor/json_pure/benchmarks/generator_benchmark.rb +0 -224
  71. data/lib/vendor/json_pure/benchmarks/ohai.json +0 -1216
  72. data/lib/vendor/json_pure/benchmarks/ohai.ruby +0 -1
  73. data/lib/vendor/json_pure/benchmarks/parser2_benchmark.rb +0 -251
  74. data/lib/vendor/json_pure/benchmarks/parser_benchmark.rb +0 -259
  75. data/lib/vendor/json_pure/bin/edit_json.rb +0 -9
  76. data/lib/vendor/json_pure/bin/prettify_json.rb +0 -75
  77. data/lib/vendor/json_pure/data/example.json +0 -1
  78. data/lib/vendor/json_pure/data/index.html +0 -38
  79. data/lib/vendor/json_pure/data/prototype.js +0 -4184
  80. data/lib/vendor/json_pure/ext/json/ext/generator/extconf.rb +0 -16
  81. data/lib/vendor/json_pure/ext/json/ext/generator/generator.c +0 -1323
  82. data/lib/vendor/json_pure/ext/json/ext/generator/generator.h +0 -170
  83. data/lib/vendor/json_pure/ext/json/ext/parser/extconf.rb +0 -15
  84. data/lib/vendor/json_pure/ext/json/ext/parser/parser.c +0 -1935
  85. data/lib/vendor/json_pure/ext/json/ext/parser/parser.h +0 -71
  86. data/lib/vendor/json_pure/ext/json/ext/parser/parser.rl +0 -792
  87. data/lib/vendor/json_pure/install.rb +0 -26
  88. data/lib/vendor/json_pure/lib/json.rb +0 -10
  89. data/lib/vendor/json_pure/lib/json/Array.xpm +0 -21
  90. data/lib/vendor/json_pure/lib/json/FalseClass.xpm +0 -21
  91. data/lib/vendor/json_pure/lib/json/Hash.xpm +0 -21
  92. data/lib/vendor/json_pure/lib/json/Key.xpm +0 -73
  93. data/lib/vendor/json_pure/lib/json/NilClass.xpm +0 -21
  94. data/lib/vendor/json_pure/lib/json/Numeric.xpm +0 -28
  95. data/lib/vendor/json_pure/lib/json/String.xpm +0 -96
  96. data/lib/vendor/json_pure/lib/json/TrueClass.xpm +0 -21
  97. data/lib/vendor/json_pure/lib/json/add/core.rb +0 -148
  98. data/lib/vendor/json_pure/lib/json/add/rails.rb +0 -58
  99. data/lib/vendor/json_pure/lib/json/common.rb +0 -397
  100. data/lib/vendor/json_pure/lib/json/editor.rb +0 -1371
  101. data/lib/vendor/json_pure/lib/json/ext.rb +0 -15
  102. data/lib/vendor/json_pure/lib/json/json.xpm +0 -1499
  103. data/lib/vendor/json_pure/lib/json/pure.rb +0 -77
  104. data/lib/vendor/json_pure/lib/json/pure/generator.rb +0 -452
  105. data/lib/vendor/json_pure/lib/json/pure/parser.rb +0 -307
  106. data/lib/vendor/json_pure/lib/json/version.rb +0 -8
  107. data/lib/vendor/json_pure/tests/fixtures/fail1.json +0 -1
  108. data/lib/vendor/json_pure/tests/fixtures/fail10.json +0 -1
  109. data/lib/vendor/json_pure/tests/fixtures/fail11.json +0 -1
  110. data/lib/vendor/json_pure/tests/fixtures/fail12.json +0 -1
  111. data/lib/vendor/json_pure/tests/fixtures/fail13.json +0 -1
  112. data/lib/vendor/json_pure/tests/fixtures/fail14.json +0 -1
  113. data/lib/vendor/json_pure/tests/fixtures/fail18.json +0 -1
  114. data/lib/vendor/json_pure/tests/fixtures/fail19.json +0 -1
  115. data/lib/vendor/json_pure/tests/fixtures/fail2.json +0 -1
  116. data/lib/vendor/json_pure/tests/fixtures/fail20.json +0 -1
  117. data/lib/vendor/json_pure/tests/fixtures/fail21.json +0 -1
  118. data/lib/vendor/json_pure/tests/fixtures/fail22.json +0 -1
  119. data/lib/vendor/json_pure/tests/fixtures/fail23.json +0 -1
  120. data/lib/vendor/json_pure/tests/fixtures/fail24.json +0 -1
  121. data/lib/vendor/json_pure/tests/fixtures/fail25.json +0 -1
  122. data/lib/vendor/json_pure/tests/fixtures/fail27.json +0 -2
  123. data/lib/vendor/json_pure/tests/fixtures/fail28.json +0 -2
  124. data/lib/vendor/json_pure/tests/fixtures/fail3.json +0 -1
  125. data/lib/vendor/json_pure/tests/fixtures/fail4.json +0 -1
  126. data/lib/vendor/json_pure/tests/fixtures/fail5.json +0 -1
  127. data/lib/vendor/json_pure/tests/fixtures/fail6.json +0 -1
  128. data/lib/vendor/json_pure/tests/fixtures/fail7.json +0 -1
  129. data/lib/vendor/json_pure/tests/fixtures/fail8.json +0 -1
  130. data/lib/vendor/json_pure/tests/fixtures/fail9.json +0 -1
  131. data/lib/vendor/json_pure/tests/fixtures/pass1.json +0 -56
  132. data/lib/vendor/json_pure/tests/fixtures/pass15.json +0 -1
  133. data/lib/vendor/json_pure/tests/fixtures/pass16.json +0 -1
  134. data/lib/vendor/json_pure/tests/fixtures/pass17.json +0 -1
  135. data/lib/vendor/json_pure/tests/fixtures/pass2.json +0 -1
  136. data/lib/vendor/json_pure/tests/fixtures/pass26.json +0 -1
  137. data/lib/vendor/json_pure/tests/fixtures/pass3.json +0 -6
  138. data/lib/vendor/json_pure/tests/test_json.rb +0 -361
  139. data/lib/vendor/json_pure/tests/test_json_addition.rb +0 -162
  140. data/lib/vendor/json_pure/tests/test_json_encoding.rb +0 -68
  141. data/lib/vendor/json_pure/tests/test_json_fixtures.rb +0 -34
  142. data/lib/vendor/json_pure/tests/test_json_generate.rb +0 -122
  143. data/lib/vendor/json_pure/tests/test_json_rails.rb +0 -144
  144. data/lib/vendor/json_pure/tests/test_json_unicode.rb +0 -76
  145. data/lib/vendor/json_pure/tools/fuzz.rb +0 -139
  146. data/lib/vendor/json_pure/tools/server.rb +0 -61
@@ -0,0 +1,12 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ desc "Run all examples"
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task :default => :spec
9
+ task :test => :spec
10
+
11
+ require 'yard'
12
+ YARD::Rake::YardocTask.new
@@ -0,0 +1,137 @@
1
+ module MultiJson
2
+ extend self
3
+
4
+ class LoadError < StandardError
5
+ attr_reader :data
6
+ def initialize(message='', backtrace=[], data='')
7
+ super(message)
8
+ self.set_backtrace(backtrace)
9
+ @data = data
10
+ end
11
+ end
12
+ DecodeError = LoadError # Legacy support
13
+
14
+
15
+ @default_options = {}
16
+ attr_accessor :default_options
17
+
18
+ REQUIREMENT_MAP = [
19
+ ['oj', :oj],
20
+ ['yajl', :yajl],
21
+ ['json', :json_gem],
22
+ ['gson', :gson],
23
+ ['json/pure', :json_pure]
24
+ ]
25
+
26
+ # The default adapter based on what you currently
27
+ # have loaded and installed. First checks to see
28
+ # if any adapters are already loaded, then checks
29
+ # to see which are installed if none are loaded.
30
+ def default_adapter
31
+ return :oj if defined?(::Oj)
32
+ return :yajl if defined?(::Yajl)
33
+ return :json_gem if defined?(::JSON)
34
+ return :gson if defined?(::Gson)
35
+
36
+ REQUIREMENT_MAP.each do |(library, adapter)|
37
+ begin
38
+ require library
39
+ return adapter
40
+ rescue ::LoadError
41
+ next
42
+ end
43
+ end
44
+
45
+ Kernel.warn '[WARNING] MultiJson is using the default adapter (ok_json). We recommend loading a different JSON library to improve performance.'
46
+ :ok_json
47
+ end
48
+ # :nodoc:
49
+ alias :default_engine :default_adapter
50
+
51
+ # Get the current adapter class.
52
+ def adapter
53
+ return @adapter if defined?(@adapter) && @adapter
54
+
55
+ self.use nil # load default adapter
56
+
57
+ @adapter
58
+ end
59
+ # :nodoc:
60
+ alias :engine :adapter
61
+
62
+ # Set the JSON parser utilizing a symbol, string, or class.
63
+ # Supported by default are:
64
+ #
65
+ # * <tt>:oj</tt>
66
+ # * <tt>:json_gem</tt>
67
+ # * <tt>:json_pure</tt>
68
+ # * <tt>:ok_json</tt>
69
+ # * <tt>:yajl</tt>
70
+ # * <tt>:nsjsonserialization</tt> (MacRuby only)
71
+ # * <tt>:gson</tt> (JRuby only)
72
+ def use(new_adapter)
73
+ @adapter = load_adapter(new_adapter)
74
+ end
75
+ alias :adapter= :use
76
+ # :nodoc:
77
+ alias :engine= :use
78
+
79
+ def load_adapter(new_adapter)
80
+ case new_adapter
81
+ when String, Symbol
82
+ require "multi_json/adapters/#{new_adapter}"
83
+ MultiJson::Adapters.const_get(:"#{new_adapter.to_s.split('_').map{|s| s.capitalize}.join('')}")
84
+ when NilClass, FalseClass
85
+ load_adapter default_adapter
86
+ when Class, Module
87
+ new_adapter
88
+ else
89
+ raise "Did not recognize your adapter specification. Please specify either a symbol or a class."
90
+ end
91
+ end
92
+
93
+ # Decode a JSON string into Ruby.
94
+ #
95
+ # <b>Options</b>
96
+ #
97
+ # <tt>:symbolize_keys</tt> :: If true, will use symbols instead of strings for the keys.
98
+ # <tt>:adapter</tt> :: If set, the selected engine will be used just for the call.
99
+ def load(string, options={})
100
+ adapter = current_adapter(options)
101
+ begin
102
+ adapter.load(string, options)
103
+ rescue adapter::ParseError => exception
104
+ raise LoadError.new(exception.message, exception.backtrace, string)
105
+ end
106
+ end
107
+ # :nodoc:
108
+ alias :decode :load
109
+
110
+ def current_adapter(options)
111
+ if new_adapter = (options || {}).delete(:adapter)
112
+ load_adapter(new_adapter)
113
+ else
114
+ adapter
115
+ end
116
+ end
117
+
118
+ # Encodes a Ruby object as JSON.
119
+ def dump(object, options={})
120
+ options = default_options.merge(options)
121
+ adapter = current_adapter(options)
122
+ adapter.dump(object, options)
123
+ end
124
+ # :nodoc:
125
+ alias :encode :dump
126
+
127
+ # Executes passed block using specified adapter.
128
+ def with_adapter(new_adapter)
129
+ old_adapter, self.adapter = adapter, new_adapter
130
+ yield
131
+ ensure
132
+ self.adapter = old_adapter
133
+ end
134
+ # :nodoc:
135
+ alias :with_engine :with_adapter
136
+
137
+ end
@@ -0,0 +1,20 @@
1
+ require 'gson' unless defined?(::Gson)
2
+
3
+ module MultiJson
4
+ module Adapters
5
+ # Use the gson.rb library to dump/load.
6
+ module Gson
7
+ extend self
8
+
9
+ ParseError = ::Gson::DecodeError
10
+
11
+ def load(string, options={}) #:nodoc:
12
+ ::Gson::Decoder.new(options).decode(string)
13
+ end
14
+
15
+ def dump(object, options={}) #:nodoc:
16
+ ::Gson::Encoder.new(options).encode(object)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,35 @@
1
+ module MultiJson
2
+ module Adapters
3
+ module JsonCommon
4
+ def load(string, options={})
5
+ string = string.read if string.respond_to?(:read)
6
+ ::JSON.parse("[#{string}]", process_load_options!(options)).first
7
+ end
8
+
9
+ def dump(object, options={})
10
+ object.to_json(process_dump_options!(options))
11
+ end
12
+
13
+ protected
14
+
15
+ def process_load_options!(options={})
16
+ process_options!({:create_additions => false}, options) do |opts|
17
+ opts.merge!(:symbolize_names => true) if options.delete(:symbolize_keys)
18
+ end
19
+ end
20
+
21
+ def process_dump_options!(options={})
22
+ process_options!({}, options) do |opts|
23
+ opts.merge!(::JSON::PRETTY_STATE_PROTOTYPE.to_h) if options.delete(:pretty)
24
+ end
25
+ end
26
+
27
+ def process_options!(default_options, options)
28
+ return default_options if options.empty?
29
+ yield default_options
30
+ default_options.merge!(options)
31
+ end
32
+
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,12 @@
1
+ require 'json' unless defined?(::JSON)
2
+ require 'multi_json/adapters/json_common'
3
+
4
+ module MultiJson
5
+ module Adapters
6
+ # Use the JSON gem to dump/load.
7
+ module JsonGem
8
+ ParseError = ::JSON::ParserError
9
+ extend JsonCommon
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ require 'json/pure' unless defined?(::JSON)
2
+ require 'multi_json/adapters/json_common'
3
+
4
+ module MultiJson
5
+ module Adapters
6
+ # Use JSON pure to dump/load.
7
+ module JsonPure
8
+ ParseError = ::JSON::ParserError
9
+ extend JsonCommon
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,35 @@
1
+ framework 'Foundation'
2
+ require 'multi_json/adapters/ok_json'
3
+
4
+ module MultiJson
5
+ module Adapters
6
+ module Nsjsonserialization
7
+ extend self, MultiJson::Adapters::OkJson
8
+ ParseError = ::MultiJson::OkJson::Error
9
+
10
+ def load(string, options={})
11
+ string = string.read if string.respond_to?(:read)
12
+ data = string.dataUsingEncoding(NSUTF8StringEncoding)
13
+ object = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves, error: nil)
14
+ if object
15
+ object = symbolize_keys(object) if options[:symbolize_keys]
16
+ object
17
+ else
18
+ super(string, options={})
19
+ end
20
+ end
21
+
22
+ def dump(object, options={})
23
+ pretty = options[:pretty] ? NSJSONWritingPrettyPrinted : 0
24
+ object = object.as_json if object.respond_to?(:as_json)
25
+ if NSJSONSerialization.isValidJSONObject(object)
26
+ data = NSJSONSerialization.dataWithJSONObject(object, options: pretty, error: nil)
27
+ NSMutableString.alloc.initWithData(data, encoding: NSUTF8StringEncoding)
28
+ else
29
+ super(object, options)
30
+ end
31
+ end
32
+
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,29 @@
1
+ require 'oj' unless defined?(::Oj)
2
+
3
+ module MultiJson
4
+ module Adapters
5
+ # Use the Oj library to dump/load.
6
+ module Oj
7
+ extend self
8
+
9
+ DEFAULT_OPTIONS = {:mode => :compat, :time_format => :ruby}.freeze
10
+
11
+ ParseError = if defined?(::Oj::ParseError)
12
+ ::Oj::ParseError
13
+ else
14
+ SyntaxError
15
+ end
16
+
17
+ def load(string, options={}) #:nodoc:
18
+ options.merge!(:symbol_keys => options[:symbolize_keys])
19
+ options[:mode] = :strict
20
+ ::Oj.load(string, DEFAULT_OPTIONS.merge(options))
21
+ end
22
+
23
+ def dump(object, options={}) #:nodoc:
24
+ options.merge!(:indent => 2) if options[:pretty]
25
+ ::Oj.dump(object, DEFAULT_OPTIONS.merge(options))
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,58 @@
1
+ require 'multi_json/vendor/okjson'
2
+
3
+ module MultiJson
4
+ module Adapters
5
+ module OkJson
6
+ extend self
7
+
8
+ ParseError = ::MultiJson::OkJson::Error
9
+
10
+ def load(string, options={}) #:nodoc:
11
+ string = string.read if string.respond_to?(:read)
12
+ result = ::MultiJson::OkJson.decode("[#{string}]").first
13
+ options[:symbolize_keys] ? symbolize_keys(result) : result
14
+ end
15
+
16
+ def dump(object, options={}) #:nodoc:
17
+ ::MultiJson::OkJson.valenc(stringify_keys(object))
18
+ end
19
+
20
+ def symbolize_keys(object) #:nodoc:
21
+ prepare_object(object) do |key|
22
+ key.is_a?(String) ? key.to_sym : key
23
+ end
24
+ end
25
+
26
+ def stringify_keys(object) #:nodoc:
27
+ prepare_object(object) do |key|
28
+ key.respond_to?(:to_s) ? key.to_s : key
29
+ end
30
+ end
31
+
32
+ def prepare_object(object, &key_modifier) #:nodoc:
33
+ case object
34
+ when Array
35
+ object.map do |value|
36
+ prepare_object(value, &key_modifier)
37
+ end
38
+ when Hash
39
+ object.inject({}) do |result, (key, value)|
40
+ new_key = key_modifier.call(key)
41
+ new_value = prepare_object(value, &key_modifier)
42
+ result.merge! new_key => new_value
43
+ end
44
+ when String, Numeric, true, false, nil
45
+ object
46
+ else
47
+ if object.respond_to?(:to_json)
48
+ object
49
+ elsif object.respond_to?(:to_s)
50
+ object.to_s
51
+ else
52
+ object
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,20 @@
1
+ require 'yajl' unless defined?(::Yajl)
2
+
3
+ module MultiJson
4
+ module Adapters
5
+ # Use the Yajl-Ruby library to dump/load.
6
+ module Yajl
7
+ extend self
8
+
9
+ ParseError = ::Yajl::ParseError
10
+
11
+ def load(string, options={}) #:nodoc:
12
+ ::Yajl::Parser.new(:symbolize_keys => options[:symbolize_keys]).parse(string)
13
+ end
14
+
15
+ def dump(object, options={}) #:nodoc:
16
+ ::Yajl::Encoder.encode(object, options)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,602 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Copyright 2011, 2012 Keith Rarick
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ # See https://github.com/kr/okjson for updates.
24
+
25
+ require 'stringio'
26
+
27
+ # Some parts adapted from
28
+ # http://golang.org/src/pkg/json/decode.go and
29
+ # http://golang.org/src/pkg/utf8/utf8.go
30
+ module MultiJson
31
+ module OkJson
32
+ extend self
33
+
34
+
35
+ # Decodes a json document in string s and
36
+ # returns the corresponding ruby value.
37
+ # String s must be valid UTF-8. If you have
38
+ # a string in some other encoding, convert
39
+ # it first.
40
+ #
41
+ # String values in the resulting structure
42
+ # will be UTF-8.
43
+ def decode(s)
44
+ ts = lex(s)
45
+ v, ts = textparse(ts)
46
+ if ts.length > 0
47
+ raise Error, 'trailing garbage'
48
+ end
49
+ v
50
+ end
51
+
52
+
53
+ # Parses a "json text" in the sense of RFC 4627.
54
+ # Returns the parsed value and any trailing tokens.
55
+ # Note: this is almost the same as valparse,
56
+ # except that it does not accept atomic values.
57
+ def textparse(ts)
58
+ if ts.length < 0
59
+ raise Error, 'empty'
60
+ end
61
+
62
+ typ, _, val = ts[0]
63
+ case typ
64
+ when '{' then objparse(ts)
65
+ when '[' then arrparse(ts)
66
+ else
67
+ raise Error, "unexpected #{val.inspect}"
68
+ end
69
+ end
70
+
71
+
72
+ # Parses a "value" in the sense of RFC 4627.
73
+ # Returns the parsed value and any trailing tokens.
74
+ def valparse(ts)
75
+ if ts.length < 0
76
+ raise Error, 'empty'
77
+ end
78
+
79
+ typ, _, val = ts[0]
80
+ case typ
81
+ when '{' then objparse(ts)
82
+ when '[' then arrparse(ts)
83
+ when :val,:str then [val, ts[1..-1]]
84
+ else
85
+ raise Error, "unexpected #{val.inspect}"
86
+ end
87
+ end
88
+
89
+
90
+ # Parses an "object" in the sense of RFC 4627.
91
+ # Returns the parsed value and any trailing tokens.
92
+ def objparse(ts)
93
+ ts = eat('{', ts)
94
+ obj = {}
95
+
96
+ if ts[0][0] == '}'
97
+ return obj, ts[1..-1]
98
+ end
99
+
100
+ k, v, ts = pairparse(ts)
101
+ obj[k] = v
102
+
103
+ if ts[0][0] == '}'
104
+ return obj, ts[1..-1]
105
+ end
106
+
107
+ loop do
108
+ ts = eat(',', ts)
109
+
110
+ k, v, ts = pairparse(ts)
111
+ obj[k] = v
112
+
113
+ if ts[0][0] == '}'
114
+ return obj, ts[1..-1]
115
+ end
116
+ end
117
+ end
118
+
119
+
120
+ # Parses a "member" in the sense of RFC 4627.
121
+ # Returns the parsed values and any trailing tokens.
122
+ def pairparse(ts)
123
+ (typ, _, k), ts = ts[0], ts[1..-1]
124
+ if typ != :str
125
+ raise Error, "unexpected #{k.inspect}"
126
+ end
127
+ ts = eat(':', ts)
128
+ v, ts = valparse(ts)
129
+ [k, v, ts]
130
+ end
131
+
132
+
133
+ # Parses an "array" in the sense of RFC 4627.
134
+ # Returns the parsed value and any trailing tokens.
135
+ def arrparse(ts)
136
+ ts = eat('[', ts)
137
+ arr = []
138
+
139
+ if ts[0][0] == ']'
140
+ return arr, ts[1..-1]
141
+ end
142
+
143
+ v, ts = valparse(ts)
144
+ arr << v
145
+
146
+ if ts[0][0] == ']'
147
+ return arr, ts[1..-1]
148
+ end
149
+
150
+ loop do
151
+ ts = eat(',', ts)
152
+
153
+ v, ts = valparse(ts)
154
+ arr << v
155
+
156
+ if ts[0][0] == ']'
157
+ return arr, ts[1..-1]
158
+ end
159
+ end
160
+ end
161
+
162
+
163
+ def eat(typ, ts)
164
+ if ts[0][0] != typ
165
+ raise Error, "expected #{typ} (got #{ts[0].inspect})"
166
+ end
167
+ ts[1..-1]
168
+ end
169
+
170
+
171
+ # Scans s and returns a list of json tokens,
172
+ # excluding white space (as defined in RFC 4627).
173
+ def lex(s)
174
+ ts = []
175
+ while s.length > 0
176
+ typ, lexeme, val = tok(s)
177
+ if typ == nil
178
+ raise Error, "invalid character at #{s[0,10].inspect}"
179
+ end
180
+ if typ != :space
181
+ ts << [typ, lexeme, val]
182
+ end
183
+ s = s[lexeme.length..-1]
184
+ end
185
+ ts
186
+ end
187
+
188
+
189
+ # Scans the first token in s and
190
+ # returns a 3-element list, or nil
191
+ # if s does not begin with a valid token.
192
+ #
193
+ # The first list element is one of
194
+ # '{', '}', ':', ',', '[', ']',
195
+ # :val, :str, and :space.
196
+ #
197
+ # The second element is the lexeme.
198
+ #
199
+ # The third element is the value of the
200
+ # token for :val and :str, otherwise
201
+ # it is the lexeme.
202
+ def tok(s)
203
+ case s[0]
204
+ when ?{ then ['{', s[0,1], s[0,1]]
205
+ when ?} then ['}', s[0,1], s[0,1]]
206
+ when ?: then [':', s[0,1], s[0,1]]
207
+ when ?, then [',', s[0,1], s[0,1]]
208
+ when ?[ then ['[', s[0,1], s[0,1]]
209
+ when ?] then [']', s[0,1], s[0,1]]
210
+ when ?n then nulltok(s)
211
+ when ?t then truetok(s)
212
+ when ?f then falsetok(s)
213
+ when ?" then strtok(s)
214
+ when Spc then [:space, s[0,1], s[0,1]]
215
+ when ?\t then [:space, s[0,1], s[0,1]]
216
+ when ?\n then [:space, s[0,1], s[0,1]]
217
+ when ?\r then [:space, s[0,1], s[0,1]]
218
+ else numtok(s)
219
+ end
220
+ end
221
+
222
+
223
+ def nulltok(s); s[0,4] == 'null' ? [:val, 'null', nil] : [] end
224
+ def truetok(s); s[0,4] == 'true' ? [:val, 'true', true] : [] end
225
+ def falsetok(s); s[0,5] == 'false' ? [:val, 'false', false] : [] end
226
+
227
+
228
+ def numtok(s)
229
+ m = /-?([1-9][0-9]+|[0-9])([.][0-9]+)?([eE][+-]?[0-9]+)?/.match(s)
230
+ if m && m.begin(0) == 0
231
+ if m[3] && !m[2]
232
+ [:val, m[0], Integer(m[1])*(10**Integer(m[3][1..-1]))]
233
+ elsif m[2]
234
+ [:val, m[0], Float(m[0])]
235
+ else
236
+ [:val, m[0], Integer(m[0])]
237
+ end
238
+ else
239
+ []
240
+ end
241
+ end
242
+
243
+
244
+ def strtok(s)
245
+ m = /"([^"\\]|\\["\/\\bfnrt]|\\u[0-9a-fA-F]{4})*"/.match(s)
246
+ if ! m
247
+ raise Error, "invalid string literal at #{abbrev(s)}"
248
+ end
249
+ [:str, m[0], unquote(m[0])]
250
+ end
251
+
252
+
253
+ def abbrev(s)
254
+ t = s[0,10]
255
+ p = t['`']
256
+ t = t[0,p] if p
257
+ t = t + '...' if t.length < s.length
258
+ '`' + t + '`'
259
+ end
260
+
261
+
262
+ # Converts a quoted json string literal q into a UTF-8-encoded string.
263
+ # The rules are different than for Ruby, so we cannot use eval.
264
+ # Unquote will raise an error if q contains control characters.
265
+ def unquote(q)
266
+ q = q[1...-1]
267
+ a = q.dup # allocate a big enough string
268
+ rubydoesenc = false
269
+ # In ruby >= 1.9, a[w] is a codepoint, not a byte.
270
+ if a.class.method_defined?(:force_encoding)
271
+ a.force_encoding('UTF-8')
272
+ rubydoesenc = true
273
+ end
274
+ r, w = 0, 0
275
+ while r < q.length
276
+ c = q[r]
277
+ case true
278
+ when c == ?\\
279
+ r += 1
280
+ if r >= q.length
281
+ raise Error, "string literal ends with a \"\\\": \"#{q}\""
282
+ end
283
+
284
+ case q[r]
285
+ when ?",?\\,?/,?'
286
+ a[w] = q[r]
287
+ r += 1
288
+ w += 1
289
+ when ?b,?f,?n,?r,?t
290
+ a[w] = Unesc[q[r]]
291
+ r += 1
292
+ w += 1
293
+ when ?u
294
+ r += 1
295
+ uchar = begin
296
+ hexdec4(q[r,4])
297
+ rescue RuntimeError => e
298
+ raise Error, "invalid escape sequence \\u#{q[r,4]}: #{e}"
299
+ end
300
+ r += 4
301
+ if surrogate? uchar
302
+ if q.length >= r+6
303
+ uchar1 = hexdec4(q[r+2,4])
304
+ uchar = subst(uchar, uchar1)
305
+ if uchar != Ucharerr
306
+ # A valid pair; consume.
307
+ r += 6
308
+ end
309
+ end
310
+ end
311
+ if rubydoesenc
312
+ a[w] = '' << uchar
313
+ w += 1
314
+ else
315
+ w += ucharenc(a, w, uchar)
316
+ end
317
+ else
318
+ raise Error, "invalid escape char #{q[r]} in \"#{q}\""
319
+ end
320
+ when c == ?", c < Spc
321
+ raise Error, "invalid character in string literal \"#{q}\""
322
+ else
323
+ # Copy anything else byte-for-byte.
324
+ # Valid UTF-8 will remain valid UTF-8.
325
+ # Invalid UTF-8 will remain invalid UTF-8.
326
+ # In ruby >= 1.9, c is a codepoint, not a byte,
327
+ # in which case this is still what we want.
328
+ a[w] = c
329
+ r += 1
330
+ w += 1
331
+ end
332
+ end
333
+ a[0,w]
334
+ end
335
+
336
+
337
+ # Encodes unicode character u as UTF-8
338
+ # bytes in string a at position i.
339
+ # Returns the number of bytes written.
340
+ def ucharenc(a, i, u)
341
+ case true
342
+ when u <= Uchar1max
343
+ a[i] = (u & 0xff).chr
344
+ 1
345
+ when u <= Uchar2max
346
+ a[i+0] = (Utag2 | ((u>>6)&0xff)).chr
347
+ a[i+1] = (Utagx | (u&Umaskx)).chr
348
+ 2
349
+ when u <= Uchar3max
350
+ a[i+0] = (Utag3 | ((u>>12)&0xff)).chr
351
+ a[i+1] = (Utagx | ((u>>6)&Umaskx)).chr
352
+ a[i+2] = (Utagx | (u&Umaskx)).chr
353
+ 3
354
+ else
355
+ a[i+0] = (Utag4 | ((u>>18)&0xff)).chr
356
+ a[i+1] = (Utagx | ((u>>12)&Umaskx)).chr
357
+ a[i+2] = (Utagx | ((u>>6)&Umaskx)).chr
358
+ a[i+3] = (Utagx | (u&Umaskx)).chr
359
+ 4
360
+ end
361
+ end
362
+
363
+
364
+ def hexdec4(s)
365
+ if s.length != 4
366
+ raise Error, 'short'
367
+ end
368
+ (nibble(s[0])<<12) | (nibble(s[1])<<8) | (nibble(s[2])<<4) | nibble(s[3])
369
+ end
370
+
371
+
372
+ def subst(u1, u2)
373
+ if Usurr1 <= u1 && u1 < Usurr2 && Usurr2 <= u2 && u2 < Usurr3
374
+ return ((u1-Usurr1)<<10) | (u2-Usurr2) + Usurrself
375
+ end
376
+ return Ucharerr
377
+ end
378
+
379
+
380
+ def surrogate?(u)
381
+ Usurr1 <= u && u < Usurr3
382
+ end
383
+
384
+
385
+ def nibble(c)
386
+ case true
387
+ when ?0 <= c && c <= ?9 then c.ord - ?0.ord
388
+ when ?a <= c && c <= ?z then c.ord - ?a.ord + 10
389
+ when ?A <= c && c <= ?Z then c.ord - ?A.ord + 10
390
+ else
391
+ raise Error, "invalid hex code #{c}"
392
+ end
393
+ end
394
+
395
+
396
+ # Encodes x into a json text. It may contain only
397
+ # Array, Hash, String, Numeric, true, false, nil.
398
+ # (Note, this list excludes Symbol.)
399
+ # X itself must be an Array or a Hash.
400
+ # No other value can be encoded, and an error will
401
+ # be raised if x contains any other value, such as
402
+ # Nan, Infinity, Symbol, and Proc, or if a Hash key
403
+ # is not a String.
404
+ # Strings contained in x must be valid UTF-8.
405
+ def encode(x)
406
+ case x
407
+ when Hash then objenc(x)
408
+ when Array then arrenc(x)
409
+ else
410
+ raise Error, 'root value must be an Array or a Hash'
411
+ end
412
+ end
413
+
414
+
415
+ def valenc(x)
416
+ case x
417
+ when Hash then objenc(x)
418
+ when Array then arrenc(x)
419
+ when String then strenc(x)
420
+ when Numeric then numenc(x)
421
+ when true then "true"
422
+ when false then "false"
423
+ when nil then "null"
424
+ else
425
+ if x.respond_to?(:to_json)
426
+ x.to_json
427
+ else
428
+ raise Error, "cannot encode #{x.class}: #{x.inspect}"
429
+ end
430
+ end
431
+ end
432
+
433
+
434
+ def objenc(x)
435
+ '{' + x.map{|k,v| keyenc(k) + ':' + valenc(v)}.join(',') + '}'
436
+ end
437
+
438
+
439
+ def arrenc(a)
440
+ '[' + a.map{|x| valenc(x)}.join(',') + ']'
441
+ end
442
+
443
+
444
+ def keyenc(k)
445
+ case k
446
+ when String then strenc(k)
447
+ else
448
+ raise Error, "Hash key is not a string: #{k.inspect}"
449
+ end
450
+ end
451
+
452
+
453
+ def strenc(s)
454
+ t = StringIO.new
455
+ t.putc(?")
456
+ r = 0
457
+
458
+ # In ruby >= 1.9, s[r] is a codepoint, not a byte.
459
+ rubydoesenc = s.class.method_defined?(:encoding)
460
+
461
+ while r < s.length
462
+ case s[r]
463
+ when ?" then t.print('\\"')
464
+ when ?\\ then t.print('\\\\')
465
+ when ?\b then t.print('\\b')
466
+ when ?\f then t.print('\\f')
467
+ when ?\n then t.print('\\n')
468
+ when ?\r then t.print('\\r')
469
+ when ?\t then t.print('\\t')
470
+ else
471
+ c = s[r]
472
+ case true
473
+ when rubydoesenc
474
+ begin
475
+ c.ord # will raise an error if c is invalid UTF-8
476
+ t.write(c)
477
+ rescue
478
+ t.write(Ustrerr)
479
+ end
480
+ when Spc <= c && c <= ?~
481
+ t.putc(c)
482
+ else
483
+ n = ucharcopy(t, s, r) # ensure valid UTF-8 output
484
+ r += n - 1 # r is incremented below
485
+ end
486
+ end
487
+ r += 1
488
+ end
489
+ t.putc(?")
490
+ t.string
491
+ end
492
+
493
+
494
+ def numenc(x)
495
+ if ((x.nan? || x.infinite?) rescue false)
496
+ raise Error, "Numeric cannot be represented: #{x}"
497
+ end
498
+ "#{x}"
499
+ end
500
+
501
+
502
+ # Copies the valid UTF-8 bytes of a single character
503
+ # from string s at position i to I/O object t, and
504
+ # returns the number of bytes copied.
505
+ # If no valid UTF-8 char exists at position i,
506
+ # ucharcopy writes Ustrerr and returns 1.
507
+ def ucharcopy(t, s, i)
508
+ n = s.length - i
509
+ raise Utf8Error if n < 1
510
+
511
+ c0 = s[i].ord
512
+
513
+ # 1-byte, 7-bit sequence?
514
+ if c0 < Utagx
515
+ t.putc(c0)
516
+ return 1
517
+ end
518
+
519
+ raise Utf8Error if c0 < Utag2 # unexpected continuation byte?
520
+
521
+ raise Utf8Error if n < 2 # need continuation byte
522
+ c1 = s[i+1].ord
523
+ raise Utf8Error if c1 < Utagx || Utag2 <= c1
524
+
525
+ # 2-byte, 11-bit sequence?
526
+ if c0 < Utag3
527
+ raise Utf8Error if ((c0&Umask2)<<6 | (c1&Umaskx)) <= Uchar1max
528
+ t.putc(c0)
529
+ t.putc(c1)
530
+ return 2
531
+ end
532
+
533
+ # need second continuation byte
534
+ raise Utf8Error if n < 3
535
+
536
+ c2 = s[i+2].ord
537
+ raise Utf8Error if c2 < Utagx || Utag2 <= c2
538
+
539
+ # 3-byte, 16-bit sequence?
540
+ if c0 < Utag4
541
+ u = (c0&Umask3)<<12 | (c1&Umaskx)<<6 | (c2&Umaskx)
542
+ raise Utf8Error if u <= Uchar2max
543
+ t.putc(c0)
544
+ t.putc(c1)
545
+ t.putc(c2)
546
+ return 3
547
+ end
548
+
549
+ # need third continuation byte
550
+ raise Utf8Error if n < 4
551
+ c3 = s[i+3].ord
552
+ raise Utf8Error if c3 < Utagx || Utag2 <= c3
553
+
554
+ # 4-byte, 21-bit sequence?
555
+ if c0 < Utag5
556
+ u = (c0&Umask4)<<18 | (c1&Umaskx)<<12 | (c2&Umaskx)<<6 | (c3&Umaskx)
557
+ raise Utf8Error if u <= Uchar3max
558
+ t.putc(c0)
559
+ t.putc(c1)
560
+ t.putc(c2)
561
+ t.putc(c3)
562
+ return 4
563
+ end
564
+
565
+ raise Utf8Error
566
+ rescue Utf8Error
567
+ t.write(Ustrerr)
568
+ return 1
569
+ end
570
+
571
+
572
+ class Utf8Error < ::StandardError
573
+ end
574
+
575
+
576
+ class Error < ::StandardError
577
+ end
578
+
579
+
580
+ Utagx = 0x80 # 1000 0000
581
+ Utag2 = 0xc0 # 1100 0000
582
+ Utag3 = 0xe0 # 1110 0000
583
+ Utag4 = 0xf0 # 1111 0000
584
+ Utag5 = 0xF8 # 1111 1000
585
+ Umaskx = 0x3f # 0011 1111
586
+ Umask2 = 0x1f # 0001 1111
587
+ Umask3 = 0x0f # 0000 1111
588
+ Umask4 = 0x07 # 0000 0111
589
+ Uchar1max = (1<<7) - 1
590
+ Uchar2max = (1<<11) - 1
591
+ Uchar3max = (1<<16) - 1
592
+ Ucharerr = 0xFFFD # unicode "replacement char"
593
+ Ustrerr = "\xef\xbf\xbd" # unicode "replacement char"
594
+ Usurrself = 0x10000
595
+ Usurr1 = 0xd800
596
+ Usurr2 = 0xdc00
597
+ Usurr3 = 0xe000
598
+
599
+ Spc = ' '[0]
600
+ Unesc = {?b=>?\b, ?f=>?\f, ?n=>?\n, ?r=>?\r, ?t=>?\t}
601
+ end
602
+ end