Skip to content

Instantly share code, notes, and snippets.

module Yajl
module Stream
def self.encode(obj)
case obj.class.name
when "Hash"
val = "{"
val << obj.keys.map do |key|
"\"#{key}\": #{encode(obj[key])}"
end * ", "
val << "}"
require 'lib/yajl/http_stream'
require 'uri'
uri = URI.parse('http://jchrisa.net/toast/_all_docs_by_seq?include_docs=true')
Yajl::HttpStream.get(uri) do |hash|
# will take a few seconds, since the response is a single ~4MB JSON string
# if the response was a bunch of individual JSON strings, this would be fired
# for every string parsed off the stream, as they were parsed
puts hash.inspect
end
function messageInsertedCallback(message) {
// ... blah blah blah, I'm a dirty tramp
}
message = Message.parse(raw_data);
inserter = new DatabaseInserter();
inserter.onMessageInsert = messageInsertedCallback;
DatabaseInserter.insert(message);
# Patch to ActiveResource (based on the 2.3.2 codebase) for including the class name in the JSON generated.
# This is so the receiving end (Rails) can more easily manage the params hash.
#
# The current implementation sends the attributes as top-level JSON keys, which ends up on the other end as:
# params[:some_attribute]
# params[:some_other_attribute]
# params[:something_else] # => not related to the posted JSON
#
# This makes it hard to do things like:
# obj = SomeModel.new(params[:some_model])
# Comparing JSON parsing (yajl-ruby) and file sizes against YAML (Syck),
# Marshal, and XML (Libxml and Nokogiri) using ruby 1.8.6 (2008-08-11 patchlevel 287) [universal-darwin9.0]
# It should be noted that for this test Yajl, the JSON gem, YAML and Marshal all return Ruby objects (in this case, a Ruby Hash).
# But both the Libxml and Nokogiri tests merely ran a #load call, which returns their respective XMLDocument classes.
# In order to be *fully* accurate I had to convert said document to a hash (in pure ruby) which is
# painfully slow. But made the test more accurate.
== Raw
JSON(Yajl): contacts.json 2405005 bytes
0.110000 0.010000 0.120000 ( 0.123369)
# Override the default db:drop and db:create Rake tasks to fix bug with using a database driver
# other than mysql, postgres or sqlite.
namespace :db do
task :create do
@charset = ENV['CHARSET'] || 'utf8'
@collation = ENV['COLLATION'] || 'utf8_general_ci'
config = ActiveRecord::Base.configurations[RAILS_ENV || 'development']
begin
ActiveRecord::Base.establish_connection(config.merge('database' => nil))
ActiveRecord::Base.connection.create_database(config['database'], :charset => (config['charset'] || @charset), :collation => (config['collation'] || @collation))
# encoding: UTF-8
# This was ported from Yajl (http://github.com/lloyd/yajl)
# The reason for this is because Ruby 1.8's pack/unpack("U"), nor 1.9's native Unicode
# implementation seemed to support surrogate characters (I may be wrong about that...)
# The example below would throw exceptions on *every* attempt I'd tried to decode it.
# But Yajl decodes it fine, so I ported it's decoding logic into pure Ruby for us all
# to enjoy :)
#
#
# Starting benchmark parsing 2405005 bytes of JSON data 1 times
Yajl::FFI.parse (FFI)
13.530000 8.170000 21.700000 ( 21.802142)
Yajl::Parser.parse (C, from an IO)
0.180000 0.010000 0.190000 ( 0.200328)
Yajl::Parser.parse (C, from a String)
0.200000 0.020000 0.220000 ( 0.214135)
JSON.parse (C)
0.270000 0.020000 0.290000 ( 0.291869)
module EventMachine
module Protocols
module JsonProtocol
def post_init
@parser = Yajl::Parser.new
@parser.on_parse_complete = method(:receive_object)
@encoder = Yajl::Encoder.new
end
def receive_data(data)
>> Time.now.to_json
=> "\"2009-08-25T13:30:18-07:00\""
>> require 'yajl/json_gem'
>> Time.now.to_json
=> "Tue Aug 25 13:30:26 -0700 2009"