mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* 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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12456 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
6d2dee14f3
commit
b60d64b001
7 changed files with 51 additions and 22 deletions
11
ChangeLog
11
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>
|
Thu Jun 7 03:29:18 2007 Koichi Sasada <ko1@atdot.net>
|
||||||
|
|
||||||
* test_fiber.rb: add a test (Continuation and Fiber).
|
* test_fiber.rb: add a test (Continuation and Fiber).
|
||||||
|
|
|
@ -6,4 +6,5 @@ if CONFIG['CC'] =~ /gcc/
|
||||||
#CONFIG['CC'] += ' -Wall'
|
#CONFIG['CC'] += ' -Wall'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
have_header 'st.h'
|
||||||
create_makefile 'json/ext/generator'
|
create_makefile 'json/ext/generator'
|
||||||
|
|
|
@ -6,4 +6,5 @@ if CONFIG['CC'] =~ /gcc/
|
||||||
CONFIG['CC'] += ' -Wall'
|
CONFIG['CC'] += ' -Wall'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
have_header 'st.h'
|
||||||
create_makefile 'json/ext/parser'
|
create_makefile 'json/ext/parser'
|
||||||
|
|
|
@ -101,11 +101,27 @@ module JSON
|
||||||
# _opts_ can have the following
|
# _opts_ can have the following
|
||||||
# keys:
|
# keys:
|
||||||
# * *max_nesting*: The maximum depth of nesting allowed in the parsed data
|
# * *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 = {})
|
def parse(source, opts = {})
|
||||||
JSON.parser.new(source, opts).parse
|
JSON.parser.new(source, opts).parse
|
||||||
end
|
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
|
# 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
|
# return it. _state_ is a JSON::State object, that can be used to configure
|
||||||
# the output further.
|
# the output further.
|
||||||
|
|
|
@ -52,14 +52,12 @@ module JSON
|
||||||
MessageDialog::ERROR,
|
MessageDialog::ERROR,
|
||||||
MessageDialog::BUTTONS_CLOSE, text)
|
MessageDialog::BUTTONS_CLOSE, text)
|
||||||
dialog.show_all
|
dialog.show_all
|
||||||
window.focus = dialog
|
|
||||||
dialog.run
|
dialog.run
|
||||||
rescue TypeError
|
rescue TypeError
|
||||||
dialog = MessageDialog.new(Editor.window, Dialog::MODAL,
|
dialog = MessageDialog.new(Editor.window, Dialog::MODAL,
|
||||||
MessageDialog::ERROR,
|
MessageDialog::ERROR,
|
||||||
MessageDialog::BUTTONS_CLOSE, text)
|
MessageDialog::BUTTONS_CLOSE, text)
|
||||||
dialog.show_all
|
dialog.show_all
|
||||||
window.focus = dialog
|
|
||||||
dialog.run
|
dialog.run
|
||||||
ensure
|
ensure
|
||||||
dialog.destroy if dialog
|
dialog.destroy if dialog
|
||||||
|
@ -73,7 +71,6 @@ module JSON
|
||||||
MessageDialog::QUESTION,
|
MessageDialog::QUESTION,
|
||||||
MessageDialog::BUTTONS_YES_NO, text)
|
MessageDialog::BUTTONS_YES_NO, text)
|
||||||
dialog.show_all
|
dialog.show_all
|
||||||
window.focus = dialog
|
|
||||||
dialog.run do |response|
|
dialog.run do |response|
|
||||||
return Gtk::Dialog::RESPONSE_YES === response
|
return Gtk::Dialog::RESPONSE_YES === response
|
||||||
end
|
end
|
||||||
|
@ -1102,9 +1099,11 @@ module JSON
|
||||||
# Quit this editor, that is, leave this editor's main loop.
|
# Quit this editor, that is, leave this editor's main loop.
|
||||||
def quit
|
def quit
|
||||||
ask_save if @changed
|
ask_save if @changed
|
||||||
destroy
|
if Gtk.main_level > 0
|
||||||
Gtk.main_quit
|
destroy
|
||||||
true
|
Gtk.main_quit
|
||||||
|
end
|
||||||
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
# Display the new title according to the editor's current state.
|
# Display the new title according to the editor's current state.
|
||||||
|
@ -1185,20 +1184,21 @@ module JSON
|
||||||
if filename
|
if filename
|
||||||
if File.directory?(filename)
|
if File.directory?(filename)
|
||||||
Editor.error_dialog(self, "Try to select a JSON file!")
|
Editor.error_dialog(self, "Try to select a JSON file!")
|
||||||
return
|
nil
|
||||||
else
|
else
|
||||||
data = read_data(filename)
|
|
||||||
@filename = 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
|
display_title
|
||||||
return data
|
data
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Load the data at location _uri_ into the editor as a JSON document.
|
# Load the data at location _uri_ into the editor as a JSON document.
|
||||||
def load_location(uri)
|
def load_location(uri)
|
||||||
data = read_data(uri)
|
data = read_data(uri) or return
|
||||||
@filename = nil
|
@filename = nil
|
||||||
toplevel.display_status("Loaded data from '#{uri}'.")
|
toplevel.display_status("Loaded data from '#{uri}'.")
|
||||||
display_title
|
display_title
|
||||||
|
@ -1217,11 +1217,9 @@ module JSON
|
||||||
end
|
end
|
||||||
return JSON::parse(json, :max_nesting => false)
|
return JSON::parse(json, :max_nesting => false)
|
||||||
end
|
end
|
||||||
rescue JSON::JSONError => e
|
rescue => e
|
||||||
Editor.error_dialog(self, "Failed to parse JSON file: #{e}!")
|
Editor.error_dialog(self, "Failed to parse JSON file: #{e}!")
|
||||||
return
|
return
|
||||||
rescue SystemCallError => e
|
|
||||||
quit
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Open a file selecton dialog, displaying _message_, and return the
|
# Open a file selecton dialog, displaying _message_, and return the
|
||||||
|
|
|
@ -40,7 +40,7 @@ EOT
|
||||||
|
|
||||||
def test_unparse
|
def test_unparse
|
||||||
json = unparse(@hash)
|
json = unparse(@hash)
|
||||||
assert_equal(@json2, json)
|
assert_equal(JSON.parse(@json2), JSON.parse(json))
|
||||||
parsed_json = parse(json)
|
parsed_json = parse(json)
|
||||||
assert_equal(@hash, parsed_json)
|
assert_equal(@hash, parsed_json)
|
||||||
json = generate({1=>2})
|
json = generate({1=>2})
|
||||||
|
@ -51,7 +51,7 @@ EOT
|
||||||
|
|
||||||
def test_unparse_pretty
|
def test_unparse_pretty
|
||||||
json = pretty_unparse(@hash)
|
json = pretty_unparse(@hash)
|
||||||
assert_equal(@json3, json)
|
assert_equal(JSON.parse(@json3), JSON.parse(json))
|
||||||
parsed_json = parse(json)
|
parsed_json = parse(json)
|
||||||
assert_equal(@hash, parsed_json)
|
assert_equal(@hash, parsed_json)
|
||||||
json = pretty_generate({1=>2})
|
json = pretty_generate({1=>2})
|
||||||
|
|
|
@ -39,15 +39,17 @@ class TC_JSONUnicode < Test::Unit::TestCase
|
||||||
|
|
||||||
def test_chars
|
def test_chars
|
||||||
(0..0x7f).each do |i|
|
(0..0x7f).each do |i|
|
||||||
c = ('%c' % i)[0] # c is a character object
|
|
||||||
json = '["\u%04x"]' % i
|
json = '["\u%04x"]' % i
|
||||||
assert_equal c, JSON.parse(json).first
|
if RUBY_VERSION >= "1.9."
|
||||||
if c == ?\b
|
i = i.chr
|
||||||
|
end
|
||||||
|
assert_equal i, JSON.parse(json).first[0]
|
||||||
|
if i == ?\b
|
||||||
generated = JSON.generate(["" << i])
|
generated = JSON.generate(["" << i])
|
||||||
assert '["\b"]' == generated || '["\10"]' == generated
|
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])
|
assert_equal '[' << ('' << i).dump << ']', JSON.generate(["" << i])
|
||||||
elsif i < 0x20
|
elsif i.chr < 0x20.chr
|
||||||
assert_equal json, JSON.generate(["" << i])
|
assert_equal json, JSON.generate(["" << i])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue