ffi-yajl 0.0.1-universal-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (214) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +201 -0
  3. data/README.md +17 -0
  4. data/Rakefile +62 -0
  5. data/bin/ffi-yajl-bench +36 -0
  6. data/ext/ffi_yajl/ext/encoder/encoder.c +240 -0
  7. data/ext/ffi_yajl/ext/encoder/extconf.rb +29 -0
  8. data/ext/ffi_yajl/ext/parser/extconf.rb +29 -0
  9. data/ext/ffi_yajl/ext/parser/parser.c +199 -0
  10. data/ext/libyajl2/extconf.rb +65 -0
  11. data/ext/libyajl2/vendored/.gitignore +3 -0
  12. data/ext/libyajl2/vendored/BUILDING +23 -0
  13. data/ext/libyajl2/vendored/BUILDING.win32 +27 -0
  14. data/ext/libyajl2/vendored/CMakeLists.txt +79 -0
  15. data/ext/libyajl2/vendored/COPYING +13 -0
  16. data/ext/libyajl2/vendored/ChangeLog +175 -0
  17. data/ext/libyajl2/vendored/README +74 -0
  18. data/ext/libyajl2/vendored/TODO +9 -0
  19. data/ext/libyajl2/vendored/YAJLDoc.cmake +26 -0
  20. data/ext/libyajl2/vendored/configure +79 -0
  21. data/ext/libyajl2/vendored/example/CMakeLists.txt +23 -0
  22. data/ext/libyajl2/vendored/example/README.md +7 -0
  23. data/ext/libyajl2/vendored/example/parse_config.c +69 -0
  24. data/ext/libyajl2/vendored/example/sample.config +101 -0
  25. data/ext/libyajl2/vendored/perf/CMakeLists.txt +23 -0
  26. data/ext/libyajl2/vendored/perf/documents.c +1418 -0
  27. data/ext/libyajl2/vendored/perf/documents.h +28 -0
  28. data/ext/libyajl2/vendored/perf/perftest.c +134 -0
  29. data/ext/libyajl2/vendored/reformatter/CMakeLists.txt +39 -0
  30. data/ext/libyajl2/vendored/reformatter/json_reformat.c +194 -0
  31. data/ext/libyajl2/vendored/src/CMakeLists.txt +86 -0
  32. data/ext/libyajl2/vendored/src/YAJL.dxy +1258 -0
  33. data/ext/libyajl2/vendored/src/api/yajl_common.h +75 -0
  34. data/ext/libyajl2/vendored/src/api/yajl_gen.h +157 -0
  35. data/ext/libyajl2/vendored/src/api/yajl_parse.h +226 -0
  36. data/ext/libyajl2/vendored/src/api/yajl_tree.h +185 -0
  37. data/ext/libyajl2/vendored/src/api/yajl_version.h.cmake +23 -0
  38. data/ext/libyajl2/vendored/src/yajl +33 -0
  39. data/ext/libyajl2/vendored/src/yajl.c +175 -0
  40. data/ext/libyajl2/vendored/src/yajl.pc.cmake +9 -0
  41. data/ext/libyajl2/vendored/src/yajl_alloc.c +52 -0
  42. data/ext/libyajl2/vendored/src/yajl_alloc.h +34 -0
  43. data/ext/libyajl2/vendored/src/yajl_buf.c +103 -0
  44. data/ext/libyajl2/vendored/src/yajl_buf.h +57 -0
  45. data/ext/libyajl2/vendored/src/yajl_bytestack.h +69 -0
  46. data/ext/libyajl2/vendored/src/yajl_encode.c +220 -0
  47. data/ext/libyajl2/vendored/src/yajl_encode.h +34 -0
  48. data/ext/libyajl2/vendored/src/yajl_gen.c +354 -0
  49. data/ext/libyajl2/vendored/src/yajl_lex.c +763 -0
  50. data/ext/libyajl2/vendored/src/yajl_lex.h +117 -0
  51. data/ext/libyajl2/vendored/src/yajl_parser.c +498 -0
  52. data/ext/libyajl2/vendored/src/yajl_parser.h +78 -0
  53. data/ext/libyajl2/vendored/src/yajl_tree.c +503 -0
  54. data/ext/libyajl2/vendored/src/yajl_version.c +7 -0
  55. data/ext/libyajl2/vendored/test/CMakeLists.txt +23 -0
  56. data/ext/libyajl2/vendored/test/cases/ac_difficult_json_c_test_case_with_comments.json +1 -0
  57. data/ext/libyajl2/vendored/test/cases/ac_difficult_json_c_test_case_with_comments.json.gold +36 -0
  58. data/ext/libyajl2/vendored/test/cases/ac_simple_with_comments.json +11 -0
  59. data/ext/libyajl2/vendored/test/cases/ac_simple_with_comments.json.gold +9 -0
  60. data/ext/libyajl2/vendored/test/cases/ag_false_then_garbage.json +1 -0
  61. data/ext/libyajl2/vendored/test/cases/ag_false_then_garbage.json.gold +2 -0
  62. data/ext/libyajl2/vendored/test/cases/ag_null_then_garbage.json +1 -0
  63. data/ext/libyajl2/vendored/test/cases/ag_null_then_garbage.json.gold +2 -0
  64. data/ext/libyajl2/vendored/test/cases/ag_true_then_garbage.json +1 -0
  65. data/ext/libyajl2/vendored/test/cases/ag_true_then_garbage.json.gold +2 -0
  66. data/ext/libyajl2/vendored/test/cases/am_eof.json +1 -0
  67. data/ext/libyajl2/vendored/test/cases/am_eof.json.gold +4 -0
  68. data/ext/libyajl2/vendored/test/cases/am_integers.json +1 -0
  69. data/ext/libyajl2/vendored/test/cases/am_integers.json.gold +3 -0
  70. data/ext/libyajl2/vendored/test/cases/am_multiple.json +3 -0
  71. data/ext/libyajl2/vendored/test/cases/am_multiple.json.gold +5 -0
  72. data/ext/libyajl2/vendored/test/cases/am_stuff.json +7 -0
  73. data/ext/libyajl2/vendored/test/cases/am_stuff.json.gold +14 -0
  74. data/ext/libyajl2/vendored/test/cases/ap_array_open.json +1 -0
  75. data/ext/libyajl2/vendored/test/cases/ap_array_open.json.gold +2 -0
  76. data/ext/libyajl2/vendored/test/cases/ap_eof_str.json +1 -0
  77. data/ext/libyajl2/vendored/test/cases/ap_eof_str.json.gold +1 -0
  78. data/ext/libyajl2/vendored/test/cases/ap_map_open.json +1 -0
  79. data/ext/libyajl2/vendored/test/cases/ap_map_open.json.gold +2 -0
  80. data/ext/libyajl2/vendored/test/cases/ap_partial_ok.json +1 -0
  81. data/ext/libyajl2/vendored/test/cases/ap_partial_ok.json.gold +4 -0
  82. data/ext/libyajl2/vendored/test/cases/array.json +6 -0
  83. data/ext/libyajl2/vendored/test/cases/array.json.gold +22 -0
  84. data/ext/libyajl2/vendored/test/cases/array_close.json +1 -0
  85. data/ext/libyajl2/vendored/test/cases/array_close.json.gold +2 -0
  86. data/ext/libyajl2/vendored/test/cases/bignums.json +1 -0
  87. data/ext/libyajl2/vendored/test/cases/bignums.json.gold +5 -0
  88. data/ext/libyajl2/vendored/test/cases/bogus_char.json +4 -0
  89. data/ext/libyajl2/vendored/test/cases/bogus_char.json.gold +10 -0
  90. data/ext/libyajl2/vendored/test/cases/codepoints_from_unicode_org.json +1 -0
  91. data/ext/libyajl2/vendored/test/cases/codepoints_from_unicode_org.json.gold +2 -0
  92. data/ext/libyajl2/vendored/test/cases/deep_arrays.json +1 -0
  93. data/ext/libyajl2/vendored/test/cases/deep_arrays.json.gold +2049 -0
  94. data/ext/libyajl2/vendored/test/cases/difficult_json_c_test_case.json +1 -0
  95. data/ext/libyajl2/vendored/test/cases/difficult_json_c_test_case.json.gold +36 -0
  96. data/ext/libyajl2/vendored/test/cases/doubles.json +1 -0
  97. data/ext/libyajl2/vendored/test/cases/doubles.json.gold +7 -0
  98. data/ext/libyajl2/vendored/test/cases/doubles_in_array.json +1 -0
  99. data/ext/libyajl2/vendored/test/cases/doubles_in_array.json.gold +8 -0
  100. data/ext/libyajl2/vendored/test/cases/empty_array.json +1 -0
  101. data/ext/libyajl2/vendored/test/cases/empty_array.json.gold +3 -0
  102. data/ext/libyajl2/vendored/test/cases/empty_string.json +1 -0
  103. data/ext/libyajl2/vendored/test/cases/empty_string.json.gold +2 -0
  104. data/ext/libyajl2/vendored/test/cases/escaped_bulgarian.json +4 -0
  105. data/ext/libyajl2/vendored/test/cases/escaped_bulgarian.json.gold +7 -0
  106. data/ext/libyajl2/vendored/test/cases/escaped_foobar.json +1 -0
  107. data/ext/libyajl2/vendored/test/cases/escaped_foobar.json.gold +2 -0
  108. data/ext/libyajl2/vendored/test/cases/false.json +1 -0
  109. data/ext/libyajl2/vendored/test/cases/false.json.gold +2 -0
  110. data/ext/libyajl2/vendored/test/cases/fg_false_then_garbage.json +1 -0
  111. data/ext/libyajl2/vendored/test/cases/fg_false_then_garbage.json.gold +3 -0
  112. data/ext/libyajl2/vendored/test/cases/fg_issue_7.json +1 -0
  113. data/ext/libyajl2/vendored/test/cases/fg_issue_7.json.gold +3 -0
  114. data/ext/libyajl2/vendored/test/cases/fg_null_then_garbage.json +1 -0
  115. data/ext/libyajl2/vendored/test/cases/fg_null_then_garbage.json.gold +3 -0
  116. data/ext/libyajl2/vendored/test/cases/fg_true_then_garbage.json +1 -0
  117. data/ext/libyajl2/vendored/test/cases/fg_true_then_garbage.json.gold +3 -0
  118. data/ext/libyajl2/vendored/test/cases/four_byte_utf8.json +2 -0
  119. data/ext/libyajl2/vendored/test/cases/four_byte_utf8.json.gold +5 -0
  120. data/ext/libyajl2/vendored/test/cases/high_overflow.json +1 -0
  121. data/ext/libyajl2/vendored/test/cases/high_overflow.json.gold +2 -0
  122. data/ext/libyajl2/vendored/test/cases/integers.json +3 -0
  123. data/ext/libyajl2/vendored/test/cases/integers.json.gold +14 -0
  124. data/ext/libyajl2/vendored/test/cases/invalid_utf8.json +1 -0
  125. data/ext/libyajl2/vendored/test/cases/invalid_utf8.json.gold +3 -0
  126. data/ext/libyajl2/vendored/test/cases/isolated_surrogate_marker.json +1 -0
  127. data/ext/libyajl2/vendored/test/cases/isolated_surrogate_marker.json.gold +2 -0
  128. data/ext/libyajl2/vendored/test/cases/leading_zero_in_number.json +1 -0
  129. data/ext/libyajl2/vendored/test/cases/leading_zero_in_number.json.gold +5 -0
  130. data/ext/libyajl2/vendored/test/cases/lonely_minus_sign.json +7 -0
  131. data/ext/libyajl2/vendored/test/cases/lonely_minus_sign.json.gold +9 -0
  132. data/ext/libyajl2/vendored/test/cases/lonely_number.json +1 -0
  133. data/ext/libyajl2/vendored/test/cases/lonely_number.json.gold +2 -0
  134. data/ext/libyajl2/vendored/test/cases/low_overflow.json +1 -0
  135. data/ext/libyajl2/vendored/test/cases/low_overflow.json.gold +2 -0
  136. data/ext/libyajl2/vendored/test/cases/map_close.json +1 -0
  137. data/ext/libyajl2/vendored/test/cases/map_close.json.gold +2 -0
  138. data/ext/libyajl2/vendored/test/cases/missing_integer_after_decimal_point.json +1 -0
  139. data/ext/libyajl2/vendored/test/cases/missing_integer_after_decimal_point.json.gold +2 -0
  140. data/ext/libyajl2/vendored/test/cases/missing_integer_after_exponent.json +1 -0
  141. data/ext/libyajl2/vendored/test/cases/missing_integer_after_exponent.json.gold +2 -0
  142. data/ext/libyajl2/vendored/test/cases/multiple.json +3 -0
  143. data/ext/libyajl2/vendored/test/cases/multiple.json.gold +4 -0
  144. data/ext/libyajl2/vendored/test/cases/non_utf8_char_in_string.json +1 -0
  145. data/ext/libyajl2/vendored/test/cases/non_utf8_char_in_string.json.gold +8 -0
  146. data/ext/libyajl2/vendored/test/cases/np_partial_bad.json +1 -0
  147. data/ext/libyajl2/vendored/test/cases/np_partial_bad.json.gold +5 -0
  148. data/ext/libyajl2/vendored/test/cases/null.json +1 -0
  149. data/ext/libyajl2/vendored/test/cases/null.json.gold +2 -0
  150. data/ext/libyajl2/vendored/test/cases/nulls_and_bools.json +5 -0
  151. data/ext/libyajl2/vendored/test/cases/nulls_and_bools.json.gold +9 -0
  152. data/ext/libyajl2/vendored/test/cases/simple.json +5 -0
  153. data/ext/libyajl2/vendored/test/cases/simple.json.gold +9 -0
  154. data/ext/libyajl2/vendored/test/cases/simple_with_comments.json +11 -0
  155. data/ext/libyajl2/vendored/test/cases/simple_with_comments.json.gold +5 -0
  156. data/ext/libyajl2/vendored/test/cases/string_invalid_escape.json +1 -0
  157. data/ext/libyajl2/vendored/test/cases/string_invalid_escape.json.gold +3 -0
  158. data/ext/libyajl2/vendored/test/cases/string_invalid_hex_char.json +1 -0
  159. data/ext/libyajl2/vendored/test/cases/string_invalid_hex_char.json.gold +2 -0
  160. data/ext/libyajl2/vendored/test/cases/string_with_escapes.json +3 -0
  161. data/ext/libyajl2/vendored/test/cases/string_with_escapes.json.gold +7 -0
  162. data/ext/libyajl2/vendored/test/cases/string_with_invalid_newline.json +2 -0
  163. data/ext/libyajl2/vendored/test/cases/string_with_invalid_newline.json.gold +2 -0
  164. data/ext/libyajl2/vendored/test/cases/three_byte_utf8.json +1 -0
  165. data/ext/libyajl2/vendored/test/cases/three_byte_utf8.json.gold +7 -0
  166. data/ext/libyajl2/vendored/test/cases/true.json +1 -0
  167. data/ext/libyajl2/vendored/test/cases/true.json.gold +2 -0
  168. data/ext/libyajl2/vendored/test/cases/unescaped_bulgarian.json +1 -0
  169. data/ext/libyajl2/vendored/test/cases/unescaped_bulgarian.json.gold +4 -0
  170. data/ext/libyajl2/vendored/test/cases/zerobyte.json +1 -0
  171. data/ext/libyajl2/vendored/test/cases/zerobyte.json.gold +0 -0
  172. data/ext/libyajl2/vendored/test/run_tests.sh +94 -0
  173. data/ext/libyajl2/vendored/test/yajl_test.c +281 -0
  174. data/ext/libyajl2/vendored/verify/CMakeLists.txt +39 -0
  175. data/ext/libyajl2/vendored/verify/json_verify.c +116 -0
  176. data/lib/ffi_yajl.rb +14 -0
  177. data/lib/ffi_yajl/benchmark.rb +7 -0
  178. data/lib/ffi_yajl/benchmark/MIT-LICENSE +20 -0
  179. data/lib/ffi_yajl/benchmark/encode.rb +135 -0
  180. data/lib/ffi_yajl/benchmark/encode_json_and_marshal.rb +42 -0
  181. data/lib/ffi_yajl/benchmark/encode_json_and_yaml.rb +53 -0
  182. data/lib/ffi_yajl/benchmark/encode_profile.rb +38 -0
  183. data/lib/ffi_yajl/benchmark/http.rb +32 -0
  184. data/lib/ffi_yajl/benchmark/parse.rb +133 -0
  185. data/lib/ffi_yajl/benchmark/parse_json_and_marshal.rb +50 -0
  186. data/lib/ffi_yajl/benchmark/parse_json_and_yaml.rb +55 -0
  187. data/lib/ffi_yajl/benchmark/parse_profile.rb +37 -0
  188. data/lib/ffi_yajl/benchmark/parse_profile_ruby_prof.rb +39 -0
  189. data/lib/ffi_yajl/benchmark/parse_stream.rb +54 -0
  190. data/lib/ffi_yajl/benchmark/subjects/item.json +1 -0
  191. data/lib/ffi_yajl/benchmark/subjects/ohai.json +1216 -0
  192. data/lib/ffi_yajl/benchmark/subjects/ohai.marshal_dump +0 -0
  193. data/lib/ffi_yajl/benchmark/subjects/ohai.yml +975 -0
  194. data/lib/ffi_yajl/benchmark/subjects/twitter_search.json +1 -0
  195. data/lib/ffi_yajl/benchmark/subjects/twitter_stream.json +430 -0
  196. data/lib/ffi_yajl/benchmark/subjects/unicode.json +1 -0
  197. data/lib/ffi_yajl/encoder.rb +53 -0
  198. data/lib/ffi_yajl/ext.rb +22 -0
  199. data/lib/ffi_yajl/ext/.keep +0 -0
  200. data/lib/ffi_yajl/ffi.rb +129 -0
  201. data/lib/ffi_yajl/ffi/encoder.rb +175 -0
  202. data/lib/ffi_yajl/ffi/parser.rb +145 -0
  203. data/lib/ffi_yajl/json_gem.rb +121 -0
  204. data/lib/ffi_yajl/parser.rb +23 -0
  205. data/lib/ffi_yajl/version.rb +3 -0
  206. data/lib/libyajl.so +0 -0
  207. data/lib/libyajl.so.2 +0 -0
  208. data/lib/libyajl.so.2.0.5 +0 -0
  209. data/lib/libyajl_s.a +0 -0
  210. data/spec/ffi_yajl/encoder_spec.rb +39 -0
  211. data/spec/ffi_yajl/json_gem_spec.rb +355 -0
  212. data/spec/ffi_yajl/parser_spec.rb +78 -0
  213. data/spec/spec_helper.rb +14 -0
  214. metadata +330 -0
@@ -0,0 +1,121 @@
1
+
2
+ # JSON compatibility layer, largely plagarized from yajl-ruby
3
+
4
+ require 'ffi_yajl' unless defined?(FFI_Yajl::Parser)
5
+
6
+ module JSON
7
+ class JSONError < StandardError; end unless defined?(JSON::JSONError)
8
+ class GeneratorError < JSONError; end unless defined?(JSON::GeneratorError)
9
+ class ParserError < JSONError; end unless defined?(JSON::ParserError)
10
+
11
+ def self.generate(obj, opts=nil)
12
+ opts ||= {}
13
+ options_map = {}
14
+ if opts.has_key?(:indent)
15
+ options_map[:pretty] = true
16
+ options_map[:indent] = opts[:indent]
17
+ end
18
+ FFI_Yajl::Encoder.encode(obj, options_map)
19
+ rescue FFI_Yajl::EncodeError => e
20
+ raise JSON::GeneratorError, e.message
21
+ end
22
+
23
+ def self.pretty_generate(obj, opts={})
24
+ options_map = {}
25
+ options_map[:pretty] = true
26
+ options_map[:indent] = opts[:indent] if opts.has_key?(:indent)
27
+ FFI_Yajl::Encoder.encode(obj, options_map).chomp
28
+ rescue FFI_Yajl::EncodeError => e
29
+ raise JSON::GeneratorError, e.message
30
+ end
31
+
32
+ def self.dump(obj, io=nil, *args)
33
+ FFI_Yajl::Encoder.encode(obj, io)
34
+ rescue FFI_Yajl::EncodeError => e
35
+ raise JSON::GeneratorError, e.message
36
+ end
37
+
38
+ def self.default_options
39
+ #@default_options ||= {:symbolize_keys => false}
40
+ @default_options ||= {}
41
+ end
42
+
43
+ def self.parse(str, opts=JSON.default_options)
44
+ FFI_Yajl::Parser.parse(str, opts)
45
+ rescue FFI_Yajl::ParseError => e
46
+ raise JSON::ParserError, e.message
47
+ end
48
+
49
+ def self.load(input, *args)
50
+ FFI_Yajl::Parser.parse(input, default_options)
51
+ rescue FFI_Yajl::ParseError => e
52
+ raise JSON::ParserError, e.message
53
+ end
54
+ end
55
+
56
+ class Array
57
+ def to_json(*opts, &block)
58
+ FFI_Yajl::Encoder.encode(self)
59
+ end
60
+ end
61
+
62
+ class Hash
63
+ def to_json(*opts, &block)
64
+ FFI_Yajl::Encoder.encode(self)
65
+ end
66
+ end
67
+
68
+ class Fixnum
69
+ def to_json(*opts, &block)
70
+ FFI_Yajl::Encoder.encode(self)
71
+ end
72
+ end
73
+
74
+ class Float
75
+ def to_json(*opts, &block)
76
+ FFI_Yajl::Encoder.encode(self)
77
+ end
78
+ end
79
+
80
+ class String
81
+ def to_json(*opts, &block)
82
+ FFI_Yajl::Encoder.encode(self)
83
+ end
84
+ end
85
+
86
+ class TrueClass
87
+ def to_json(*opts, &block)
88
+ FFI_Yajl::Encoder.encode(self)
89
+ end
90
+ end
91
+
92
+ class FalseClass
93
+ def to_json(*opts, &block)
94
+ FFI_Yajl::Encoder.encode(self)
95
+ end
96
+ end
97
+
98
+ class NilClass
99
+ def to_json(*opts, &block)
100
+ FFI_Yajl::Encoder.encode(self)
101
+ end
102
+ end
103
+
104
+ module ::Kernel
105
+ def JSON(object, opts = {})
106
+ if object.respond_to? :to_s
107
+ JSON.parse(object.to_s, JSON.default_options.merge(opts))
108
+ else
109
+ JSON.generate(object,opts)
110
+ end
111
+ end
112
+ end
113
+
114
+ class Object
115
+ unless defined?(ActiveSupport)
116
+ def to_json(*args, &block)
117
+ "\"#{to_s}\""
118
+ end
119
+ end
120
+ end
121
+
@@ -0,0 +1,23 @@
1
+
2
+ module FFI_Yajl
3
+ class ParseError < StandardError; end
4
+ class Parser
5
+ attr_accessor :opts
6
+
7
+ def self.parse(obj, *args)
8
+ new(*args).parse(obj)
9
+ end
10
+
11
+ def initialize(opts = {})
12
+ @opts = opts
13
+ end
14
+
15
+ def parse(str)
16
+ # initialization that we can do in pure ruby
17
+ yajl_opts = {}
18
+
19
+ # call either the ext or ffi hook
20
+ do_yajl_parse(str, yajl_opts)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module FFI_Yajl
2
+ VERSION = "0.0.1"
3
+ end
data/lib/libyajl.so ADDED
Binary file
data/lib/libyajl.so.2 ADDED
Binary file
Binary file
data/lib/libyajl_s.a ADDED
Binary file
@@ -0,0 +1,39 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe "FFI_Yajl::Encoder" do
6
+
7
+ let(:encoder) { FFI_Yajl::Encoder.new }
8
+
9
+ it "encodes fixnums in keys as strings" do
10
+ ruby = {1 => 2}
11
+ expect(encoder.encode(ruby)).to eq('{"1":2}')
12
+ end
13
+
14
+ it "encodes floats in keys as strings" do
15
+ ruby = {1.1 => 2}
16
+ expect(encoder.encode(ruby)).to eq('{"1.1":2}')
17
+ end
18
+
19
+ it "encodes bignums in keys as strings" do
20
+ ruby = {12345678901234567890 => 2}
21
+ expect(encoder.encode(ruby)).to eq('{"12345678901234567890":2}')
22
+ end
23
+
24
+ # XXX: 127 == YAJL_MAX_DEPTH hardcodedness, zero control for us, it isn't even a twiddleable #define
25
+ it "raises an exception for deeply nested arrays" do
26
+ root = []
27
+ a = root
28
+ 127.times { |_| a << []; a = a[0] }
29
+ expect{ encoder.encode(root) }.to raise_error(FFI_Yajl::EncodeError)
30
+ end
31
+
32
+ it "raises an exception for deeply nested hashes" do
33
+ root = {}
34
+ a = root
35
+ 127.times {|_| a["a"] = {}; a = a["a"] }
36
+ expect{ encoder.encode(root) }.to raise_error(FFI_Yajl::EncodeError)
37
+ end
38
+ end
39
+
@@ -0,0 +1,355 @@
1
+ # encoding: UTF-8
2
+
3
+ # This started life as yajl-ruby's json_gem_compatibility/compatibility_spec.rb file.
4
+ # Kinda looks like they stole it from the JSON gem. I updated the syntax a lot.
5
+
6
+ require 'spec_helper'
7
+
8
+ class Dummy; end
9
+
10
+ describe "JSON Gem Compat API" do
11
+
12
+ # Magic to make the before loading tests actually run before loading
13
+ RSpec.configure do |config|
14
+ config.order_groups_and_examples do |list|
15
+ list.sort_by { |item| item.description }
16
+ end
17
+ end
18
+
19
+ context "A: before loading the compat library" do
20
+ it "should not mixin #to_json on random objects" do
21
+ d = Dummy.new
22
+ expect(d.respond_to?(:to_json)).to be_false
23
+ end
24
+
25
+ it "should not mixin #to_json to a string" do
26
+ expect("".respond_to?(:to_json)).to be_false
27
+ end
28
+
29
+ it "should not mixin #to_json to a fixnum" do
30
+ expect(1.respond_to?(:to_json)).to be_false
31
+ end
32
+
33
+ it "should not mixin #to_json on a float" do
34
+ expect("1.5".to_f.respond_to?(:to_json)).to be_false
35
+ end
36
+
37
+ it "should not mixin #to_json on an array" do
38
+ expect([].respond_to?(:to_json)).to be_false
39
+ end
40
+
41
+ it "should not mixin #to_json on a hash" do
42
+ expect({:foo => "bar"}.respond_to?(:to_json)).to be_false
43
+ end
44
+
45
+ it "should not mixin #to_json on a trueclass" do
46
+ expect(true.respond_to?(:to_json)).to be_false
47
+ end
48
+
49
+ it "should not mixin #to_json on a falseclass" do
50
+ expect(false.respond_to?(:to_json)).to be_false
51
+ end
52
+
53
+ it "should not mixin #to_json on a nilclass" do
54
+ expect(nil.respond_to?(:to_json)).to be_false
55
+ end
56
+ end
57
+
58
+ context "B: when in compat mode" do
59
+
60
+ before(:all) do
61
+ require 'ffi_yajl/json_gem'
62
+ end
63
+
64
+ it "should define JSON class" do
65
+ expect(defined?(JSON)).to be_true
66
+ end
67
+
68
+ it "should implement JSON#parse" do
69
+ expect(JSON.respond_to?(:parse)).to be_true
70
+ end
71
+
72
+ it "should implement JSON#generate" do
73
+ expect(JSON.respond_to?(:generate)).to be_true
74
+ end
75
+
76
+ it "should implement JSON#pretty_generate" do
77
+ expect(JSON.respond_to?(:pretty_generate)).to be_true
78
+ end
79
+
80
+ it "should implement JSON#load" do
81
+ expect(JSON.respond_to?(:load)).to be_true
82
+ end
83
+
84
+ it "should implement JSON#dump" do
85
+ expect(JSON.respond_to?(:dump)).to be_true
86
+ end
87
+
88
+ context "when setting symbolize_keys via JSON.default_options" do
89
+ before(:all) { @saved_default = JSON.default_options[:symbolize_keys] }
90
+ after(:all) { JSON.default_options[:symbolize_keys] = @saved_default }
91
+
92
+ it "the default behavior should be to not symbolize keys" do
93
+ expect(JSON.parse('{"foo": 1234}')).to eq({"foo" => 1234})
94
+ end
95
+
96
+ it "changing the default_options should change the behavior to true" do
97
+ pending("implement symbolize keys")
98
+ JSON.default_options[:symbolize_keys] = true
99
+ expect(JSON.parse('{"foo": 1234}')).to eq({:foo => 1234})
100
+ end
101
+ end
102
+
103
+ context "when setting symbolize_names via JSON.default_options" do
104
+ before(:all) { @saved_default = JSON.default_options[:symbolize_names] }
105
+ after(:all) { JSON.default_options[:symbolize_names] = @saved_default }
106
+
107
+ it "the default behavior should be to not symbolize keys" do
108
+ expect(JSON.parse('{"foo": 1234}')).to eq({"foo" => 1234})
109
+ end
110
+
111
+ it "changing the default_options should change the behavior to true" do
112
+ pending("implement symbolize keys")
113
+ JSON.default_options[:symbolize_names] = true
114
+ expect(JSON.parse('{"foo": 1234}')).to eq({:foo => 1234})
115
+ end
116
+ end
117
+
118
+ it "should support passing symbolize_names to JSON.parse" do
119
+ pending("implement symbolize keys")
120
+ expect(JSON.parse('{"foo": 1234}', :symbolize_names => true)).to eq({:foo => 1234})
121
+ end
122
+
123
+ it "should support passing symbolize_keys to JSON.parse" do
124
+ pending("implement symbolize keys")
125
+ expect(JSON.parse('{"foo": 1234}', :symbolize_keys => true)).to eq({:foo => 1234})
126
+ end
127
+
128
+ context "when encode arbitrary classes via their default to_json method" do
129
+ it "encodes random classes correctly" do
130
+ d = Dummy.new
131
+ expect(d.to_json).to eq( %Q{"#{d.to_s}"} )
132
+ end
133
+
134
+ it "encodes Time values correctly" do
135
+ t = Time.new
136
+ expect(t.to_json).to eq( %Q{"#{t.to_s}"} )
137
+ end
138
+
139
+ it "encodes Date values correctly" do
140
+ da = Date.new
141
+ expect(da.to_json).to eq( %Q{"#{da.to_s}"} )
142
+ end
143
+
144
+ it "encodes DateTime values correctly" do
145
+ dt = DateTime.new
146
+ expect(dt.to_json).to eq( %Q{"#{dt.to_s}"} )
147
+ end
148
+ end
149
+
150
+ context "JSON exception classes" do
151
+ it "should define JSON::JSONError as a StandardError" do
152
+ expect(JSON::JSONError.new.is_a?(StandardError)).to be_true
153
+ end
154
+ it "should define JSON::ParserError as a JSON::JSONError" do
155
+ expect(JSON::ParserError.new.is_a?(JSON::JSONError)).to be_true
156
+ end
157
+ it "should define JSON::GeneratorError as a JSON::JSONError" do
158
+ expect(JSON::GeneratorError.new.is_a?(JSON::JSONError)).to be_true
159
+ end
160
+ it "should raise JSON::ParserError on a bad parse" do
161
+ expect{ JSON.parse("blah") }.to raise_error(JSON::ParserError)
162
+ end
163
+ it "should raise JSON::GeneratorError on encoding NaN" do
164
+ expect{ JSON.generate(0.0/0.0) }.to raise_error(JSON::GeneratorError)
165
+ end
166
+ it "should raise JSON::GeneratorError on encoding -Infinity" do
167
+ expect{ JSON.generate(-1.0/0.0) }.to raise_error(JSON::GeneratorError)
168
+ end
169
+ it "should raise JSON::GeneratorError on encoding Infinity" do
170
+ expect{ JSON.generate(1.0/0.0) }.to raise_error(JSON::GeneratorError)
171
+ end
172
+ it "should raise JSON::GeneratorError on encoding a partial UTF-8 character" do
173
+ expect{ JSON.generate(["\xea"]) }.to raise_error(JSON::GeneratorError)
174
+ end
175
+ end
176
+
177
+ shared_examples_for "handles encoding and parsing correctly" do
178
+ it "should encode the content correctly" do
179
+ expect(ruby.to_json).to eq(json)
180
+ end
181
+ it "should parse the content correctly" do
182
+ expect(JSON.parse(json)).to eq(ruby)
183
+ end
184
+ end
185
+
186
+ context "when encoding strings" do
187
+ it "should render empty string correctly" do
188
+ expect(''.to_json).to eq( %q{""} )
189
+ end
190
+ it "should encode backspace character" do
191
+ expect("\b".to_json).to eq( %q{"\\b"} )
192
+ end
193
+ it "should encode \u0001 correctly" do
194
+ expect(0x1.chr.to_json).to eq( %q{"\u0001"} )
195
+ end
196
+
197
+ it "should encode \u001f correctly" do
198
+ expect(0x1f.chr.to_json).to eq( %q{"\u001F"} )
199
+ end
200
+
201
+ it "should encode a string with a space correctly" do
202
+ expect(' '.to_json).to eq( %q{" "} )
203
+ end
204
+
205
+ it "should encode 0x75 correctly" do
206
+ expect(0x7f.chr.to_json).to eq( %Q{"#{0x7f.chr}"} )
207
+ end
208
+
209
+ context "when dealing with bignums" do
210
+ let(:ruby) { [ 12345678901234567890 ] }
211
+ let(:json) { "[12345678901234567890]" }
212
+
213
+ it_behaves_like "handles encoding and parsing correctly"
214
+ end
215
+
216
+
217
+ context "when dealing with common UTF-8 symbols" do
218
+ let(:ruby) { [ "© ≠ €! \01" ] }
219
+ let(:json) { "[\"© ≠ €! \\u0001\"]" }
220
+
221
+ it_behaves_like "handles encoding and parsing correctly"
222
+ end
223
+
224
+ context "when dealing with Hiragana UTF-8 characters" do
225
+ let(:ruby) { ["\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212"] }
226
+ let(:json) { "[\"あいうえお\"]" }
227
+
228
+ it_behaves_like "handles encoding and parsing correctly"
229
+ end
230
+
231
+ context "whatever this is" do
232
+ let(:ruby) { ['საქართველო'] }
233
+ let(:json) { "[\"საქართველო\"]" }
234
+
235
+ it_behaves_like "handles encoding and parsing correctly"
236
+ end
237
+
238
+ context "accents" do
239
+ let(:ruby) { ["Ã"] }
240
+ let(:json) { '["Ã"]' }
241
+
242
+ it_behaves_like "handles encoding and parsing correctly"
243
+ end
244
+
245
+ context "euro symbol" do
246
+ let(:ruby) { ["€"] }
247
+ let(:json) { '["\u20ac"]' }
248
+ it "should parse the content correctly" do
249
+ expect(JSON.parse(json)).to eq(ruby)
250
+ end
251
+ end
252
+
253
+ context "and whatever this is" do
254
+
255
+ utf8_str = "\xf0\xa0\x80\x81"
256
+ let(:ruby) { [utf8_str] }
257
+ let(:json) { "[\"#{utf8_str}\"]" }
258
+
259
+ it_behaves_like "handles encoding and parsing correctly"
260
+ end
261
+ end
262
+
263
+
264
+ context "when encoding basic types with #to_json" do
265
+ it "Array#to_json should work" do
266
+ expect([ "a", "b", "c" ].to_json).to eq(%Q{["a","b","c"]})
267
+ end
268
+
269
+ it "Hash#to_json should work" do
270
+ expect({"a"=>"b"}.to_json).to eq(%Q{{"a":"b"}})
271
+ end
272
+
273
+ it "Fixnum#to_json should work" do
274
+ expect(1.to_json).to eq("1")
275
+ end
276
+
277
+ it "Float#to_json should work" do
278
+ expect(1.1.to_json).to eq("1.1")
279
+ end
280
+
281
+ it "String#to_json should work" do
282
+ expect("foo".to_json).to eq(%Q{"foo"})
283
+ end
284
+
285
+ it "TrueClass#to_json should work" do
286
+ expect(true.to_json).to eq("true")
287
+ end
288
+
289
+ it "FalseClass#to_json should work" do
290
+ expect(false.to_json).to eq("false")
291
+ end
292
+
293
+ it "NilClass#to_json should work" do
294
+ expect(nil.to_json).to eq("null")
295
+ end
296
+ end
297
+
298
+ context "ported tests for generation" do
299
+ before(:all) do
300
+ @hash = {
301
+ 'a' => 2,
302
+ 'b' => 3.141,
303
+ 'c' => 'c',
304
+ 'd' => [ 1, "b", 3.14 ],
305
+ 'e' => { 'foo' => 'bar' },
306
+ 'g' => "blah",
307
+ 'h' => 1000.0,
308
+ 'i' => 0.001
309
+ }
310
+
311
+ @json2 = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},"g":"blah","h":1000.0,"i":0.001}'
312
+
313
+ @json3 = %{
314
+ {
315
+ "a": 2,
316
+ "b": 3.141,
317
+ "c": "c",
318
+ "d": [1, "b", 3.14],
319
+ "e": {"foo": "bar"},
320
+ "g": "blah",
321
+ "h": 1000.0,
322
+ "i": 0.001
323
+ }
324
+ }.chomp
325
+ end
326
+
327
+ it "should be able to unparse" do
328
+ json = JSON.generate(@hash)
329
+ expect(JSON.parse(@json2)).to eq(JSON.parse(json))
330
+ parsed_json = JSON.parse(json)
331
+ expect(@hash).to eq(parsed_json)
332
+ json = JSON.generate({1=>2})
333
+ expect('{"1":2}').to eql(json)
334
+ parsed_json = JSON.parse(json)
335
+ expect({"1"=>2}).to eq(parsed_json)
336
+ end
337
+
338
+ it "should be able to unparse pretty" do
339
+ json = JSON.pretty_generate(@hash)
340
+ expect(JSON.parse(@json3)).to eq(JSON.parse(json))
341
+ parsed_json = JSON.parse(json)
342
+ expect(@hash).to eq(parsed_json)
343
+ json = JSON.pretty_generate({1=>2})
344
+ test = "{\n \"1\": 2\n}".chomp
345
+ expect(test).to eq(json)
346
+ parsed_json = JSON.parse(json)
347
+ expect({"1"=>2}).to eq(parsed_json)
348
+ end
349
+
350
+ it "JSON.generate should handle nil second argument" do
351
+ expect(JSON.generate(["foo"], nil)).to eql(%q{["foo"]})
352
+ end
353
+ end
354
+ end
355
+ end