summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog11
-rw-r--r--ext/json/ext/generator/extconf.rb1
-rw-r--r--ext/json/ext/parser/extconf.rb1
-rw-r--r--lib/json/common.rb18
-rw-r--r--lib/json/editor.rb26
-rw-r--r--test/json/test_json_generate.rb4
-rwxr-xr-xtest/json/test_json_unicode.rb12
7 files changed, 51 insertions, 22 deletions
diff --git a/ChangeLog b/ChangeLog
index 1abeb5e..c75d85f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+Thu Jun 07 07:24:36 2007 NARUSE, Yui <naruse@ruby-lang.org>
+
+ * lib/json/common.rb: Ponder offering parse! method.
+
+ * lib/json/editor.rb: be a bit more robust while loading data.
+
+ * ext/json/ext/{generator,parser}/extconf.rb:
+ add a have_header directive for st.h
+
+ * test/json: fix some tests.
+
Thu Jun 7 03:29:18 2007 Koichi Sasada <ko1@atdot.net>
* test_fiber.rb: add a test (Continuation and Fiber).
diff --git a/ext/json/ext/generator/extconf.rb b/ext/json/ext/generator/extconf.rb
index db721a9..d1d0401 100644
--- a/ext/json/ext/generator/extconf.rb
+++ b/ext/json/ext/generator/extconf.rb
@@ -6,4 +6,5 @@ if CONFIG['CC'] =~ /gcc/
#CONFIG['CC'] += ' -Wall'
end
+have_header 'st.h'
create_makefile 'json/ext/generator'
diff --git a/ext/json/ext/parser/extconf.rb b/ext/json/ext/parser/extconf.rb
index 085c8d0..cef706a 100644
--- a/ext/json/ext/parser/extconf.rb
+++ b/ext/json/ext/parser/extconf.rb
@@ -6,4 +6,5 @@ if CONFIG['CC'] =~ /gcc/
CONFIG['CC'] += ' -Wall'
end
+have_header 'st.h'
create_makefile 'json/ext/parser'
diff --git a/lib/json/common.rb b/lib/json/common.rb
index c820cfb..0967aed 100644
--- a/lib/json/common.rb
+++ b/lib/json/common.rb
@@ -101,11 +101,27 @@ module JSON
# _opts_ can have the following
# keys:
# * *max_nesting*: The maximum depth of nesting allowed in the parsed data
- # structures. Disable depth checking with :max_nesting => false.
+ # structures. Disable depth checking with :max_nesting => false. This value
+ # defaults to 19.
def parse(source, opts = {})
JSON.parser.new(source, opts).parse
end
+ # Parse the JSON string _source_ into a Ruby data structure and return it.
+ #
+ # _opts_ can have the following
+ # keys:
+ # * *max_nesting*: The maximum depth of nesting allowed in the parsed data
+ # structures. Enable depth checking with :max_nesting => anInteger. The parse!
+ # methods defaults to not doing max depth checking: This can be dangerous,
+ # if someone wants to fill up your stack.
+ def parse!(source, opts = {})
+ opts = {
+ :max_nesting => false
+ }.update(opts)
+ JSON.parser.new(source, opts).parse
+ end
+
# Unparse the Ruby data structure _obj_ into a single line JSON string and
# return it. _state_ is a JSON::State object, that can be used to configure
# the output further.
diff --git a/lib/json/editor.rb b/lib/json/editor.rb
index 063df69..3dbcce5 100644
--- a/lib/json/editor.rb
+++ b/lib/json/editor.rb
@@ -52,14 +52,12 @@ module JSON
MessageDialog::ERROR,
MessageDialog::BUTTONS_CLOSE, text)
dialog.show_all
- window.focus = dialog
dialog.run
rescue TypeError
dialog = MessageDialog.new(Editor.window, Dialog::MODAL,
MessageDialog::ERROR,
MessageDialog::BUTTONS_CLOSE, text)
dialog.show_all
- window.focus = dialog
dialog.run
ensure
dialog.destroy if dialog
@@ -73,7 +71,6 @@ module JSON
MessageDialog::QUESTION,
MessageDialog::BUTTONS_YES_NO, text)
dialog.show_all
- window.focus = dialog
dialog.run do |response|
return Gtk::Dialog::RESPONSE_YES === response
end
@@ -1102,9 +1099,11 @@ module JSON
# Quit this editor, that is, leave this editor's main loop.
def quit
ask_save if @changed
- destroy
- Gtk.main_quit
- true
+ if Gtk.main_level > 0
+ destroy
+ Gtk.main_quit
+ end
+ nil
end
# Display the new title according to the editor's current state.
@@ -1185,20 +1184,21 @@ module JSON
if filename
if File.directory?(filename)
Editor.error_dialog(self, "Try to select a JSON file!")
- return
+ nil
else
- data = read_data(filename)
@filename = filename
- toplevel.display_status("Loaded data from '#@filename'.")
+ if data = read_data(filename)
+ toplevel.display_status("Loaded data from '#@filename'.")
+ end
display_title
- return data
+ data
end
end
end
# Load the data at location _uri_ into the editor as a JSON document.
def load_location(uri)
- data = read_data(uri)
+ data = read_data(uri) or return
@filename = nil
toplevel.display_status("Loaded data from '#{uri}'.")
display_title
@@ -1217,11 +1217,9 @@ module JSON
end
return JSON::parse(json, :max_nesting => false)
end
- rescue JSON::JSONError => e
+ rescue => e
Editor.error_dialog(self, "Failed to parse JSON file: #{e}!")
return
- rescue SystemCallError => e
- quit
end
# Open a file selecton dialog, displaying _message_, and return the
diff --git a/test/json/test_json_generate.rb b/test/json/test_json_generate.rb
index 519d56a..82d8c3d 100644
--- a/test/json/test_json_generate.rb
+++ b/test/json/test_json_generate.rb
@@ -40,7 +40,7 @@ EOT
def test_unparse
json = unparse(@hash)
- assert_equal(@json2, json)
+ assert_equal(JSON.parse(@json2), JSON.parse(json))
parsed_json = parse(json)
assert_equal(@hash, parsed_json)
json = generate({1=>2})
@@ -51,7 +51,7 @@ EOT
def test_unparse_pretty
json = pretty_unparse(@hash)
- assert_equal(@json3, json)
+ assert_equal(JSON.parse(@json3), JSON.parse(json))
parsed_json = parse(json)
assert_equal(@hash, parsed_json)
json = pretty_generate({1=>2})
diff --git a/test/json/test_json_unicode.rb b/test/json/test_json_unicode.rb
index a23e50e..a91f4b5 100755
--- a/test/json/test_json_unicode.rb
+++ b/test/json/test_json_unicode.rb
@@ -39,15 +39,17 @@ class TC_JSONUnicode < Test::Unit::TestCase
def test_chars
(0..0x7f).each do |i|
- c = ('%c' % i)[0] # c is a character object
json = '["\u%04x"]' % i
- assert_equal c, JSON.parse(json).first
- if c == ?\b
+ if RUBY_VERSION >= "1.9."
+ i = i.chr
+ end
+ assert_equal i, JSON.parse(json).first[0]
+ if i == ?\b
generated = JSON.generate(["" << i])
assert '["\b"]' == generated || '["\10"]' == generated
- elsif [?\n, ?\r, ?\t, ?\f].include?(c)
+ elsif [?\n, ?\r, ?\t, ?\f].include?(i)
assert_equal '[' << ('' << i).dump << ']', JSON.generate(["" << i])
- elsif i < 0x20
+ elsif i.chr < 0x20.chr
assert_equal json, JSON.generate(["" << i])
end
end