зеркало из https://github.com/github/ruby.git
* 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:
Родитель
6d2dee14f3
Коммит
b60d64b001
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>
|
||||
|
||||
* test_fiber.rb: add a test (Continuation and Fiber).
|
||||
|
|
|
@ -6,4 +6,5 @@ if CONFIG['CC'] =~ /gcc/
|
|||
#CONFIG['CC'] += ' -Wall'
|
||||
end
|
||||
|
||||
have_header 'st.h'
|
||||
create_makefile 'json/ext/generator'
|
||||
|
|
|
@ -6,4 +6,5 @@ if CONFIG['CC'] =~ /gcc/
|
|||
CONFIG['CC'] += ' -Wall'
|
||||
end
|
||||
|
||||
have_header 'st.h'
|
||||
create_makefile 'json/ext/parser'
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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})
|
||||
|
|
|
@ -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
|
||||
|
|
Загрузка…
Ссылка в новой задаче