benofsky-yajl-ruby 0.7.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (143) hide show
  1. data/.gitignore +9 -0
  2. data/CHANGELOG.md +281 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +320 -0
  5. data/Rakefile +40 -0
  6. data/VERSION.yml +5 -0
  7. data/benchmark/encode.rb +58 -0
  8. data/benchmark/encode_json_and_marshal.rb +42 -0
  9. data/benchmark/encode_json_and_yaml.rb +53 -0
  10. data/benchmark/http.rb +32 -0
  11. data/benchmark/parse.rb +59 -0
  12. data/benchmark/parse_json_and_marshal.rb +50 -0
  13. data/benchmark/parse_json_and_yaml.rb +55 -0
  14. data/benchmark/parse_stream.rb +54 -0
  15. data/benchmark/subjects/item.json +1 -0
  16. data/benchmark/subjects/ohai.json +1216 -0
  17. data/benchmark/subjects/ohai.marshal_dump +0 -0
  18. data/benchmark/subjects/ohai.yml +975 -0
  19. data/benchmark/subjects/twitter_search.json +1 -0
  20. data/benchmark/subjects/twitter_stream.json +430 -0
  21. data/benchmark/subjects/unicode.json +1 -0
  22. data/examples/encoding/chunked_encoding.rb +27 -0
  23. data/examples/encoding/one_shot.rb +13 -0
  24. data/examples/encoding/to_an_io.rb +12 -0
  25. data/examples/http/twitter_search_api.rb +12 -0
  26. data/examples/http/twitter_stream_api.rb +26 -0
  27. data/examples/parsing/from_file.rb +14 -0
  28. data/examples/parsing/from_stdin.rb +9 -0
  29. data/examples/parsing/from_string.rb +13 -0
  30. data/ext/api/yajl_common.h +85 -0
  31. data/ext/api/yajl_gen.h +159 -0
  32. data/ext/api/yajl_parse.h +196 -0
  33. data/ext/extconf.rb +9 -0
  34. data/ext/yajl.c +164 -0
  35. data/ext/yajl_alloc.c +65 -0
  36. data/ext/yajl_alloc.h +50 -0
  37. data/ext/yajl_buf.c +119 -0
  38. data/ext/yajl_buf.h +73 -0
  39. data/ext/yajl_bytestack.h +85 -0
  40. data/ext/yajl_encode.c +188 -0
  41. data/ext/yajl_encode.h +50 -0
  42. data/ext/yajl_ext.c +911 -0
  43. data/ext/yajl_ext.h +128 -0
  44. data/ext/yajl_gen.c +317 -0
  45. data/ext/yajl_lex.c +747 -0
  46. data/ext/yajl_lex.h +135 -0
  47. data/ext/yajl_parser.c +450 -0
  48. data/ext/yajl_parser.h +82 -0
  49. data/lib/yajl/bzip2/stream_reader.rb +32 -0
  50. data/lib/yajl/bzip2/stream_writer.rb +15 -0
  51. data/lib/yajl/bzip2.rb +11 -0
  52. data/lib/yajl/deflate/stream_reader.rb +44 -0
  53. data/lib/yajl/deflate/stream_writer.rb +21 -0
  54. data/lib/yajl/deflate.rb +6 -0
  55. data/lib/yajl/gzip/stream_reader.rb +31 -0
  56. data/lib/yajl/gzip/stream_writer.rb +14 -0
  57. data/lib/yajl/gzip.rb +6 -0
  58. data/lib/yajl/http_stream.rb +197 -0
  59. data/lib/yajl/json_gem/encoding.rb +50 -0
  60. data/lib/yajl/json_gem/parsing.rb +27 -0
  61. data/lib/yajl/json_gem.rb +14 -0
  62. data/lib/yajl.rb +93 -0
  63. data/spec/encoding/encoding_spec.rb +234 -0
  64. data/spec/global/global_spec.rb +55 -0
  65. data/spec/http/fixtures/http.bzip2.dump +0 -0
  66. data/spec/http/fixtures/http.chunked.dump +11 -0
  67. data/spec/http/fixtures/http.deflate.dump +0 -0
  68. data/spec/http/fixtures/http.error.dump +12 -0
  69. data/spec/http/fixtures/http.gzip.dump +0 -0
  70. data/spec/http/fixtures/http.html.dump +1220 -0
  71. data/spec/http/fixtures/http.raw.dump +1226 -0
  72. data/spec/http/http_delete_spec.rb +99 -0
  73. data/spec/http/http_error_spec.rb +33 -0
  74. data/spec/http/http_get_spec.rb +110 -0
  75. data/spec/http/http_post_spec.rb +124 -0
  76. data/spec/http/http_put_spec.rb +106 -0
  77. data/spec/json_gem_compatibility/compatibility_spec.rb +203 -0
  78. data/spec/parsing/active_support_spec.rb +64 -0
  79. data/spec/parsing/chunked_spec.rb +98 -0
  80. data/spec/parsing/fixtures/fail.15.json +1 -0
  81. data/spec/parsing/fixtures/fail.16.json +1 -0
  82. data/spec/parsing/fixtures/fail.17.json +1 -0
  83. data/spec/parsing/fixtures/fail.26.json +1 -0
  84. data/spec/parsing/fixtures/fail11.json +1 -0
  85. data/spec/parsing/fixtures/fail12.json +1 -0
  86. data/spec/parsing/fixtures/fail13.json +1 -0
  87. data/spec/parsing/fixtures/fail14.json +1 -0
  88. data/spec/parsing/fixtures/fail19.json +1 -0
  89. data/spec/parsing/fixtures/fail20.json +1 -0
  90. data/spec/parsing/fixtures/fail21.json +1 -0
  91. data/spec/parsing/fixtures/fail22.json +1 -0
  92. data/spec/parsing/fixtures/fail23.json +1 -0
  93. data/spec/parsing/fixtures/fail24.json +1 -0
  94. data/spec/parsing/fixtures/fail25.json +1 -0
  95. data/spec/parsing/fixtures/fail27.json +2 -0
  96. data/spec/parsing/fixtures/fail28.json +2 -0
  97. data/spec/parsing/fixtures/fail3.json +1 -0
  98. data/spec/parsing/fixtures/fail4.json +1 -0
  99. data/spec/parsing/fixtures/fail5.json +1 -0
  100. data/spec/parsing/fixtures/fail6.json +1 -0
  101. data/spec/parsing/fixtures/fail9.json +1 -0
  102. data/spec/parsing/fixtures/pass.array.json +6 -0
  103. data/spec/parsing/fixtures/pass.codepoints_from_unicode_org.json +1 -0
  104. data/spec/parsing/fixtures/pass.contacts.json +1 -0
  105. data/spec/parsing/fixtures/pass.db100.xml.json +1 -0
  106. data/spec/parsing/fixtures/pass.db1000.xml.json +1 -0
  107. data/spec/parsing/fixtures/pass.dc_simple_with_comments.json +11 -0
  108. data/spec/parsing/fixtures/pass.deep_arrays.json +1 -0
  109. data/spec/parsing/fixtures/pass.difficult_json_c_test_case.json +1 -0
  110. data/spec/parsing/fixtures/pass.difficult_json_c_test_case_with_comments.json +1 -0
  111. data/spec/parsing/fixtures/pass.doubles.json +1 -0
  112. data/spec/parsing/fixtures/pass.empty_array.json +1 -0
  113. data/spec/parsing/fixtures/pass.empty_string.json +1 -0
  114. data/spec/parsing/fixtures/pass.escaped_bulgarian.json +4 -0
  115. data/spec/parsing/fixtures/pass.escaped_foobar.json +1 -0
  116. data/spec/parsing/fixtures/pass.item.json +1 -0
  117. data/spec/parsing/fixtures/pass.json-org-sample1.json +23 -0
  118. data/spec/parsing/fixtures/pass.json-org-sample2.json +11 -0
  119. data/spec/parsing/fixtures/pass.json-org-sample3.json +26 -0
  120. data/spec/parsing/fixtures/pass.json-org-sample4-nows.json +88 -0
  121. data/spec/parsing/fixtures/pass.json-org-sample4.json +89 -0
  122. data/spec/parsing/fixtures/pass.json-org-sample5.json +27 -0
  123. data/spec/parsing/fixtures/pass.map-spain.xml.json +1 -0
  124. data/spec/parsing/fixtures/pass.ns-invoice100.xml.json +1 -0
  125. data/spec/parsing/fixtures/pass.ns-soap.xml.json +1 -0
  126. data/spec/parsing/fixtures/pass.numbers-fp-4k.json +6 -0
  127. data/spec/parsing/fixtures/pass.numbers-fp-64k.json +61 -0
  128. data/spec/parsing/fixtures/pass.numbers-int-4k.json +11 -0
  129. data/spec/parsing/fixtures/pass.numbers-int-64k.json +154 -0
  130. data/spec/parsing/fixtures/pass.twitter-search.json +1 -0
  131. data/spec/parsing/fixtures/pass.twitter-search2.json +1 -0
  132. data/spec/parsing/fixtures/pass.unicode.json +3315 -0
  133. data/spec/parsing/fixtures/pass.yelp.json +1 -0
  134. data/spec/parsing/fixtures/pass1.json +56 -0
  135. data/spec/parsing/fixtures/pass2.json +1 -0
  136. data/spec/parsing/fixtures/pass3.json +6 -0
  137. data/spec/parsing/fixtures_spec.rb +41 -0
  138. data/spec/parsing/one_off_spec.rb +81 -0
  139. data/spec/rcov.opts +3 -0
  140. data/spec/spec.opts +2 -0
  141. data/spec/spec_helper.rb +16 -0
  142. data/yajl-ruby.gemspec +203 -0
  143. metadata +232 -0
@@ -0,0 +1 @@
1
+ {"results":[{"text":"#ruby \u732e\u672c\u30ad\u30bf\u30fc\u30fc\u30fc\u30fc\u30fc\u30fc\uff08\u309c\u2200\u309c\uff09\u30fc\u30fc\u30fc\u30fc\u30fc\u30fc\uff01\uff01 http:\/\/www.amazon.co.jp\/gp\/product\/4863540221","to_user_id":null,"from_user":"rubikitch","id":1843394737,"from_user_id":847295,"iso_language_code":"ja","source":"<a href="http:\/\/www.misuzilla.org\/dist\/net\/twitterircgateway\/">TwitterIrcGateway<\/a>","profile_image_url":"http:\/\/static.twitter.com\/images\/default_profile_normal.png","created_at":"Tue, 19 May 2009 03:12:47 +0000"},{"text":"Ruby on Rails\u3068CakePHP\u3001\u3069\u3063\u3061\u3067\u88fd\u9020\u3057\u3088\u3046\u304b\u3057\u3089","to_user_id":null,"from_user":"takkada","id":1842694220,"from_user_id":412519,"iso_language_code":"ja","source":"<a href="http:\/\/twitterfox.net\/">TwitterFox<\/a>","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/16301882\/62305_4124481745_normal.jpg","created_at":"Tue, 19 May 2009 02:00:08 +0000"},{"text":"Ruby on Rails \u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u306f\u30fc\u3058\u307e\u30fc\u308b\u3088\u30fc","to_user_id":null,"from_user":"Tomohiro","id":1842375120,"from_user_id":38704,"iso_language_code":"ja","source":"<a href="http:\/\/www.misuzilla.org\/dist\/net\/twitterircgateway\/">TwitterIrcGateway<\/a>","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/20970892\/578576_3772887925_normal.jpg","created_at":"Tue, 19 May 2009 01:27:18 +0000"},{"text":"ruby winole.rb\u3067\u547c\u3073\u51fa\u3057\u305f\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306e\u30e1\u30e2\u30ea\u304c\u9069\u5207\u306b\u958b\u653e\u3055\u308c\u3066\u306a\u304b\u3063\u305f\u3002","to_user_id":null,"from_user":"tomofusa","id":1841955169,"from_user_id":35354,"iso_language_code":"ja","source":"<a href="http:\/\/cheebow.info\/chemt\/archives\/2007\/04\/twitterwindowst.html">Twit<\/a>","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/18213412\/fusa2.0_about53_normal.png","created_at":"Tue, 19 May 2009 00:43:20 +0000"},{"text":"Ruby HTTP\u4e26\u5217\u3092\u51e6\u7406\u3092\u9ad8\u901f\u5316\u3001"Typhoeus"\u767b\u5834 | \u30a8\u30f3\u30bf\u30fc\u30d7\u30e9\u30a4\u30ba | \u30de\u30a4\u30b3\u30df\u30b8\u30e3\u30fc\u30ca\u30eb http:\/\/ff.im\/-31cez","to_user_id":null,"from_user":"MyGReaderFeed","id":1841206557,"from_user_id":11923042,"iso_language_code":"ja","source":"<a href="http:\/\/friendfeed.com\/">FriendFeed<\/a>","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/141487706\/3379573163_e79dab7511_normal.bmp","created_at":"Mon, 18 May 2009 23:22:26 +0000"},{"text":"\u3044\u305a\u308c\u306b\u3057\u3066\u3082\u3069\u3053\u304b\u306b Ruby 1.9.1 \u74b0\u5883\u306f\u4f5c\u3089\u306a\u3044\u3068\u30c0\u30e1\u304b\u306a\u3041\u3002Debian lenny \u4efb\u305b\u3060\u3068 1.9.0 \u3057\u304b\u8a66\u305b\u306a\u3044\u3063\u307d\u3044\u3057\u3002","to_user_id":null,"from_user":"wtnabe","id":1840166013,"from_user_id":32040,"iso_language_code":"ja","source":"<a href="http:\/\/www.misuzilla.org\/dist\/net\/twitterircgateway\/">TwitterIrcGateway<\/a>","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/59828670\/200805-wtnabe-film_normal.png","created_at":"Mon, 18 May 2009 21:26:17 +0000"},{"text":"Ruby 1.9 \u3067\u306f URI#decode \u3060\u3051\u3058\u3083\u610f\u5473\u304c\u306a\u3044\u306e\u304b\u306a\uff1f \u3053\u306e\u8fba\u304b\u3089\u5206\u304b\u3089\u306a\u304f\u306a\u3063\u3066\u304f\u308b\u306a\u3002","to_user_id":null,"from_user":"wtnabe","id":1839967007,"from_user_id":32040,"iso_language_code":"ja","source":"<a href="http:\/\/www.misuzilla.org\/dist\/net\/twitterircgateway\/">TwitterIrcGateway<\/a>","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/59828670\/200805-wtnabe-film_normal.png","created_at":"Mon, 18 May 2009 21:06:06 +0000"},{"text":"Star Ruby \u3067\u30d5\u30ec\u30fc\u30e0\u30b9\u30ad\u30c3\u30d7\u304c\u5b9f\u88c5\u3057\u306b\u304f\u3044\u7406\u7531\u306e\u3072\u3068\u3064\u306b\u3001\u30b9\u30af\u30ea\u30fc\u30f3\u3078\u306e\u63cf\u753b\u304c\u9045\u5ef6\u3067\u304d\u306a\u3044\u3068\u3044\u3046\u306e\u304c\u3042\u308b\u3002 Texture#[] \u306a\u3069\u3067\u30d4\u30af\u30bb\u30eb\u306e\u5024\u3092\u8aad\u307f\u53d6\u3089\u308c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u305f\u3081\u3002\u8aad\u307f\u53d6\u3089\u308c\u308b\u30ae\u30ea\u30ae\u30ea\u307e\u3067\u9045\u5ef6\u3059\u308b\u306e\u3082\u30a2\u30ea\u3060\u304c\u3001\u5b9f\u88c5\u306f\u9762\u5012\u3067\u3042\u308b\u3002","to_user_id":null,"from_user":"hajimehoshi","id":1837784833,"from_user_id":7543,"iso_language_code":"ja","source":"<a href="http:\/\/twitter.com\/">web<\/a>","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/14446892\/michael_normal.jpg","created_at":"Mon, 18 May 2009 17:21:16 +0000"},{"text":"ruby-gem\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3066\u3001rails\u306b\u30c1\u30e3\u30ec\u30f3\u30b8","to_user_id":null,"from_user":"tom_a","id":1837459653,"from_user_id":366098,"iso_language_code":"ja","source":"<a href="http:\/\/twitterfox.net\/">TwitterFox<\/a>","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/16661182\/DSC02701_normal.jpg","created_at":"Mon, 18 May 2009 16:46:36 +0000"},{"text":"@saronpasu \u4e2d\u8eab\u306b\u30a2\u30af\u30bb\u30b9\u3057\u306b\u304f\u3044\u3057\u3001\u4f55\u3088\u308a\u4e0d\u5b89\u5b9a\u3059\u304e\u3067\u3059\u306d\u2026\u4f8b\u5916\u3063\u3066\u30ec\u30d9\u30eb\u3058\u3083\u306a\u304f\u3066 ruby \u5dfb\u304d\u8fbc\u3093\u3067\u843d\u3061\u308b\u306e\u3067\u2026","to_user_id":28082,"to_user":"saronpasu","from_user":"negaton","id":1836710607,"from_user_id":5893,"iso_language_code":"ja","source":"<a href="http:\/\/cheebow.info\/chemt\/archives\/2007\/04\/twitterwindowst.html">Twit<\/a>","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/35934732\/DSC00338_icon_normal.jpg","created_at":"Mon, 18 May 2009 15:28:25 +0000"},{"text":"\u6628\u65e5 CaboCha-Ruby \u306e token \u306e surface \u3092\u53e9\u3044\u305f\u3089\u843d\u3061\u305f\u306e\u306b\u4eca\u65e5\u306f\u5168\u304f\u540c\u3058\u30b3\u30fc\u30c9\u52d5\u304b\u3057\u3066\u3066\u843d\u3061\u306a\u3044\u3068\u304b\u306a\u3093\u306a\u306e\u2026","to_user_id":null,"from_user":"negaton","id":1836267822,"from_user_id":5893,"iso_language_code":"ja","source":"<a href="http:\/\/cheebow.info\/chemt\/archives\/2007\/04\/twitterwindowst.html">Twit<\/a>","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/35934732\/DSC00338_icon_normal.jpg","created_at":"Mon, 18 May 2009 14:41:26 +0000"},{"text":"ruby.snippets \u3092\u773a\u3081\u3066\u307f\u3066\u3001\u3053\u308c\u306f\u81ea\u5206\u304c\u4f7f\u3044\u3053\u306a\u305b\u308b\u3082\u306e\u3067\u306f\u306a\u3044\u3068\u308f\u304b\u3063\u305f","to_user_id":null,"from_user":"ursm","id":1835699306,"from_user_id":41919,"iso_language_code":"ja","source":"<a href="http:\/\/www.nambu.com">Nambu<\/a>","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/28792412\/1153147983506_normal.jpg","created_at":"Mon, 18 May 2009 13:33:59 +0000"},{"text":"\u3068\u3053\u308d\u3067\u3001PHP \u304b\u3089\u306f Ruby \u3063\u3066\u547c\u3079\u308b\u306e\uff1f","to_user_id":null,"from_user":"nov","id":1835354604,"from_user_id":76705,"iso_language_code":"ja","source":"<a href="http:\/\/twitterfon.net\/">TwitterFon<\/a>","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/60572614\/nov_normal.gif","created_at":"Mon, 18 May 2009 12:47:52 +0000"},{"text":"\u3067\u304d\u3042\u304c\u3063\u305f\u3053\u308d shindig ruby ver. \u51fa\u3066\u305f\u308a\u3057\u305f\u3089\u6ce3\u304f\u3002","to_user_id":null,"from_user":"nov","id":1835155373,"from_user_id":76705,"iso_language_code":"ja","source":"<a href="http:\/\/twitterfon.net\/">TwitterFon<\/a>","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/60572614\/nov_normal.gif","created_at":"Mon, 18 May 2009 12:17:17 +0000"},{"text":"@papiron ruby-dev\u3067ruby1.9\u306bsqlite\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u6a19\u6e96\u6dfb\u4ed8\u3057\u3088\u3046\u3068\u3044\u3046\u8b70\u8ad6\u304c\u3055\u308c\u3066\u307e\u3059\u3002http:\/\/bit.ly\/Limi9","to_user_id":10226526,"to_user":"papiron","from_user":"taigou","id":1834625775,"from_user_id":3915244,"iso_language_code":"ja","source":"<a href="http:\/\/twitterfox.net\/">TwitterFox<\/a>","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/95583044\/me_normal.png","created_at":"Mon, 18 May 2009 10:38:40 +0000"}],"since_id":1769782474,"max_id":1843394737,"refresh_url":"?since_id=1843394737&q=ruby","results_per_page":15,"next_page":"?page=2&max_id=1843394737&lang=ja&q=ruby","warning":"adjusted since_id, it was older than allowedsince_id removed for pagination.","completed_in":0.052502,"page":1,"query":"ruby"}
@@ -0,0 +1,27 @@
1
+ # encoding: UTF-8
2
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../../lib')
3
+
4
+ require 'yajl'
5
+
6
+ obj = {
7
+ :a_test => "of encoding in one pass" * 512,
8
+ :a_test2 => "of encoding in one pass" * 512,
9
+ :a_test3 => "of encoding in one pass" * 512,
10
+ :a_test4 => "of encoding in one pass" * 512,
11
+ :which_will => "simply return a string when finished" * 512,
12
+ :which_will2 => "simply return a string when finished" * 512,
13
+ :which_will3 => "simply return a string when finished" * 512,
14
+ :which_will4 => "simply return a string when finished" * 512,
15
+ :as_easy_as => 123
16
+ }
17
+
18
+ chunks = 0
19
+ total_size = 0
20
+
21
+ Yajl::Encoder.encode(obj) do |chunk|
22
+ chunks += 1
23
+ total_size += chunk.size
24
+ STDOUT << chunk
25
+ end
26
+
27
+ puts "\n\nEncoder generated #{total_size} bytes of data, in #{chunks} chunks"
@@ -0,0 +1,13 @@
1
+ # encoding: UTF-8
2
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../../lib')
3
+
4
+ require 'yajl'
5
+
6
+ obj = {
7
+ :a_test => "of encoding in one pass",
8
+ :which_will => "simply return a string when finished",
9
+ :as_easy_as => 123
10
+ }
11
+
12
+ str = Yajl::Encoder.encode(obj)
13
+ puts str
@@ -0,0 +1,12 @@
1
+ # encoding: UTF-8
2
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../../lib')
3
+
4
+ require 'yajl'
5
+
6
+ obj = {
7
+ :a_test => "of encoding directly to an IO stream",
8
+ :which_will => "simply return a string when finished",
9
+ :as_easy_as => 123
10
+ }
11
+
12
+ Yajl::Encoder.encode(obj, STDOUT)
@@ -0,0 +1,12 @@
1
+ # encoding: UTF-8
2
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../../lib')
3
+
4
+ require 'yajl/http_stream'
5
+ require 'uri'
6
+
7
+ unless keywords = ARGV[0]
8
+ puts "\nUsage: ruby examples/http/twitter_search_api.rb keyword\n\n"
9
+ exit(0)
10
+ end
11
+
12
+ puts Yajl::HttpStream.get("http://search.twitter.com/search.json?q=#{keywords}").inspect
@@ -0,0 +1,26 @@
1
+ # encoding: UTF-8
2
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../../lib')
3
+
4
+ require 'yajl/gzip'
5
+ require 'yajl/deflate'
6
+ require 'yajl/http_stream'
7
+ require 'uri'
8
+
9
+ unless (username = ARGV[0]) && (password = ARGV[1])
10
+ puts "\nUsage: ruby examples/http/twitter_stream_api.rb username password\n\n"
11
+ exit(0)
12
+ end
13
+ captured = 0
14
+ uri = URI.parse("http://#{username}:#{password}@stream.twitter.com/1/statuses/sample.json")
15
+
16
+ trap('INT') {
17
+ puts "\n\nCaptured #{captured} objects from the stream"
18
+ puts "CTRL+C caught, later!"
19
+ exit(0)
20
+ }
21
+
22
+ Yajl::HttpStream.get(uri, :symbolize_keys => true) do |hash|
23
+ STDOUT.putc '.'
24
+ STDOUT.flush
25
+ captured += 1
26
+ end
@@ -0,0 +1,14 @@
1
+ # encoding: UTF-8
2
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../../lib')
3
+
4
+ require 'yajl'
5
+
6
+ unless file = ARGV[0]
7
+ puts "\nUsage: ruby examples/from_file.rb benchmark/subjects/item.json\n\n"
8
+ exit(0)
9
+ end
10
+
11
+ json = File.new(file, 'r')
12
+
13
+ hash = Yajl::Parser.parse(json)
14
+ puts hash.inspect
@@ -0,0 +1,9 @@
1
+ # encoding: UTF-8
2
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../../lib')
3
+
4
+ require 'yajl'
5
+
6
+ # Usage: cat benchmark/subjects/item.json | ruby examples/from_stdin.rb
7
+
8
+ hash = Yajl::Parser.parse(STDIN)
9
+ puts hash.inspect
@@ -0,0 +1,13 @@
1
+ # encoding: UTF-8
2
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../../lib')
3
+
4
+ require 'yajl'
5
+ require 'stringio'
6
+
7
+ unless string = ARGV[0]
8
+ puts "\nUsage: ruby examples/from_string.rb '{\"foo\": 1145}'\n\n"
9
+ exit(0)
10
+ end
11
+
12
+ hash = Yajl::Parser.parse(string)
13
+ puts hash.inspect
@@ -0,0 +1,85 @@
1
+ /*
2
+ * Copyright 2010, Lloyd Hilaiel.
3
+ *
4
+ * Redistribution and use in source and binary forms, with or without
5
+ * modification, are permitted provided that the following conditions are
6
+ * met:
7
+ *
8
+ * 1. Redistributions of source code must retain the above copyright
9
+ * notice, this list of conditions and the following disclaimer.
10
+ *
11
+ * 2. Redistributions in binary form must reproduce the above copyright
12
+ * notice, this list of conditions and the following disclaimer in
13
+ * the documentation and/or other materials provided with the
14
+ * distribution.
15
+ *
16
+ * 3. Neither the name of Lloyd Hilaiel nor the names of its
17
+ * contributors may be used to endorse or promote products derived
18
+ * from this software without specific prior written permission.
19
+ *
20
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
+ * POSSIBILITY OF SUCH DAMAGE.
31
+ */
32
+
33
+ #ifndef __YAJL_COMMON_H__
34
+ #define __YAJL_COMMON_H__
35
+
36
+ #ifdef __cplusplus
37
+ extern "C" {
38
+ #endif
39
+
40
+ #define YAJL_MAX_DEPTH 128
41
+
42
+ /* msft dll export gunk. To build a DLL on windows, you
43
+ * must define WIN32, YAJL_SHARED, and YAJL_BUILD. To use a shared
44
+ * DLL, you must define YAJL_SHARED and WIN32 */
45
+ #if defined(WIN32) && defined(YAJL_SHARED)
46
+ # ifdef YAJL_BUILD
47
+ # define YAJL_API __declspec(dllexport)
48
+ # else
49
+ # define YAJL_API __declspec(dllimport)
50
+ # endif
51
+ #else
52
+ # define YAJL_API
53
+ #endif
54
+
55
+ /** pointer to a malloc function, supporting client overriding memory
56
+ * allocation routines */
57
+ typedef void * (*yajl_malloc_func)(void *ctx, unsigned int sz);
58
+
59
+ /** pointer to a free function, supporting client overriding memory
60
+ * allocation routines */
61
+ typedef void (*yajl_free_func)(void *ctx, void * ptr);
62
+
63
+ /** pointer to a realloc function which can resize an allocation. */
64
+ typedef void * (*yajl_realloc_func)(void *ctx, void * ptr, unsigned int sz);
65
+
66
+ /** A structure which can be passed to yajl_*_alloc routines to allow the
67
+ * client to specify memory allocation functions to be used. */
68
+ typedef struct
69
+ {
70
+ /** pointer to a function that can allocate uninitialized memory */
71
+ yajl_malloc_func malloc;
72
+ /** pointer to a function that can resize memory allocations */
73
+ yajl_realloc_func realloc;
74
+ /** pointer to a function that can free memory allocated using
75
+ * reallocFunction or mallocFunction */
76
+ yajl_free_func free;
77
+ /** a context pointer that will be passed to above allocation routines */
78
+ void * ctx;
79
+ } yajl_alloc_funcs;
80
+
81
+ #ifdef __cplusplus
82
+ }
83
+ #endif
84
+
85
+ #endif
@@ -0,0 +1,159 @@
1
+ /*
2
+ * Copyright 2010, Lloyd Hilaiel.
3
+ *
4
+ * Redistribution and use in source and binary forms, with or without
5
+ * modification, are permitted provided that the following conditions are
6
+ * met:
7
+ *
8
+ * 1. Redistributions of source code must retain the above copyright
9
+ * notice, this list of conditions and the following disclaimer.
10
+ *
11
+ * 2. Redistributions in binary form must reproduce the above copyright
12
+ * notice, this list of conditions and the following disclaimer in
13
+ * the documentation and/or other materials provided with the
14
+ * distribution.
15
+ *
16
+ * 3. Neither the name of Lloyd Hilaiel nor the names of its
17
+ * contributors may be used to endorse or promote products derived
18
+ * from this software without specific prior written permission.
19
+ *
20
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
+ * POSSIBILITY OF SUCH DAMAGE.
31
+ */
32
+
33
+ /**
34
+ * \file yajl_gen.h
35
+ * Interface to YAJL's JSON generation facilities.
36
+ */
37
+
38
+ #include "api/yajl_common.h"
39
+
40
+ #ifndef __YAJL_GEN_H__
41
+ #define __YAJL_GEN_H__
42
+
43
+ #ifdef __cplusplus
44
+ extern "C" {
45
+ #endif
46
+ /** generator status codes */
47
+ typedef enum {
48
+ /** no error */
49
+ yajl_gen_status_ok = 0,
50
+ /** at a point where a map key is generated, a function other than
51
+ * yajl_gen_string was called */
52
+ yajl_gen_keys_must_be_strings,
53
+ /** YAJL's maximum generation depth was exceeded. see
54
+ * YAJL_MAX_DEPTH */
55
+ yajl_max_depth_exceeded,
56
+ /** A generator function (yajl_gen_XXX) was called while in an error
57
+ * state */
58
+ yajl_gen_in_error_state,
59
+ /** A complete JSON document has been generated */
60
+ yajl_gen_generation_complete,
61
+ /** yajl_gen_double was passed an invalid floating point value
62
+ * (infinity or NaN). */
63
+ yajl_gen_invalid_number,
64
+ /** A print callback was passed in, so there is no internal
65
+ * buffer to get from */
66
+ yajl_gen_no_buf
67
+ } yajl_gen_status;
68
+
69
+ /** an opaque handle to a generator */
70
+ typedef struct yajl_gen_t * yajl_gen;
71
+
72
+ /** a callback used for "printing" the results. */
73
+ typedef void (*yajl_print_t)(void * ctx,
74
+ const char * str,
75
+ unsigned int len);
76
+
77
+ /** configuration structure for the generator */
78
+ typedef struct {
79
+ /** generate indented (beautiful) output */
80
+ unsigned int beautify;
81
+ /** an opportunity to define an indent string. such as \\t or
82
+ * some number of spaces. default is four spaces ' '. This
83
+ * member is only relevant when beautify is true */
84
+ const char * indentString;
85
+ } yajl_gen_config;
86
+
87
+ /** allocate a generator handle
88
+ * \param config a pointer to a structure containing parameters which
89
+ * configure the behavior of the json generator
90
+ * \param allocFuncs an optional pointer to a structure which allows
91
+ * the client to overide the memory allocation
92
+ * used by yajl. May be NULL, in which case
93
+ * malloc/free/realloc will be used.
94
+ *
95
+ * \returns an allocated handle on success, NULL on failure (bad params)
96
+ */
97
+ YAJL_API yajl_gen yajl_gen_alloc(const yajl_gen_config * config,
98
+ const yajl_alloc_funcs * allocFuncs);
99
+
100
+ /** allocate a generator handle that will print to the specified
101
+ * callback rather than storing the results in an internal buffer.
102
+ * \param callback a pointer to a printer function. May be NULL
103
+ * in which case, the results will be store in an
104
+ * internal buffer.
105
+ * \param config a pointer to a structure containing parameters
106
+ * which configure the behavior of the json
107
+ * generator.
108
+ * \param allocFuncs an optional pointer to a structure which allows
109
+ * the client to overide the memory allocation
110
+ * used by yajl. May be NULL, in which case
111
+ * malloc/free/realloc will be used.
112
+ * \param ctx a context pointer that will be passed to the
113
+ * printer callback.
114
+ *
115
+ * \returns an allocated handle on success, NULL on failure (bad params)
116
+ */
117
+ YAJL_API yajl_gen yajl_gen_alloc2(const yajl_print_t callback,
118
+ const yajl_gen_config * config,
119
+ const yajl_alloc_funcs * allocFuncs,
120
+ void * ctx);
121
+
122
+ /** free a generator handle */
123
+ YAJL_API void yajl_gen_free(yajl_gen handle);
124
+
125
+ YAJL_API yajl_gen_status yajl_gen_integer(yajl_gen hand, long int number);
126
+ /** generate a floating point number. number may not be infinity or
127
+ * NaN, as these have no representation in JSON. In these cases the
128
+ * generator will return 'yajl_gen_invalid_number' */
129
+ YAJL_API yajl_gen_status yajl_gen_double(yajl_gen hand, double number);
130
+ YAJL_API yajl_gen_status yajl_gen_number(yajl_gen hand,
131
+ const char * num,
132
+ unsigned int len);
133
+ YAJL_API yajl_gen_status yajl_gen_string(yajl_gen hand,
134
+ const unsigned char * str,
135
+ unsigned int len);
136
+ YAJL_API yajl_gen_status yajl_gen_null(yajl_gen hand);
137
+ YAJL_API yajl_gen_status yajl_gen_bool(yajl_gen hand, int boolean);
138
+ YAJL_API yajl_gen_status yajl_gen_map_open(yajl_gen hand);
139
+ YAJL_API yajl_gen_status yajl_gen_map_close(yajl_gen hand);
140
+ YAJL_API yajl_gen_status yajl_gen_array_open(yajl_gen hand);
141
+ YAJL_API yajl_gen_status yajl_gen_array_close(yajl_gen hand);
142
+
143
+ /** access the null terminated generator buffer. If incrementally
144
+ * outputing JSON, one should call yajl_gen_clear to clear the
145
+ * buffer. This allows stream generation. */
146
+ YAJL_API yajl_gen_status yajl_gen_get_buf(yajl_gen hand,
147
+ const unsigned char ** buf,
148
+ unsigned int * len);
149
+
150
+ /** clear yajl's output buffer, but maintain all internal generation
151
+ * state. This function will not "reset" the generator state, and is
152
+ * intended to enable incremental JSON outputing. */
153
+ YAJL_API void yajl_gen_clear(yajl_gen hand);
154
+
155
+ #ifdef __cplusplus
156
+ }
157
+ #endif
158
+
159
+ #endif
@@ -0,0 +1,196 @@
1
+ /*
2
+ * Copyright 2010, Lloyd Hilaiel.
3
+ *
4
+ * Redistribution and use in source and binary forms, with or without
5
+ * modification, are permitted provided that the following conditions are
6
+ * met:
7
+ *
8
+ * 1. Redistributions of source code must retain the above copyright
9
+ * notice, this list of conditions and the following disclaimer.
10
+ *
11
+ * 2. Redistributions in binary form must reproduce the above copyright
12
+ * notice, this list of conditions and the following disclaimer in
13
+ * the documentation and/or other materials provided with the
14
+ * distribution.
15
+ *
16
+ * 3. Neither the name of Lloyd Hilaiel nor the names of its
17
+ * contributors may be used to endorse or promote products derived
18
+ * from this software without specific prior written permission.
19
+ *
20
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
+ * POSSIBILITY OF SUCH DAMAGE.
31
+ */
32
+
33
+ /**
34
+ * \file yajl_parse.h
35
+ * Interface to YAJL's JSON parsing facilities.
36
+ */
37
+
38
+ #include "api/yajl_common.h"
39
+
40
+ #ifndef __YAJL_PARSE_H__
41
+ #define __YAJL_PARSE_H__
42
+
43
+ #ifdef __cplusplus
44
+ extern "C" {
45
+ #endif
46
+ /** error codes returned from this interface */
47
+ typedef enum {
48
+ /** no error was encountered */
49
+ yajl_status_ok,
50
+ /** a client callback returned zero, stopping the parse */
51
+ yajl_status_client_canceled,
52
+ /** The parse cannot yet complete because more json input text
53
+ * is required, call yajl_parse with the next buffer of input text.
54
+ * (pertinent only when stream parsing) */
55
+ yajl_status_insufficient_data,
56
+ /** An error occured during the parse. Call yajl_get_error for
57
+ * more information about the encountered error */
58
+ yajl_status_error
59
+ } yajl_status;
60
+
61
+ /** attain a human readable, english, string for an error */
62
+ YAJL_API const char * yajl_status_to_string(yajl_status code);
63
+
64
+ /** an opaque handle to a parser */
65
+ typedef struct yajl_handle_t * yajl_handle;
66
+
67
+ /** yajl is an event driven parser. this means as json elements are
68
+ * parsed, you are called back to do something with the data. The
69
+ * functions in this table indicate the various events for which
70
+ * you will be called back. Each callback accepts a "context"
71
+ * pointer, this is a void * that is passed into the yajl_parse
72
+ * function which the client code may use to pass around context.
73
+ *
74
+ * All callbacks return an integer. If non-zero, the parse will
75
+ * continue. If zero, the parse will be canceled and
76
+ * yajl_status_client_canceled will be returned from the parse.
77
+ *
78
+ * Note about handling of numbers:
79
+ * yajl will only convert numbers that can be represented in a double
80
+ * or a long int. All other numbers will be passed to the client
81
+ * in string form using the yajl_number callback. Furthermore, if
82
+ * yajl_number is not NULL, it will always be used to return numbers,
83
+ * that is yajl_integer and yajl_double will be ignored. If
84
+ * yajl_number is NULL but one of yajl_integer or yajl_double are
85
+ * defined, parsing of a number larger than is representable
86
+ * in a double or long int will result in a parse error.
87
+ */
88
+ typedef struct {
89
+ int (* yajl_null)(void * ctx);
90
+ int (* yajl_boolean)(void * ctx, int boolVal);
91
+ int (* yajl_integer)(void * ctx, long integerVal);
92
+ int (* yajl_double)(void * ctx, double doubleVal);
93
+ /** A callback which passes the string representation of the number
94
+ * back to the client. Will be used for all numbers when present */
95
+ int (* yajl_number)(void * ctx, const char * numberVal,
96
+ unsigned int numberLen);
97
+
98
+ /** strings are returned as pointers into the JSON text when,
99
+ * possible, as a result, they are _not_ null padded */
100
+ int (* yajl_string)(void * ctx, const unsigned char * stringVal,
101
+ unsigned int stringLen);
102
+
103
+ int (* yajl_start_map)(void * ctx);
104
+ int (* yajl_map_key)(void * ctx, const unsigned char * key,
105
+ unsigned int stringLen);
106
+ int (* yajl_end_map)(void * ctx);
107
+
108
+ int (* yajl_start_array)(void * ctx);
109
+ int (* yajl_end_array)(void * ctx);
110
+ } yajl_callbacks;
111
+
112
+ /** configuration structure for the generator */
113
+ typedef struct {
114
+ /** if nonzero, javascript style comments will be allowed in
115
+ * the json input, both slash star and slash slash */
116
+ unsigned int allowComments;
117
+ /** if nonzero, invalid UTF8 strings will cause a parse
118
+ * error */
119
+ unsigned int checkUTF8;
120
+ } yajl_parser_config;
121
+
122
+ /** allocate a parser handle
123
+ * \param callbacks a yajl callbacks structure specifying the
124
+ * functions to call when different JSON entities
125
+ * are encountered in the input text. May be NULL,
126
+ * which is only useful for validation.
127
+ * \param config configuration parameters for the parse.
128
+ * \param ctx a context pointer that will be passed to callbacks.
129
+ */
130
+ YAJL_API yajl_handle yajl_alloc(const yajl_callbacks * callbacks,
131
+ const yajl_parser_config * config,
132
+ const yajl_alloc_funcs * allocFuncs,
133
+ void * ctx);
134
+
135
+ /** allow resetting of the lexer without the need to realloc a new parser */
136
+ void yajl_reset_parser(yajl_handle hand);
137
+
138
+ /** free a parser handle */
139
+ void YAJL_API yajl_free(yajl_handle handle);
140
+
141
+ /** Parse some json!
142
+ * \param hand - a handle to the json parser allocated with yajl_alloc
143
+ * \param jsonText - a pointer to the UTF8 json text to be parsed
144
+ * \param jsonTextLength - the length, in bytes, of input text
145
+ */
146
+ YAJL_API yajl_status yajl_parse(yajl_handle hand,
147
+ const unsigned char * jsonText,
148
+ unsigned int jsonTextLength);
149
+
150
+ /** Parse any remaining buffered json.
151
+ * Since yajl is a stream-based parser, without an explicit end of
152
+ * input, yajl sometimes can't decide if content at the end of the
153
+ * stream is valid or not. For example, if "1" has been fed in,
154
+ * yajl can't know whether another digit is next or some character
155
+ * that would terminate the integer token.
156
+ *
157
+ * \param hand - a handle to the json parser allocated with yajl_alloc
158
+ */
159
+ YAJL_API yajl_status yajl_parse_complete(yajl_handle hand);
160
+
161
+ /** get an error string describing the state of the
162
+ * parse.
163
+ *
164
+ * If verbose is non-zero, the message will include the JSON
165
+ * text where the error occured, along with an arrow pointing to
166
+ * the specific char.
167
+ *
168
+ * \returns A dynamically allocated string will be returned which should
169
+ * be freed with yajl_free_error
170
+ */
171
+ YAJL_API unsigned char * yajl_get_error(yajl_handle hand, int verbose,
172
+ const unsigned char * jsonText,
173
+ unsigned int jsonTextLength);
174
+
175
+ /**
176
+ * get the amount of data consumed from the last chunk passed to YAJL.
177
+ *
178
+ * In the case of a successful parse this can help you understand if
179
+ * the entire buffer was consumed (which will allow you to handle
180
+ * "junk at end of input".
181
+ *
182
+ * In the event an error is encountered during parsing, this function
183
+ * affords the client a way to get the offset into the most recent
184
+ * chunk where the error occured. 0 will be returned if no error
185
+ * was encountered.
186
+ */
187
+ YAJL_API unsigned int yajl_get_bytes_consumed(yajl_handle hand);
188
+
189
+ /** free an error returned from yajl_get_error */
190
+ YAJL_API void yajl_free_error(yajl_handle hand, unsigned char * str);
191
+
192
+ #ifdef __cplusplus
193
+ }
194
+ #endif
195
+
196
+ #endif
data/ext/extconf.rb ADDED
@@ -0,0 +1,9 @@
1
+ # encoding: UTF-8
2
+ require 'mkmf'
3
+ require 'rbconfig'
4
+
5
+ $CFLAGS << ' -Wall -funroll-loops'
6
+ $CFLAGS << ' -Wextra' if ENV['DEBUG']
7
+ # $CFLAGS << ' -O0 -ggdb'
8
+
9
+ create_makefile("yajl_ext")