Permalink
Browse files

changed the JSON objective code to be less strict and use a dynlib

  • Loading branch information...
mattetti committed Jan 24, 2009
1 parent 5182d9a commit 2bd0b072c08ff59ceecdda10a0b6e63fda2df29d
View
@@ -1,9 +1,6 @@
class Array
def to_json(*args)
return self if self.empty?
- errorp = Pointer.new_with_type('@')
- obj = JSON.sbjson.stringWithFragment(self, error:errorp)
- raise errorp[0].description if errorp[0]
- obj
+ self.toJson
end
end
View
@@ -1,5 +1,5 @@
module Enumerable
def to_json(*a)
- to_a.to_json(*a)
+ self.is_a?(Hash) ? self.jsonStringValue : to_a.to_json(*a)
end
end
View
@@ -1,8 +1,6 @@
class Hash
def to_json(*args)
- errorp = Pointer.new_with_type('@')
- obj = JSON.sbjson.stringWithFragment(self, error:errorp)
- raise errorp[0].description if errorp[0]
- obj
+ p "Hash to_json"
+ self.jsonStringValue
end
end
View
92 json.rb
@@ -1,5 +1,8 @@
-# Uses the objc JSON framework http://code.brautaset.org/JSON/
-framework "JSON"
+# Check the ObjC source code available in src
+# if you modify the source code, you will need to rebuild
+# the dynlib and rename it BSJON.bundle
+require File.join(File.dirname(__FILE__), 'lib', 'BSJSON')
+
ext_path = File.join(File.dirname(__FILE__), "core_ext")
require File.join(ext_path, 'array')
require File.join(ext_path, 'object')
@@ -26,15 +29,6 @@ class NestingError < ParserError; end
# This exception is raised, if a generator or unparser error occurs.
class GeneratorError < JSONError; end
- # Returns an instance of SBJSON
- # SBJSON is framework implementing a strict JSON parser and generator in Objective-C.
- #
- # http://code.google.com/p/json-framework/
- #
- def self.sbjson
- @sbjson ||= SBJSON.new
- end
-
# Load a ruby data structure from a JSON _source_ and return it. A source can
# either be a string-like object, an IO like object, or an object responding
# to the read method. If _proc_ was given, it will be called with any nested
@@ -58,21 +52,25 @@ def self.load(source, proc = nil)
source = source.to_io.read
else
source = source.read
- end
- errorp = Pointer.new_with_type('@')
- result = JSON.sbjson.objectWithString(source, error:errorp)
- raise (JSON::ParserError, errorp[0].description) if errorp[0]
- recurse_proc(result, &proc) if proc
- ## TEMP LIMITED HACK
- if result.is_a?(Array)
- result.map do |obj|
- obj_d, obj_l = obj.doubleValue, obj.longValue
- obj_d == obj_l ? obj_l : obj_d
- end
+ end
+
+ begin
+ result = Hash.dictionaryWithJSONString(source)
+ rescue => e
+ raise ParserError, e
else
- result
- end
+ recurse_proc(result, &proc) if proc
+ ## TEMP LIMITED HACK
+ if result.is_a?(Array)
+ result.map do |obj|
+ obj_d, obj_l = obj.doubleValue, obj.longValue
+ obj_d == obj_l ? obj_l : obj_d
+ end
+ else
+ result
+ end
##
+ end
end
@@ -100,48 +98,15 @@ def recurse_proc(result, &proc)
# :api: public
#
def self.dump(obj, anIO = nil, limit = nil)
- errorp = Pointer.new_with_type('@')
- json = sbjson.stringWithFragment(obj, error:errorp)
- raise errorp[0].description if errorp[0]
- json
+ begin
+ obj.to_json
+ rescue => e
+ raise GeneratorError, e
+ end
end
- # Unparse the Ruby data structure _obj_ into a single line JSON string and
- # return it. _state_ is
- # * a JSON::State object,
- # * or a Hash like object (responding to to_hash),
- # * an object convertible into a hash by a to_h method,
- # that is used as or to configure a State object.
- #
- # It defaults to a state object, that creates the shortest possible JSON text
- # in one line, checks for circular data structures and doesn't allow NaN,
- # Infinity, and -Infinity.
- #
- # A _state_ hash can have the following keys:
- # * *indent*: a string used to indent levels (default: ''),
- # * *space*: a string that is put after, a : or , delimiter (default: ''),
- # * *space_before*: a string that is put before a : pair delimiter (default: ''),
- # * *object_nl*: a string that is put at the end of a JSON object (default: ''),
- # * *array_nl*: a string that is put at the end of a JSON array (default: ''),
- # * *check_circular*: true if checking for circular data structures
- # should be done (the default), false otherwise.
- # * *allow_nan*: true if NaN, Infinity, and -Infinity should be
- # generated, otherwise an exception is thrown, if these values are
- # encountered. This options defaults to false.
- # * *max_nesting*: The maximum depth of nesting allowed in the data
- # structures from which JSON is to be generated. Disable depth checking
- # with :max_nesting => false, it defaults to 19.
- #
- # See also the fast_generate for the fastest creation method with the least
- # amount of sanity checks, and the pretty_generate method for some
- # defaults for a pretty output.
def self.generate(obj, state = nil)
- if state
- p "State not implemented yet"
- # state = State.from_state(state)
- else
- # state = State.new
- end
+ puts "State not implemented yet" if state
obj.to_json(state)
end
@@ -159,6 +124,7 @@ def self.generate(obj, state = nil)
# additions even if a matchin class and create_id was found. This option
# defaults to true.
def self.parse(source, opts = {})
+ p "Options not supported yet" unless opts.empty?
JSON.load(source)
end
View
Binary file not shown.
Oops, something went wrong.

2 comments on commit 2bd0b07

@alloy

This comment has been minimized.

Show comment Hide comment
@alloy

alloy Jan 24, 2009

Hi, could you please add your personal xcode files (mattetti.*) to your .gitignore file? It really clobbers the changeset view and is unnecessary anyways :) Thanks

Hi, could you please add your personal xcode files (mattetti.*) to your .gitignore file? It really clobbers the changeset view and is unnecessary anyways :) Thanks

@mattetti

This comment has been minimized.

Show comment Hide comment
@mattetti

mattetti Jan 25, 2009

Owner

sure thing, thanks for the tip

Owner

mattetti replied Jan 25, 2009

sure thing, thanks for the tip

Please sign in to comment.