Skip to content
This repository has been archived by the owner on Nov 10, 2017. It is now read-only.

Commit

Permalink
adding initial readme, adding initial encoder code
Browse files Browse the repository at this point in the history
  • Loading branch information
brianmario committed Aug 20, 2009
1 parent 94650ad commit 03e9844
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
14 changes: 14 additions & 0 deletions README.rdoc
@@ -0,0 +1,14 @@
= WARNING: THIS CODE IS EXPERIMENTAL AT THE MOMENT

= JSON Machine

A modern, pure-ruby streaming JSON parser/encoder modeled after yajl-ruby.

My intention is to create a pure-ruby, SAX-like, streaming JSON parser that allows the caller to build their own objects while parsing is happening.
For example, by overriding the "found_string(str)" method, the caller can scan and parse out other types of objects (like say, Time or Date).

I have the parser working, and according to my testing it's able to generate matching output to the JSON gem, ActiveSupport and yajl-ruby but with one important difference.
It has better UTF-8 support (specifically regarding surrogate character sequences) and [will eventually be] able to parse and encode to/from a stream in chunks.

Now that I have the parser working, my next step is to go back over the implementation and optimize the shit out of it.
Help/suggestions would be greatly appreciated :)
10 changes: 5 additions & 5 deletions benchmark/parser.rb
Expand Up @@ -6,7 +6,7 @@
# require 'json/pure'
# if we require activesupport, it'll unconditionally require the C version of the JSON gem
# this benchmark wants to use the pure ruby version
# require 'activesupport'
require 'activesupport'
require File.join(File.dirname(__FILE__), '..', 'lib', 'json_machine')

json = File.read(ARGV[0])
Expand All @@ -27,8 +27,8 @@
# JSON.parse(json, :max_nesting => false)
# end

# puts "ActiveSupport"
# x.report do
# active_support = ActiveSupport::JSON.decode(json)
# end
puts "ActiveSupport"
x.report do
active_support = ActiveSupport::JSON.decode(json)
end
end
18 changes: 17 additions & 1 deletion lib/json_machine/encoder.rb
Expand Up @@ -5,7 +5,23 @@ class Encoder
def initialize(opts={})
end

def encode(obj, str_or_io=nil)
def encode(obj)
case obj.class.name
when "Hash"
val = "{"
val << obj.keys.map do |key|
"\"#{key}\": #{encode(obj[key])}"
end * ", "
val << "}"
when "Array"
"[#{obj.map{|val| encode(val)} * ', '}]"
when "NilClass"
"null"
when "TrueClass", "FalseClass", "Fixnum", "Float"
obj.to_s
else
"\"#{obj.to_s}\""
end
end
end
end

0 comments on commit 03e9844

Please sign in to comment.