diff options
author | Tim Felgentreff <timfelgentreff@gmail.com> | 2011-08-09 14:58:23 -0700 |
---|---|---|
committer | Tim Felgentreff <timfelgentreff@gmail.com> | 2011-08-09 14:58:23 -0700 |
commit | cc178803a1e38c7f03b0d23984ba50042d40dcb5 (patch) | |
tree | 438d1e8b6f8b273299872c1b6b051383052cc923 | |
parent | 7d9458ed616719e105bf347458195de1b87f2bb5 (diff) | |
parent | f7f78896607b6f6226cdee4ae76de922d4583d32 (diff) | |
download | json-cc178803a1e38c7f03b0d23984ba50042d40dcb5.tar.gz |
Merge remote-tracking branch 'json/master'
Conflicts:
ext/json/ext/parser/parser.c
ext/json/ext/parser/parser.rl
json.gemspec
json_pure.gemspec
37 files changed, 909 insertions, 624 deletions
@@ -4,3 +4,4 @@ pkg .nfs.* .idea java/Json.iml +Gemfile.lock diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..2ebf7fc --- /dev/null +++ b/.travis.yml @@ -0,0 +1,15 @@ +# Passes arguments to bundle install (http://gembundler.com/man/bundle-install.1.html) +bundler_args: --binstubs + +# Specify which ruby versions you wish to run your tests on, each version will be used +rvm: + - 1.8.7 + - 1.9.2 + - rbx + - rbx-2.0 + - ree + - jruby + - ruby-head + - 1.8.6 + +script: "bundle exec rake" @@ -1,3 +1,15 @@ +2011-07-04 (1.5.4) + * Fix memory leak when used from multiple JRuby. (Patch by + jfirebaugh@github). +2011-06-20 (1.5.3) + * Alias State#configure method as State#merge to increase duck type synonymy with Hash. + * Add as_json methods in json/add/core, so rails can create its json objects + the new way. +2011-05-11 (1.5.2) + * Apply documentation patch by Cory Monty <cory.monty@gmail.com>. + * Add gemspecs for json and json_pure. + * Fix bug in jruby pretty printing. + * Fix bug in object_class and array_class when inheriting from Hash or Array. 2011-01-24 (1.5.1) * Made rake-compiler build a fat binary gem. This should fix issue https://github.com/flori/json/issues#issue/54. @@ -0,0 +1,7 @@ +# vim: set ft=ruby: + +source :rubygems + +gemspec :name => 'json' +gemspec :name => 'json_pure' +gemspec :name => 'json-java' @@ -1,3 +1,5 @@ += JSON implementation for Ruby http://travis-ci.org/flori/json.png + == Description This is a implementation of the JSON specification according to RFC 4627 @@ -1,22 +1,16 @@ begin - require 'rake/gempackagetask' + require 'rubygems/package_task' rescue LoadError end -begin - require 'rake/extensiontask' -rescue LoadError - warn "WARNING: rake-compiler is not installed. You will not be able to build the json gem until you install it." -end - require 'rbconfig' include Config require 'rake/clean' -CLOBBER.include Dir['benchmarks/data/*.{dat,log}'] +CLOBBER.include Dir['benchmarks/data/*.{dat,log}'], 'doc', 'Gemfile.lock' CLEAN.include FileList['diagrams/*.*'], 'doc', 'coverage', 'tmp', FileList["ext/**/{Makefile,mkmf.log}"], 'build', 'dist', FileList['**/*.rbc'], - FileList["{ext,lib}/**/*.{so,bundle,#{CONFIG['DLEXT']},o,obj,pdb,lib,manifest,exp,def,jar,class}"], + FileList["{ext,lib}/**/*.{so,bundle,#{CONFIG['DLEXT']},o,obj,pdb,lib,manifest,exp,def,jar,class,dSYM}"], FileList['java/src/**/*.class'] MAKE = ENV['MAKE'] || %w[gmake make].find { |c| system(c, '-v') } @@ -69,11 +63,12 @@ task :install_ext_really do mkdir_p File.dirname(d) install(file, d) end + warn " *** Installed EXT ruby library." end end desc "Installing library (extension)" -task :install_ext => [ :compile_ext, :install_pure, :install_ext_really ] +task :install_ext => [ :compile, :install_pure, :install_ext_really ] desc "Installing library (extension)" if RUBY_PLATFORM =~ /java/ @@ -82,7 +77,7 @@ else task :install => :install_ext end -if defined?(Gem) and defined?(Rake::GemPackageTask) +if defined?(Gem) and defined?(Gem::PackageTask) spec_pure = Gem::Specification.new do |s| s.name = 'json_pure' s.version = PKG_VERSION @@ -92,15 +87,18 @@ if defined?(Gem) and defined?(Rake::GemPackageTask) s.files = PKG_FILES s.require_path = 'lib' + s.add_development_dependency 'permutation' + s.add_development_dependency 'bullshit' + s.add_development_dependency 'sdoc' + s.add_development_dependency 'rake', '~>0.9.2' + s.add_dependency 'spruz', '~>0.2.8' s.bindir = "bin" s.executables = [ "edit_json.rb", "prettify_json.rb" ] - s.default_executable = "edit_json.rb" - s.has_rdoc = true - s.extra_rdoc_files << 'README' + s.extra_rdoc_files << 'README.rdoc' s.rdoc_options << - '--title' << 'JSON implemention for ruby' << '--main' << 'README' + '--title' << 'JSON implemention for ruby' << '--main' << 'README.rdoc' s.test_files.concat Dir['./tests/test_*.rb'] s.author = "Florian Frank" @@ -109,13 +107,18 @@ if defined?(Gem) and defined?(Rake::GemPackageTask) s.rubyforge_project = "json" end - Rake::GemPackageTask.new(spec_pure) do |pkg| + desc 'Creates a json_pure.gemspec file' + task :gemspec_pure => :version do + File.open('json_pure.gemspec', 'w') do |gemspec| + gemspec.write spec_pure.to_ruby + end + end + + Gem::PackageTask.new(spec_pure) do |pkg| pkg.need_tar = true pkg.package_files = PKG_FILES end -end -if defined?(Gem) and defined?(Rake::GemPackageTask) and defined?(Rake::ExtensionTask) spec_ext = Gem::Specification.new do |s| s.name = 'json' s.version = PKG_VERSION @@ -129,15 +132,16 @@ if defined?(Gem) and defined?(Rake::GemPackageTask) and defined?(Rake::Extension s.require_path = EXT_ROOT_DIR s.require_paths << 'ext' s.require_paths << 'lib' + s.add_development_dependency 'permutation' + s.add_development_dependency 'bullshit' + s.add_development_dependency 'sdoc' s.bindir = "bin" s.executables = [ "edit_json.rb", "prettify_json.rb" ] - s.default_executable = "edit_json.rb" - s.has_rdoc = true - s.extra_rdoc_files << 'README' + s.extra_rdoc_files << 'README.rdoc' s.rdoc_options << - '--title' << 'JSON implemention for Ruby' << '--main' << 'README' + '--title' << 'JSON implemention for Ruby' << '--main' << 'README.rdoc' s.test_files.concat Dir['./tests/test_*.rb'] s.author = "Florian Frank" @@ -146,28 +150,21 @@ if defined?(Gem) and defined?(Rake::GemPackageTask) and defined?(Rake::Extension s.rubyforge_project = "json" end - Rake::GemPackageTask.new(spec_ext) do |pkg| + desc 'Creates a json.gemspec file' + task :gemspec_ext => :version do + File.open('json.gemspec', 'w') do |gemspec| + gemspec.write spec_ext.to_ruby + end + end + + Gem::PackageTask.new(spec_ext) do |pkg| pkg.need_tar = true pkg.package_files = PKG_FILES end - Rake::ExtensionTask.new do |ext| - ext.name = 'parser' - ext.gem_spec = spec_ext - ext.cross_compile = true - ext.cross_platform = %w[i386-mswin32 i386-mingw32] - ext.ext_dir = 'ext/json/ext/parser' - ext.lib_dir = 'lib/json/ext' - end - Rake::ExtensionTask.new do |ext| - ext.name = 'generator' - ext.gem_spec = spec_ext - ext.cross_compile = true - ext.cross_platform = %w[i386-mswin32 i386-mingw32] - ext.ext_dir = 'ext/json/ext/generator' - ext.lib_dir = 'lib/json/ext' - end + desc 'Create all gemspec files' + task :gemspec => [ :gemspec_pure, :gemspec_ext ] end desc m = "Writing version information for #{PKG_VERSION}" @@ -197,8 +194,22 @@ end desc "Testing library (pure ruby and extension)" task :test => [ :test_pure, :test_ext ] +namespace :gems do + task :install do + sh 'bundle' + end +end if defined?(RUBY_ENGINE) and RUBY_ENGINE == 'jruby' + if ENV.key?('JAVA_HOME') + warn " *** JAVA_HOME was set to #{ENV['JAVA_HOME'].inspect}" + else File.directory?(local_java = '/usr/local/java/jdk') + ENV['JAVA_HOME'] = local_java + warn " *** JAVA_HOME is set to #{ENV['JAVA_HOME'].inspect}" + ENV['PATH'] = ENV['PATH'].split(/:/).unshift(java_path = "#{ENV['JAVA_HOME']}/bin") * ':' + warn " *** java binaries are assumed to be in #{java_path.inspect}" + end + file JAVA_PARSER_SRC => JAVA_RAGEL_PATH do cd JAVA_DIR do if RAGEL_CODEGEN == 'ragel' @@ -232,7 +243,7 @@ if defined?(RUBY_ENGINE) and RUBY_ENGINE == 'jruby' end desc "Compiling jruby extension" - task :compile_ext => JAVA_CLASSES + task :compile => JAVA_CLASSES desc "Package the jruby gem" task :jruby_gem => :create_jar do @@ -247,7 +258,7 @@ if defined?(RUBY_ENGINE) and RUBY_ENGINE == 'jruby' myruby '-S', 'testrb', '-Ilib', *Dir['./tests/test_*.rb'] end - file JRUBY_PARSER_JAR => :compile_ext do + file JRUBY_PARSER_JAR => :compile do cd 'java/src' do parser_classes = FileList[ "json/ext/ByteListTranscoder*.class", @@ -265,7 +276,7 @@ if defined?(RUBY_ENGINE) and RUBY_ENGINE == 'jruby' desc "Create parser jar" task :create_parser_jar => JRUBY_PARSER_JAR - file JRUBY_GENERATOR_JAR => :compile_ext do + file JRUBY_GENERATOR_JAR => :compile do cd 'java/src' do generator_classes = FileList[ "json/ext/ByteListTranscoder*.class", @@ -290,7 +301,7 @@ if defined?(RUBY_ENGINE) and RUBY_ENGINE == 'jruby' task :release => [ :clean, :version, :jruby_gem ] else desc "Compiling extension" - task :compile_ext => [ EXT_PARSER_DL, EXT_GENERATOR_DL ] + task :compile => [ EXT_PARSER_DL, EXT_GENERATOR_DL ] file EXT_PARSER_DL => EXT_PARSER_SRC do cd EXT_PARSER_DIR do @@ -309,7 +320,7 @@ else end desc "Testing library (extension)" - task :test_ext => :compile_ext do + task :test_ext => :compile do ENV['JSON'] = 'ext' ENV['RUBYOPT'] = "-Iext:lib #{ENV['RUBYOPT']}" myruby '-S', 'testrb', *Dir['./tests/test_*.rb'] @@ -334,7 +345,7 @@ else desc "Create RDOC documentation" task :doc => [ :version, EXT_PARSER_SRC ] do - sh "sdoc -o doc -t '#{PKG_TITLE}' -m README README lib/json.rb #{FileList['lib/json/**/*.rb']} #{EXT_PARSER_SRC} #{EXT_GENERATOR_SRC}" + sh "sdoc -o doc -t '#{PKG_TITLE}' -m README.rdoc README.rdoc lib/json.rb #{FileList['lib/json/**/*.rb']} #{EXT_PARSER_SRC} #{EXT_GENERATOR_SRC}" end desc "Generate parser with ragel" @@ -352,6 +363,8 @@ else else sh "ragel -x parser.rl | #{RAGEL_CODEGEN} -G2" end + src = File.read("parser.c").gsub(/[ \t]+$/, '') + File.open("parser.c", "w") {|f| f.print src} end end @@ -386,16 +399,9 @@ else desc "Generate diagrams of ragel parser" task :ragel_dot => [ :ragel_dot_png, :ragel_dot_ps ] - task :environment do - ENV['RUBY_CC_VERSION'] = '1.8.7:1.9.2' - end - desc "Build all gems and archives for a new release of json and json_pure." - task :release => [ :clean, :version, :environment, :cross, :native, :gem, ] do - sh "#$0 clean native gem" - sh "#$0 clean package" - end + task :release => [ :clean, :gemspec, :package ] end desc "Compile in the the source directory" -task :default => [ :version ] +task :default => [ :clean, :gemspec, :test ] @@ -1 +1 @@ -1.5.1 +1.5.4 diff --git a/bin/prettify_json.rb b/bin/prettify_json.rb index 5e1f806..3c53183 100755 --- a/bin/prettify_json.rb +++ b/bin/prettify_json.rb @@ -3,37 +3,8 @@ require 'json' require 'fileutils' include FileUtils - -# Parses the argument array _args_, according to the pattern _s_, to -# retrieve the single character command line options from it. If _s_ is -# 'xy:' an option '-x' without an option argument is searched, and an -# option '-y foo' with an option argument ('foo'). -# -# An option hash is returned with all found options set to true or the -# found option argument. -def go(s, args = ARGV) - b, v = s.scan(/(.)(:?)/).inject([{},{}]) { |t,(o,a)| - t[a.empty? ? 0 : 1][o] = a.empty? ? false : nil - t - } - while a = args.shift - a !~ /\A-(.+)/ and args.unshift a and break - p = $1 - until p == '' - o = p.slice!(0, 1) - if v.key?(o) - v[o] = if p == '' then args.shift or break 1 else p end - break - elsif b.key?(o) - b[o] = true - else - args.unshift a - break 1 - end - end and break - end - b.merge(v) -end +require 'spruz/go' +include Spruz::GO opts = go 'slhi:', args = ARGV.dup if opts['h'] || opts['l'] && opts['s'] @@ -52,19 +23,21 @@ EOT exit 0 end -filename = nil -json = JSON[ - if args.empty? - STDIN.read +json_opts = { :max_nesting => false, :create_additions => false } + +document = + if filename = args.first or filename == '-' + File.read(filename) else - File.read filename = args.first + STDIN.read end -] + +json = JSON.parse document, json_opts output = if opts['s'] - JSON.fast_generate json + JSON.fast_generate json, json_opts else # default is -l - JSON.pretty_generate json + JSON.pretty_generate json, json_opts end if opts['i'] && filename diff --git a/ext/json/ext/generator/generator.c b/ext/json/ext/generator/generator.c index b476f3d..8ea0a63 100644 --- a/ext/json/ext/generator/generator.c +++ b/ext/json/ext/generator/generator.c @@ -18,9 +18,9 @@ static ID i_to_s, i_to_json, i_new, i_indent, i_space, i_space_before, /* * Copyright 2001-2004 Unicode, Inc. - * + * * Disclaimer - * + * * This source code is provided as is by Unicode, Inc. No claims are * made as to fitness for any particular purpose. No warranties of any * kind are expressed or implied. The recipient agrees to determine @@ -28,9 +28,9 @@ static ID i_to_s, i_to_json, i_new, i_indent, i_space, i_space_before, * purchased on magnetic or optical media from Unicode, Inc., the * sole remedy for any claim will be exchange of defective media * within 90 days of receipt. - * + * * Limitations on Rights to Redistribute This Code - * + * * Unicode, Inc. hereby grants the right to freely use the information * supplied in this file in the creation of products supporting the * Unicode Standard, and to make copies of this file in any form @@ -61,8 +61,8 @@ static const char trailingBytesForUTF8[256] = { * This table contains as many values as there might be trailing bytes * in a UTF-8 sequence. */ -static const UTF32 offsetsFromUTF8[6] = { 0x00000000UL, 0x00003080UL, 0x000E2080UL, - 0x03C82080UL, 0xFA082080UL, 0x82082080UL }; +static const UTF32 offsetsFromUTF8[6] = { 0x00000000UL, 0x00003080UL, 0x000E2080UL, + 0x03C82080UL, 0xFA082080UL, 0x82082080UL }; /* * Utility routine to tell whether a sequence of bytes is legal UTF-8. @@ -318,11 +318,6 @@ static void fbuffer_free(FBuffer *fb) ruby_xfree(fb); } -static void fbuffer_free_only_buffer(FBuffer *fb) -{ - ruby_xfree(fb); -} - static void fbuffer_clear(FBuffer *fb) { fb->len = 0; @@ -363,23 +358,23 @@ static void fbuffer_append_char(FBuffer *fb, char newchr) static void freverse(char *start, char *end) { - char c; + char c; - while (end > start) { - c = *end, *end-- = *start, *start++ = c; + while (end > start) { + c = *end, *end-- = *start, *start++ = c; } } static long fltoa(long number, char *buf) { - static char digits[] = "0123456789"; - long sign = number; - char* tmp = buf; + static char digits[] = "0123456789"; + long sign = number; + char* tmp = buf; - if (sign < 0) number = -number; + if (sign < 0) number = -number; do *tmp++ = digits[number % 10]; while (number /= 10); - if (sign < 0) *tmp++ = '-'; - freverse(buf, tmp - 1); + if (sign < 0) *tmp++ = '-'; + freverse(buf, tmp - 1); return tmp - buf; } @@ -404,7 +399,7 @@ static FBuffer *fbuffer_dup(FBuffer *fb) return result; } -/* +/* * Document-module: JSON::Ext::Generator * * This is the JSON generator implemented as a C extension. It can be @@ -561,6 +556,7 @@ static VALUE mFalseClass_to_json(int argc, VALUE *argv, VALUE self) /* * call-seq: to_json(*) * + * Returns a JSON string for nil: 'null'. */ static VALUE mNilClass_to_json(int argc, VALUE *argv, VALUE self) { @@ -984,7 +980,7 @@ static VALUE cState_generate(VALUE self, VALUE obj) * * *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: ''), + * * *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: ''), * * *allow_nan*: true if NaN, Infinity, and -Infinity should be * generated, otherwise an exception is thrown, if these values are @@ -1359,6 +1355,7 @@ void Init_generator() rb_define_method(cState, "depth", cState_depth, 0); rb_define_method(cState, "depth=", cState_depth_set, 1); rb_define_method(cState, "configure", cState_configure, 1); + rb_define_alias(cState, "merge", "configure"); rb_define_method(cState, "to_h", cState_to_h, 0); rb_define_method(cState, "[]", cState_aref, 1); rb_define_method(cState, "generate", cState_generate, 1); diff --git a/ext/json/ext/generator/generator.h b/ext/json/ext/generator/generator.h index e47f507..ee496fe 100644 --- a/ext/json/ext/generator/generator.h +++ b/ext/json/ext/generator/generator.h @@ -67,7 +67,6 @@ static char *fstrndup(const char *ptr, unsigned long len); static FBuffer *fbuffer_alloc(); static FBuffer *fbuffer_alloc_with_length(unsigned long initial_length); static void fbuffer_free(FBuffer *fb); -static void fbuffer_free_only_buffer(FBuffer *fb); static void fbuffer_clear(FBuffer *fb); static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len); static void fbuffer_append_long(FBuffer *fb, long number); @@ -79,9 +78,9 @@ static VALUE fbuffer_to_s(FBuffer *fb); #define UNI_STRICT_CONVERSION 1 -typedef unsigned long UTF32; /* at least 32 bits */ -typedef unsigned short UTF16; /* at least 16 bits */ -typedef unsigned char UTF8; /* typically 8 bits */ +typedef unsigned long UTF32; /* at least 32 bits */ +typedef unsigned short UTF16; /* at least 16 bits */ +typedef unsigned char UTF8; /* typically 8 bits */ #define UNI_REPLACEMENT_CHAR (UTF32)0x0000FFFD #define UNI_MAX_BMP (UTF32)0x0000FFFF diff --git a/ext/json/ext/parser/parser.c b/ext/json/ext/parser/parser.c index c5eefad..b28a6fc 100644 --- a/ext/json/ext/parser/parser.c +++ b/ext/json/ext/parser/parser.c @@ -4,7 +4,7 @@ /* unicode */ -static const char digit_values[256] = { +static const char digit_values[256] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, @@ -40,7 +40,7 @@ static UTF32 unescape_unicode(const unsigned char *p) return result; } -static int convert_UTF32_to_UTF8(char *buf, UTF32 ch) +static int convert_UTF32_to_UTF8(char *buf, UTF32 ch) { int len = 1; if (ch <= 0x7F) { @@ -69,7 +69,7 @@ static int convert_UTF32_to_UTF8(char *buf, UTF32 ch) #ifdef HAVE_RUBY_ENCODING_H static VALUE CEncoding_ASCII_8BIT, CEncoding_UTF_8, CEncoding_UTF_16BE, CEncoding_UTF_16LE, CEncoding_UTF_32BE, CEncoding_UTF_32LE; -static ID i_encoding, i_encode, i_encode_bang, i_force_encoding; +static ID i_encoding, i_encode; #else static ID i_iconv; #endif @@ -79,7 +79,7 @@ static VALUE CNaN, CInfinity, CMinusInfinity; static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions, i_chr, i_max_nesting, i_allow_nan, i_symbolize_names, i_object_class, - i_array_class, i_key_p, i_deep_const_get, i_match, i_match_string; + i_array_class, i_key_p, i_deep_const_get, i_match, i_match_string, i_aset, i_leftshift; #line 108 "parser.rl" @@ -94,7 +94,7 @@ static const int JSON_object_error = 0; static const int JSON_object_en_main = 1; -#line 144 "parser.rl" +#line 148 "parser.rl" static const char *JSON_parse_object(JSON_Parser *json, const char *p, const char *pe, VALUE *result) @@ -109,14 +109,14 @@ static const char *JSON_parse_object(JSON_Parser *json, const char *p, const cha *result = NIL_P(object_class) ? rb_hash_new() : rb_class_new_instance(0, 0, object_class); - + #line 114 "parser.c" { cs = JSON_object_start; } -#line 159 "parser.rl" - +#line 163 "parser.rl" + #line 121 "parser.c" { if ( p == pe ) @@ -145,7 +145,7 @@ case 2: goto st2; goto st0; tr2: -#line 127 "parser.rl" +#line 131 "parser.rl" { const char *np; json->parsing_name = 1; @@ -228,11 +228,15 @@ tr11: #line 116 "parser.rl" { VALUE v = Qnil; - const char *np = JSON_parse_value(json, p, pe, &v); + const char *np = JSON_parse_value(json, p, pe, &v); if (np == NULL) { p--; {p++; cs = 9; goto _out;} } else { - rb_hash_aset(*result, last_name, v); + if (NIL_P(json->object_class)) { + rb_hash_aset(*result, last_name, v); + } else { + rb_funcall(*result, i_aset, 2, last_name, v); + } {p = (( np))-1;} } } @@ -241,7 +245,7 @@ st9: if ( ++p == pe ) goto _test_eof9; case 9: -#line 245 "parser.c" +#line 249 "parser.c" switch( (*p) ) { case 13: goto st9; case 32: goto st9; @@ -330,14 +334,14 @@ case 18: goto st9; goto st18; tr4: -#line 135 "parser.rl" +#line 139 "parser.rl" { p--; {p++; cs = 27; goto _out;} } goto st27; st27: if ( ++p == pe ) goto _test_eof27; case 27: -#line 341 "parser.c" +#line 345 "parser.c" goto st0; st19: if ( ++p == pe ) @@ -404,38 +408,38 @@ case 26: goto st2; goto st26; } - _test_eof2: cs = 2; goto _test_eof; - _test_eof3: cs = 3; goto _test_eof; - _test_eof4: cs = 4; goto _test_eof; - _test_eof5: cs = 5; goto _test_eof; - _test_eof6: cs = 6; goto _test_eof; - _test_eof7: cs = 7; goto _test_eof; - _test_eof8: cs = 8; goto _test_eof; - _test_eof9: cs = 9; goto _test_eof; - _test_eof10: cs = 10; goto _test_eof; - _test_eof11: cs = 11; goto _test_eof; - _test_eof12: cs = 12; goto _test_eof; - _test_eof13: cs = 13; goto _test_eof; - _test_eof14: cs = 14; goto _test_eof; - _test_eof15: cs = 15; goto _test_eof; - _test_eof16: cs = 16; goto _test_eof; - _test_eof17: cs = 17; goto _test_eof; - _test_eof18: cs = 18; goto _test_eof; - _test_eof27: cs = 27; goto _test_eof; - _test_eof19: cs = 19; goto _test_eof; - _test_eof20: cs = 20; goto _test_eof; - _test_eof21: cs = 21; goto _test_eof; - _test_eof22: cs = 22; goto _test_eof; - _test_eof23: cs = 23; goto _test_eof; - _test_eof24: cs = 24; goto _test_eof; - _test_eof25: cs = 25; goto _test_eof; - _test_eof26: cs = 26; goto _test_eof; + _test_eof2: cs = 2; goto _test_eof; + _test_eof3: cs = 3; goto _test_eof; + _test_eof4: cs = 4; goto _test_eof; + _test_eof5: cs = 5; goto _test_eof; + _test_eof6: cs = 6; goto _test_eof; + _test_eof7: cs = 7; goto _test_eof; + _test_eof8: cs = 8; goto _test_eof; + _test_eof9: cs = 9; goto _test_eof; + _test_eof10: cs = 10; goto _test_eof; + _test_eof11: cs = 11; goto _test_eof; + _test_eof12: cs = 12; goto _test_eof; + _test_eof13: cs = 13; goto _test_eof; + _test_eof14: cs = 14; goto _test_eof; + _test_eof15: cs = 15; goto _test_eof; + _test_eof16: cs = 16; goto _test_eof; + _test_eof17: cs = 17; goto _test_eof; + _test_eof18: cs = 18; goto _test_eof; + _test_eof27: cs = 27; goto _test_eof; + _test_eof19: cs = 19; goto _test_eof; + _test_eof20: cs = 20; goto _test_eof; + _test_eof21: cs = 21; goto _test_eof; + _test_eof22: cs = 22; goto _test_eof; + _test_eof23: cs = 23; goto _test_eof; + _test_eof24: cs = 24; goto _test_eof; + _test_eof25: cs = 25; goto _test_eof; + _test_eof26: cs = 26; goto _test_eof; _test_eof: {} _out: {} } -#line 160 "parser.rl" +#line 164 "parser.rl" if (cs >= JSON_object_first_final) { if (json->create_additions) { @@ -454,7 +458,7 @@ case 26: } -#line 458 "parser.c" +#line 462 "parser.c" static const int JSON_value_start = 1; static const int JSON_value_first_final = 21; static const int JSON_value_error = 0; @@ -462,22 +466,22 @@ static const int JSON_value_error = 0; static const int JSON_value_en_main = 1; -#line 258 "parser.rl" +#line 262 "parser.rl" static const char *JSON_parse_value(JSON_Parser *json, const char *p, const char *pe, VALUE *result) { int cs = EVIL; - -#line 474 "parser.c" + +#line 478 "parser.c" { cs = JSON_value_start; } -#line 265 "parser.rl" - -#line 481 "parser.c" +#line 269 "parser.rl" + +#line 485 "parser.c" { if ( p == pe ) goto _test_eof; @@ -502,14 +506,14 @@ st0: cs = 0; goto _out; tr0: -#line 206 "parser.rl" +#line 210 "parser.rl" { const char *np = JSON_parse_string(json, p, pe, result); if (np == NULL) { p--; {p++; cs = 21; goto _out;} } else {p = (( np))-1;} } goto st21; tr2: -#line 211 "parser.rl" +#line 215 "parser.rl" { const char *np; if(pe > p + 9 && !strncmp(MinusInfinity, p, 9)) { @@ -529,8 +533,8 @@ tr2: } goto st21; tr5: -#line 229 "parser.rl" - { +#line 233 "parser.rl" + { const char *np; json->current_nesting++; np = JSON_parse_array(json, p, pe, result); @@ -539,8 +543,8 @@ tr5: } goto st21; tr9: -#line 237 "parser.rl" - { +#line 241 "parser.rl" + { const char *np; json->current_nesting++; np = JSON_parse_object(json, p, pe, result); @@ -549,7 +553,7 @@ tr9: } goto st21; tr16: -#line 199 "parser.rl" +#line 203 "parser.rl" { if (json->allow_nan) { *result = CInfinity; @@ -559,7 +563,7 @@ tr16: } goto st21; tr18: -#line 192 "parser.rl" +#line 196 "parser.rl" { if (json->allow_nan) { *result = CNaN; @@ -569,19 +573,19 @@ tr18: } goto st21; tr22: -#line 186 "parser.rl" +#line 190 "parser.rl" { *result = Qfalse; } goto st21; tr25: -#line 183 "parser.rl" +#line 187 "parser.rl" { *result = Qnil; } goto st21; tr28: -#line 189 "parser.rl" +#line 193 "parser.rl" { *result = Qtrue; } @@ -590,9 +594,9 @@ st21: if ( ++p == pe ) goto _test_eof21; case 21: -#line 245 "parser.rl" +#line 249 "parser.rl" { p--; {p++; cs = 21; goto _out;} } -#line 596 "parser.c" +#line 600 "parser.c" goto st0; st2: if ( ++p == pe ) @@ -728,32 +732,32 @@ case 20: goto tr28; goto st0; } - _test_eof21: cs = 21; goto _test_eof; - _test_eof2: cs = 2; goto _test_eof; - _test_eof3: cs = 3; goto _test_eof; - _test_eof4: cs = 4; goto _test_eof; - _test_eof5: cs = 5; goto _test_eof; - _test_eof6: cs = 6; goto _test_eof; - _test_eof7: cs = 7; goto _test_eof; - _test_eof8: cs = 8; goto _test_eof; - _test_eof9: cs = 9; goto _test_eof; - _test_eof10: cs = 10; goto _test_eof; - _test_eof11: cs = 11; goto _test_eof; - _test_eof12: cs = 12; goto _test_eof; - _test_eof13: cs = 13; goto _test_eof; - _test_eof14: cs = 14; goto _test_eof; - _test_eof15: cs = 15; goto _test_eof; - _test_eof16: cs = 16; goto _test_eof; - _test_eof17: cs = 17; goto _test_eof; - _test_eof18: cs = 18; goto _test_eof; - _test_eof19: cs = 19; goto _test_eof; - _test_eof20: cs = 20; goto _test_eof; + _test_eof21: cs = 21; goto _test_eof; + _test_eof2: cs = 2; goto _test_eof; + _test_eof3: cs = 3; goto _test_eof; + _test_eof4: cs = 4; goto _test_eof; + _test_eof5: cs = 5; goto _test_eof; + _test_eof6: cs = 6; goto _test_eof; + _test_eof7: cs = 7; goto _test_eof; + _test_eof8: cs = 8; goto _test_eof; + _test_eof9: cs = 9; goto _test_eof; + _test_eof10: cs = 10; goto _test_eof; + _test_eof11: cs = 11; goto _test_eof; + _test_eof12: cs = 12; goto _test_eof; + _test_eof13: cs = 13; goto _test_eof; + _test_eof14: cs = 14; goto _test_eof; + _test_eof15: cs = 15; goto _test_eof; + _test_eof16: cs = 16; goto _test_eof; + _test_eof17: cs = 17; goto _test_eof; + _test_eof18: cs = 18; goto _test_eof; + _test_eof19: cs = 19; goto _test_eof; + _test_eof20: cs = 20; goto _test_eof; _test_eof: {} _out: {} } -#line 266 "parser.rl" +#line 270 "parser.rl" if (cs >= JSON_value_first_final) { return p; @@ -763,7 +767,7 @@ case 20: } -#line 767 "parser.c" +#line 771 "parser.c" static const int JSON_integer_start = 1; static const int JSON_integer_first_final = 5; static const int JSON_integer_error = 0; @@ -771,23 +775,23 @@ static const int JSON_integer_error = 0; static const int JSON_integer_en_main = 1; -#line 282 "parser.rl" +#line 286 "parser.rl" static const char *JSON_parse_integer(JSON_Parser *json, const char *p, const char *pe, VALUE *result) { int cs = EVIL; - -#line 783 "parser.c" + +#line 787 "parser.c" { cs = JSON_integer_start; } -#line 289 "parser.rl" +#line 293 "parser.rl" json->memo = p; - -#line 791 "parser.c" + +#line 795 "parser.c" { if ( p == pe ) goto _test_eof; @@ -821,14 +825,14 @@ case 3: goto st0; goto tr4; tr4: -#line 279 "parser.rl" +#line 283 "parser.rl" { p--; {p++; cs = 5; goto _out;} } goto st5; st5: if ( ++p == pe ) goto _test_eof5; case 5: -#line 832 "parser.c" +#line 836 "parser.c" goto st0; st4: if ( ++p == pe ) @@ -838,16 +842,16 @@ case 4: goto st4; goto tr4; } - _test_eof2: cs = 2; goto _test_eof; - _test_eof3: cs = 3; goto _test_eof; - _test_eof5: cs = 5; goto _test_eof; - _test_eof4: cs = 4; goto _test_eof; + _test_eof2: cs = 2; goto _test_eof; + _test_eof3: cs = 3; goto _test_eof; + _test_eof5: cs = 5; goto _test_eof; + _test_eof4: cs = 4; goto _test_eof; _test_eof: {} _out: {} } -#line 291 "parser.rl" +#line 295 "parser.rl" if (cs >= JSON_integer_first_final) { long len = p - json->memo; @@ -859,7 +863,7 @@ case 4: } -#line 863 "parser.c" +#line 867 "parser.c" static const int JSON_float_start = 1; static const int JSON_float_first_final = 10; static const int JSON_float_error = 0; @@ -867,23 +871,23 @@ static const int JSON_float_error = 0; static const int JSON_float_en_main = 1; -#line 313 "parser.rl" +#line 317 "parser.rl" static const char *JSON_parse_float(JSON_Parser *json, const char *p, const char *pe, VALUE *result) { int cs = EVIL; - -#line 879 "parser.c" + +#line 883 "parser.c" { cs = JSON_float_start; } -#line 320 "parser.rl" +#line 324 "parser.rl" json->memo = p; - -#line 887 "parser.c" + +#line 891 "parser.c" { if ( p == pe ) goto _test_eof; @@ -941,14 +945,14 @@ case 5: goto st0; goto tr7; tr7: -#line 307 "parser.rl" +#line 311 "parser.rl" { p--; {p++; cs = 10; goto _out;} } goto st10; st10: if ( ++p == pe ) goto _test_eof10; case 10: -#line 952 "parser.c" +#line 956 "parser.c" goto st0; st6: if ( ++p == pe ) @@ -995,21 +999,21 @@ case 9: goto st9; goto st0; } - _test_eof2: cs = 2; goto _test_eof; - _test_eof3: cs = 3; goto _test_eof; - _test_eof4: cs = 4; goto _test_eof; - _test_eof5: cs = 5; goto _test_eof; - _test_eof10: cs = 10; goto _test_eof; - _test_eof6: cs = 6; goto _test_eof; - _test_eof7: cs = 7; goto _test_eof; - _test_eof8: cs = 8; goto _test_eof; - _test_eof9: cs = 9; goto _test_eof; + _test_eof2: cs = 2; goto _test_eof; + _test_eof3: cs = 3; goto _test_eof; + _test_eof4: cs = 4; goto _test_eof; + _test_eof5: cs = 5; goto _test_eof; + _test_eof10: cs = 10; goto _test_eof; + _test_eof6: cs = 6; goto _test_eof; + _test_eof7: cs = 7; goto _test_eof; + _test_eof8: cs = 8; goto _test_eof; + _test_eof9: cs = 9; goto _test_eof; _test_eof: {} _out: {} } -#line 322 "parser.rl" +#line 326 "parser.rl" if (cs >= JSON_float_first_final) { long len = p - json->memo; @@ -1022,7 +1026,7 @@ case 9: -#line 1026 "parser.c" +#line 1030 "parser.c" static const int JSON_array_start = 1; static const int JSON_array_first_final = 17; static const int JSON_array_error = 0; @@ -1030,7 +1034,7 @@ static const int JSON_array_error = 0; static const int JSON_array_en_main = 1; -#line 358 "parser.rl" +#line 366 "parser.rl" static const char *JSON_parse_array(JSON_Parser *json, const char *p, const char *pe, VALUE *result) @@ -1043,15 +1047,15 @@ static const char *JSON_parse_array(JSON_Parser *json, const char *p, const char } *result = NIL_P(array_class) ? rb_ary_new() : rb_class_new_instance(0, 0, array_class); - -#line 1048 "parser.c" + +#line 1052 "parser.c" { cs = JSON_array_start; } -#line 371 "parser.rl" - -#line 1055 "parser.c" +#line 379 "parser.rl" + +#line 1059 "parser.c" { if ( p == pe ) goto _test_eof; @@ -1090,14 +1094,18 @@ case 2: goto st2; goto st0; tr2: -#line 339 "parser.rl" +#line 343 "parser.rl" { VALUE v = Qnil; - const char *np = JSON_parse_value(json, p, pe, &v); + const char *np = JSON_parse_value(json, p, pe, &v); if (np == NULL) { p--; {p++; cs = 3; goto _out;} } else { - rb_ary_push(*result, v); + if (NIL_P(json->array_class)) { + rb_ary_push(*result, v); + } else { + rb_funcall(*result, i_leftshift, 1, v); + } {p = (( np))-1;} } } @@ -1106,7 +1114,7 @@ st3: if ( ++p == pe ) goto _test_eof3; case 3: -#line 1110 "parser.c" +#line 1118 "parser.c" switch( (*p) ) { case 13: goto st3; case 32: goto st3; @@ -1206,14 +1214,14 @@ case 12: goto st3; goto st12; tr4: -#line 350 "parser.rl" +#line 358 "parser.rl" { p--; {p++; cs = 17; goto _out;} } goto st17; st17: if ( ++p == pe ) goto _test_eof17; case 17: -#line 1217 "parser.c" +#line 1225 "parser.c" goto st0; st13: if ( ++p == pe ) @@ -1248,28 +1256,28 @@ case 16: goto st2; goto st16; } - _test_eof2: cs = 2; goto _test_eof; - _test_eof3: cs = 3; goto _test_eof; - _test_eof4: cs = 4; goto _test_eof; - _test_eof5: cs = 5; goto _test_eof; - _test_eof6: cs = 6; goto _test_eof; - _test_eof7: cs = 7; goto _test_eof; - _test_eof8: cs = 8; goto _test_eof; - _test_eof9: cs = 9; goto _test_eof; - _test_eof10: cs = 10; goto _test_eof; - _test_eof11: cs = 11; goto _test_eof; - _test_eof12: cs = 12; goto _test_eof; - _test_eof17: cs = 17; goto _test_eof; - _test_eof13: cs = 13; goto _test_eof; - _test_eof14: cs = 14; goto _test_eof; - _test_eof15: cs = 15; goto _test_eof; - _test_eof16: cs = 16; goto _test_eof; + _test_eof2: cs = 2; goto _test_eof; + _test_eof3: cs = 3; goto _test_eof; + _test_eof4: cs = 4; goto _test_eof; + _test_eof5: cs = 5; goto _test_eof; + _test_eof6: cs = 6; goto _test_eof; + _test_eof7: cs = 7; goto _test_eof; + _test_eof8: cs = 8; goto _test_eof; + _test_eof9: cs = 9; goto _test_eof; + _test_eof10: cs = 10; goto _test_eof; + _test_eof11: cs = 11; goto _test_eof; + _test_eof12: cs = 12; goto _test_eof; + _test_eof17: cs = 17; goto _test_eof; + _test_eof13: cs = 13; goto _test_eof; + _test_eof14: cs = 14; goto _test_eof; + _test_eof15: cs = 15; goto _test_eof; + _test_eof16: cs = 16; goto _test_eof; _test_eof: {} _out: {} } -#line 372 "parser.rl" +#line 380 "parser.rl" if(cs >= JSON_array_first_final) { return p + 1; @@ -1312,7 +1320,7 @@ static VALUE json_string_unescape(VALUE result, const char *string, const char * unescape = (char *) "\f"; break; case 'u': - if (pe > stringEnd - 4) { + if (pe > stringEnd - 4) { return Qnil; } else { char buf[4]; @@ -1350,7 +1358,7 @@ static VALUE json_string_unescape(VALUE result, const char *string, const char * } -#line 1354 "parser.c" +#line 1362 "parser.c" static const int JSON_string_start = 1; static const int JSON_string_first_final = 8; static const int JSON_string_error = 0; @@ -1358,7 +1366,7 @@ static const int JSON_string_error = 0; static const int JSON_string_en_main = 1; -#line 471 "parser.rl" +#line 479 "parser.rl" static int @@ -1377,16 +1385,16 @@ static const char *JSON_parse_string(JSON_Parser *json, const char *p, const cha { int cs = EVIL; *result = rb_str_buf_new(0); - -#line 1382 "parser.c" + +#line 1390 "parser.c" { cs = JSON_string_start; } -#line 490 "parser.rl" +#line 498 "parser.rl" json->memo = p; - -#line 1390 "parser.c" + +#line 1398 "parser.c" { if ( p == pe ) goto _test_eof; @@ -1411,7 +1419,7 @@ case 2: goto st0; goto st2; tr2: -#line 457 "parser.rl" +#line 465 "parser.rl" { *result = json_string_unescape(*result, json->memo + 1, p); if (NIL_P(*result)) { @@ -1422,14 +1430,14 @@ tr2: {p = (( p + 1))-1;} } } -#line 468 "parser.rl" +#line 476 "parser.rl" { p--; {p++; cs = 8; goto _out;} } goto st8; st8: if ( ++p == pe ) goto _test_eof8; case 8: -#line 1433 "parser.c" +#line 1441 "parser.c" goto st0; st3: if ( ++p == pe ) @@ -1493,19 +1501,19 @@ case 7: goto st2; goto st0; } - _test_eof2: cs = 2; goto _test_eof; - _test_eof8: cs = 8; goto _test_eof; - _test_eof3: cs = 3; goto _test_eof; - _test_eof4: cs = 4; goto _test_eof; - _test_eof5: cs = 5; goto _test_eof; - _test_eof6: cs = 6; goto _test_eof; - _test_eof7: cs = 7; goto _test_eof; + _test_eof2: cs = 2; goto _test_eof; + _test_eof8: cs = 8; goto _test_eof; + _test_eof3: cs = 3; goto _test_eof; + _test_eof4: cs = 4; goto _test_eof; + _test_eof5: cs = 5; goto _test_eof; + _test_eof6: cs = 6; goto _test_eof; + _test_eof7: cs = 7; goto _test_eof; _test_eof: {} _out: {} } -#line 492 "parser.rl" +#line 500 "parser.rl" if (json->create_additions) { VALUE match_string = json->match_string; @@ -1533,7 +1541,7 @@ case 7: -#line 1537 "parser.c" +#line 1545 "parser.c" static const int JSON_start = 1; static const int JSON_first_final = 10; static const int JSON_error = 0; @@ -1541,10 +1549,10 @@ static const int JSON_error = 0; static const int JSON_en_main = 1; -#line 543 "parser.rl" +#line 551 "parser.rl" -/* +/* * Document-class: JSON::Ext::Parser * * This is the JSON parser implemented as a C extension. It can be configured @@ -1568,22 +1576,15 @@ static VALUE convert_encoding(VALUE source) VALUE encoding = rb_funcall(source, i_encoding, 0); if (encoding == CEncoding_ASCII_8BIT) { if (len >= 4 && ptr[0] == 0 && ptr[1] == 0 && ptr[2] == 0) { - source = rb_str_dup(source); - rb_funcall(source, i_force_encoding, 1, CEncoding_UTF_32BE); - source = rb_funcall(source, i_encode_bang, 1, CEncoding_UTF_8); + source = rb_funcall(source, i_encode, 2, CEncoding_UTF_8, CEncoding_UTF_32BE); } else if (len >= 4 && ptr[0] == 0 && ptr[2] == 0) { - source = rb_str_dup(source); - rb_funcall(source, i_force_encoding, 1, CEncoding_UTF_16BE); - source = rb_funcall(source, i_encode_bang, 1, CEncoding_UTF_8); + source = rb_funcall(source, i_encode, 2, CEncoding_UTF_8, CEncoding_UTF_16BE); } else if (len >= 4 && ptr[1] == 0 && ptr[2] == 0 && ptr[3] == 0) { - source = rb_str_dup(source); - rb_funcall(source, i_force_encoding, 1, CEncoding_UTF_32LE); - source = rb_funcall(source, i_encode_bang, 1, CEncoding_UTF_8); + source = rb_funcall(source, i_encode, 2, CEncoding_UTF_8, CEncoding_UTF_32LE); } else if (len >= 4 && ptr[1] == 0 && ptr[3] == 0) { - source = rb_str_dup(source); - rb_funcall(source, i_force_encoding, 1, CEncoding_UTF_16LE); - source = rb_funcall(source, i_encode_bang, 1, CEncoding_UTF_8); + source = rb_funcall(source, i_encode, 2, CEncoding_UTF_8, CEncoding_UTF_16LE); } else { + source = rb_str_dup(source); FORCE_UTF8(source); } } else { @@ -1606,7 +1607,7 @@ static VALUE convert_encoding(VALUE source) static inline void parser_iv_set(JSON_Parser *json, const char* iv_name, VALUE v) { - // store reference to v in a Ruby instVar to keep v alive + // store reference to v in a Ruby instVar to keep v alive // without using a gc_mark function in Data_Wrap_Struct calls rb_iv_set(json->dwrapped_parser, iv_name, v); } @@ -1644,8 +1645,13 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) const char *ptr; long len; VALUE source, opts; - GET_PARSER; -init_count += 1; + + GET_PARSER_INIT; + init_count += 1; + + if (json->Vsource) { + rb_raise(rb_eTypeError, "already initialized instance"); + } rb_scan_args(argc, argv, "11", &source, &opts); source = convert_encoding(StringValue(source)); ptr = RSTRING_PTR(source); @@ -1737,17 +1743,17 @@ static VALUE cParser_parse(VALUE self) VALUE result = Qnil; GET_PARSER; - -#line 1742 "parser.c" + +#line 1748 "parser.c" { cs = JSON_start; } -#line 739 "parser.rl" +#line 745 "parser.rl" p = json->source; pe = p + json->len; - -#line 1751 "parser.c" + +#line 1757 "parser.c" { if ( p == pe ) goto _test_eof; @@ -1803,7 +1809,7 @@ case 5: goto st1; goto st5; tr3: -#line 532 "parser.rl" +#line 540 "parser.rl" { const char *np; json->current_nesting = 1; @@ -1812,7 +1818,7 @@ tr3: } goto st10; tr4: -#line 525 "parser.rl" +#line 533 "parser.rl" { const char *np; json->current_nesting = 1; @@ -1824,7 +1830,7 @@ st10: if ( ++p == pe ) goto _test_eof10; case 10: -#line 1828 "parser.c" +#line 1834 "parser.c" switch( (*p) ) { case 13: goto st10; case 32: goto st10; @@ -1866,22 +1872,22 @@ case 9: goto st10; goto st9; } - _test_eof1: cs = 1; goto _test_eof; - _test_eof2: cs = 2; goto _test_eof; - _test_eof3: cs = 3; goto _test_eof; - _test_eof4: cs = 4; goto _test_eof; - _test_eof5: cs = 5; goto _test_eof; - _test_eof10: cs = 10; goto _test_eof; - _test_eof6: cs = 6; goto _test_eof; - _test_eof7: cs = 7; goto _test_eof; - _test_eof8: cs = 8; goto _test_eof; - _test_eof9: cs = 9; goto _test_eof; + _test_eof1: cs = 1; goto _test_eof; + _test_eof2: cs = 2; goto _test_eof; + _test_eof3: cs = 3; goto _test_eof; + _test_eof4: cs = 4; goto _test_eof; + _test_eof5: cs = 5; goto _test_eof; + _test_eof10: cs = 10; goto _test_eof; + _test_eof6: cs = 6; goto _test_eof; + _test_eof7: cs = 7; goto _test_eof; + _test_eof8: cs = 8; goto _test_eof; + _test_eof9: cs = 9; goto _test_eof; _test_eof: {} _out: {} } -#line 742 "parser.rl" +#line 748 "parser.rl" if (cs >= JSON_first_final && p == pe) { return result; @@ -1963,6 +1969,8 @@ void Init_parser() i_match_string = rb_intern("match_string"); i_key_p = rb_intern("key?"); i_deep_const_get = rb_intern("deep_const_get"); + i_aset = rb_intern("[]="); + i_leftshift = rb_intern("<<"); #ifdef HAVE_RUBY_ENCODING_H CEncoding_UTF_8 = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8")); CEncoding_UTF_16BE = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-16be")); @@ -1972,9 +1980,15 @@ void Init_parser() CEncoding_ASCII_8BIT = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("ascii-8bit")); i_encoding = rb_intern("encoding"); i_encode = rb_intern("encode"); - i_encode_bang = rb_intern("encode!"); - i_force_encoding = rb_intern("force_encoding"); #else i_iconv = rb_intern("iconv"); #endif } + +/* + * Local variables: + * mode: c + * c-file-style: ruby + * indent-tabs-mode: nil + * End: + */ diff --git a/ext/json/ext/parser/parser.h b/ext/json/ext/parser/parser.h index 1ebb9c8..5571787 100644 --- a/ext/json/ext/parser/parser.h +++ b/ext/json/ext/parser/parser.h @@ -9,7 +9,7 @@ #ifdef HAVE_RUBY_ENCODING_H #include "ruby/encoding.h" -#define FORCE_UTF8(obj) rb_enc_associate((obj), rb_utf8_encoding()) +#define FORCE_UTF8(obj) ((obj) = rb_enc_associate(rb_str_dup(obj), rb_utf8_encoding())) #else #define FORCE_UTF8(obj) #endif @@ -23,9 +23,9 @@ /* unicode */ -typedef unsigned long UTF32; /* at least 32 bits */ -typedef unsigned short UTF16; /* at least 16 bits */ -typedef unsigned char UTF8; /* typically 8 bits */ +typedef unsigned long UTF32; /* at least 32 bits */ +typedef unsigned short UTF16; /* at least 16 bits */ +typedef unsigned char UTF8; /* typically 8 bits */ #define UNI_REPLACEMENT_CHAR (UTF32)0x0000FFFD #define UNI_SUR_HIGH_START (UTF32)0xD800 @@ -52,6 +52,9 @@ typedef struct JSON_ParserStruct { } JSON_Parser; #define GET_PARSER \ + GET_PARSER_INIT; \ + if (!json->Vsource) rb_raise(rb_eTypeError, "uninitialized instance") +#define GET_PARSER_INIT \ JSON_Parser *json; \ Data_Get_Struct(self, JSON_Parser, json) diff --git a/ext/json/ext/parser/parser.rl b/ext/json/ext/parser/parser.rl index e8ee5c1..fad8ac7 100644 --- a/ext/json/ext/parser/parser.rl +++ b/ext/json/ext/parser/parser.rl @@ -2,7 +2,7 @@ /* unicode */ -static const char digit_values[256] = { +static const char digit_values[256] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, @@ -38,7 +38,7 @@ static UTF32 unescape_unicode(const unsigned char *p) return result; } -static int convert_UTF32_to_UTF8(char *buf, UTF32 ch) +static int convert_UTF32_to_UTF8(char *buf, UTF32 ch) { int len = 1; if (ch <= 0x7F) { @@ -67,7 +67,7 @@ static int convert_UTF32_to_UTF8(char *buf, UTF32 ch) #ifdef HAVE_RUBY_ENCODING_H static VALUE CEncoding_ASCII_8BIT, CEncoding_UTF_8, CEncoding_UTF_16BE, CEncoding_UTF_16LE, CEncoding_UTF_32BE, CEncoding_UTF_32LE; -static ID i_encoding, i_encode, i_encode_bang, i_force_encoding; +static ID i_encoding, i_encode; #else static ID i_iconv; #endif @@ -77,7 +77,7 @@ static VALUE CNaN, CInfinity, CMinusInfinity; static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions, i_chr, i_max_nesting, i_allow_nan, i_symbolize_names, i_object_class, - i_array_class, i_key_p, i_deep_const_get, i_match, i_match_string; + i_array_class, i_key_p, i_deep_const_get, i_match, i_match_string, i_aset, i_leftshift; %%{ machine JSON_common; @@ -97,7 +97,7 @@ static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions, VNaN = 'NaN'; VInfinity = 'Infinity'; VMinusInfinity = '-Infinity'; - begin_value = [nft"\-[{NI] | digit; + begin_value = [nft\"\-\[\{NI] | digit; begin_object = '{'; end_object = '}'; begin_array = '['; @@ -115,11 +115,15 @@ static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions, action parse_value { VALUE v = Qnil; - const char *np = JSON_parse_value(json, fpc, pe, &v); + const char *np = JSON_parse_value(json, fpc, pe, &v); if (np == NULL) { fhold; fbreak; } else { - rb_hash_aset(*result, last_name, v); + if (NIL_P(json->object_class)) { + rb_hash_aset(*result, last_name, v); + } else { + rb_funcall(*result, i_aset, 2, last_name, v); + } fexec np; } } @@ -226,7 +230,7 @@ static const char *JSON_parse_object(JSON_Parser *json, const char *p, const cha fhold; fbreak; } - action parse_array { + action parse_array { const char *np; json->current_nesting++; np = JSON_parse_array(json, fpc, pe, result); @@ -234,7 +238,7 @@ static const char *JSON_parse_object(JSON_Parser *json, const char *p, const cha if (np == NULL) { fhold; fbreak; } else fexec np; } - action parse_object { + action parse_object { const char *np; json->current_nesting++; np = JSON_parse_object(json, fpc, pe, result); @@ -338,11 +342,15 @@ static const char *JSON_parse_float(JSON_Parser *json, const char *p, const char action parse_value { VALUE v = Qnil; - const char *np = JSON_parse_value(json, fpc, pe, &v); + const char *np = JSON_parse_value(json, fpc, pe, &v); if (np == NULL) { fhold; fbreak; } else { - rb_ary_push(*result, v); + if (NIL_P(json->array_class)) { + rb_ary_push(*result, v); + } else { + rb_funcall(*result, i_leftshift, 1, v); + } fexec np; } } @@ -411,7 +419,7 @@ static VALUE json_string_unescape(VALUE result, const char *string, const char * unescape = (char *) "\f"; break; case 'u': - if (pe > stringEnd - 4) { + if (pe > stringEnd - 4) { return Qnil; } else { char buf[4]; @@ -467,7 +475,7 @@ static VALUE json_string_unescape(VALUE result, const char *string, const char * action exit { fhold; fbreak; } - main := '"' ((^(["\\] | 0..0x1f) | '\\'["\\/bfnrt] | '\\u'[0-9a-fA-F]{4} | '\\'^(["\\/bfnrtu]|0..0x1f))* %parse_string) '"' @exit; + main := '"' ((^([\"\\] | 0..0x1f) | '\\'[\"\\/bfnrt] | '\\u'[0-9a-fA-F]{4} | '\\'^([\"\\/bfnrtu]|0..0x1f))* %parse_string) '"' @exit; }%% static int @@ -542,7 +550,7 @@ static const char *JSON_parse_string(JSON_Parser *json, const char *p, const cha ) ignore*; }%% -/* +/* * Document-class: JSON::Ext::Parser * * This is the JSON parser implemented as a C extension. It can be configured @@ -566,22 +574,15 @@ static VALUE convert_encoding(VALUE source) VALUE encoding = rb_funcall(source, i_encoding, 0); if (encoding == CEncoding_ASCII_8BIT) { if (len >= 4 && ptr[0] == 0 && ptr[1] == 0 && ptr[2] == 0) { - source = rb_str_dup(source); - rb_funcall(source, i_force_encoding, 1, CEncoding_UTF_32BE); - source = rb_funcall(source, i_encode_bang, 1, CEncoding_UTF_8); + source = rb_funcall(source, i_encode, 2, CEncoding_UTF_8, CEncoding_UTF_32BE); } else if (len >= 4 && ptr[0] == 0 && ptr[2] == 0) { - source = rb_str_dup(source); - rb_funcall(source, i_force_encoding, 1, CEncoding_UTF_16BE); - source = rb_funcall(source, i_encode_bang, 1, CEncoding_UTF_8); + source = rb_funcall(source, i_encode, 2, CEncoding_UTF_8, CEncoding_UTF_16BE); } else if (len >= 4 && ptr[1] == 0 && ptr[2] == 0 && ptr[3] == 0) { - source = rb_str_dup(source); - rb_funcall(source, i_force_encoding, 1, CEncoding_UTF_32LE); - source = rb_funcall(source, i_encode_bang, 1, CEncoding_UTF_8); + source = rb_funcall(source, i_encode, 2, CEncoding_UTF_8, CEncoding_UTF_32LE); } else if (len >= 4 && ptr[1] == 0 && ptr[3] == 0) { - source = rb_str_dup(source); - rb_funcall(source, i_force_encoding, 1, CEncoding_UTF_16LE); - source = rb_funcall(source, i_encode_bang, 1, CEncoding_UTF_8); + source = rb_funcall(source, i_encode, 2, CEncoding_UTF_8, CEncoding_UTF_16LE); } else { + source = rb_str_dup(source); FORCE_UTF8(source); } } else { @@ -604,7 +605,7 @@ static VALUE convert_encoding(VALUE source) static inline void parser_iv_set(JSON_Parser *json, const char* iv_name, VALUE v) { - // store reference to v in a Ruby instVar to keep v alive + // store reference to v in a Ruby instVar to keep v alive // without using a gc_mark function in Data_Wrap_Struct calls rb_iv_set(json->dwrapped_parser, iv_name, v); } @@ -642,8 +643,13 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) const char *ptr; long len; VALUE source, opts; - GET_PARSER; -init_count += 1; + + GET_PARSER_INIT; + init_count += 1; + + if (json->Vsource) { + rb_raise(rb_eTypeError, "already initialized instance"); + } rb_scan_args(argc, argv, "11", &source, &opts); source = convert_encoding(StringValue(source)); ptr = RSTRING_PTR(source); @@ -820,6 +826,8 @@ void Init_parser() i_match_string = rb_intern("match_string"); i_key_p = rb_intern("key?"); i_deep_const_get = rb_intern("deep_const_get"); + i_aset = rb_intern("[]="); + i_leftshift = rb_intern("<<"); #ifdef HAVE_RUBY_ENCODING_H CEncoding_UTF_8 = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8")); CEncoding_UTF_16BE = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-16be")); @@ -829,9 +837,15 @@ void Init_parser() CEncoding_ASCII_8BIT = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("ascii-8bit")); i_encoding = rb_intern("encoding"); i_encode = rb_intern("encode"); - i_encode_bang = rb_intern("encode!"); - i_force_encoding = rb_intern("force_encoding"); #else i_iconv = rb_intern("iconv"); #endif } + +/* + * Local variables: + * mode: c + * c-file-style: ruby + * indent-tabs-mode: nil + * End: + */ diff --git a/java/src/json/ext/Generator.java b/java/src/json/ext/Generator.java index 230d68f..fbc394f 100644 --- a/java/src/json/ext/Generator.java +++ b/java/src/json/ext/Generator.java @@ -354,11 +354,7 @@ public final class Generator { state.decreaseDepth(); if (objectNl.length() != 0) { buffer.append(objectNl); - if (indent.length != 0) { - for (int i = 0; i < state.getDepth(); i++) { - buffer.append(indent); - } - } + buffer.append(Utils.repeat(state.getIndent(), state.getDepth())); } buffer.append((byte)'}'); } @@ -380,9 +376,9 @@ public final class Generator { RubyString src; if (info.encodingsSupported() && - object.encoding(session.getContext()) != info.utf8) { + object.encoding(session.getContext()) != info.utf8.get()) { src = (RubyString)object.encode(session.getContext(), - info.utf8); + info.utf8.get()); } else { src = object; } diff --git a/java/src/json/ext/GeneratorMethods.java b/java/src/json/ext/GeneratorMethods.java index 28a612d..356f2d0 100644 --- a/java/src/json/ext/GeneratorMethods.java +++ b/java/src/json/ext/GeneratorMethods.java @@ -6,6 +6,7 @@ */ package json.ext; +import java.lang.ref.WeakReference; import org.jruby.Ruby; import org.jruby.RubyArray; import org.jruby.RubyBoolean; @@ -45,9 +46,9 @@ class GeneratorMethods { defineMethods(module, "String", RbString.class); defineMethods(module, "TrueClass", RbTrue.class); - info.stringExtendModule = module.defineModuleUnder("String") - .defineModuleUnder("Extend"); - info.stringExtendModule.defineAnnotatedMethods(StringExtend.class); + info.stringExtendModule = new WeakReference<RubyModule>(module.defineModuleUnder("String") + .defineModuleUnder("Extend")); + info.stringExtendModule.get().defineAnnotatedMethods(StringExtend.class); } /** @@ -140,7 +141,7 @@ class GeneratorMethods { RubyHash result = RubyHash.newHash(runtime); IRubyObject createId = RuntimeInfo.forRuntime(runtime) - .jsonModule.callMethod(context, "create_id"); + .jsonModule.get().callMethod(context, "create_id"); result.op_aset(context, createId, self.getMetaClass().to_s()); ByteList bl = self.getByteList(); @@ -158,7 +159,7 @@ class GeneratorMethods { public static IRubyObject included(ThreadContext context, IRubyObject vSelf, IRubyObject module) { RuntimeInfo info = RuntimeInfo.forRuntime(context.getRuntime()); - return module.callMethod(context, "extend", info.stringExtendModule); + return module.callMethod(context, "extend", info.stringExtendModule.get()); } } diff --git a/java/src/json/ext/GeneratorService.java b/java/src/json/ext/GeneratorService.java index b8deb22..2f3b07e 100644 --- a/java/src/json/ext/GeneratorService.java +++ b/java/src/json/ext/GeneratorService.java @@ -7,6 +7,7 @@ package json.ext; import java.io.IOException; +import java.lang.ref.WeakReference; import org.jruby.Ruby; import org.jruby.RubyClass; @@ -23,15 +24,15 @@ public class GeneratorService implements BasicLibraryService { runtime.getLoadService().require("json/common"); RuntimeInfo info = RuntimeInfo.initRuntime(runtime); - info.jsonModule = runtime.defineModule("JSON"); - RubyModule jsonExtModule = info.jsonModule.defineModuleUnder("Ext"); + info.jsonModule = new WeakReference<RubyModule>(runtime.defineModule("JSON")); + RubyModule jsonExtModule = info.jsonModule.get().defineModuleUnder("Ext"); RubyModule generatorModule = jsonExtModule.defineModuleUnder("Generator"); RubyClass stateClass = generatorModule.defineClassUnder("State", runtime.getObject(), GeneratorState.ALLOCATOR); stateClass.defineAnnotatedMethods(GeneratorState.class); - info.generatorStateClass = stateClass; + info.generatorStateClass = new WeakReference<RubyClass>(stateClass); RubyModule generatorMethods = generatorModule.defineModuleUnder("GeneratorMethods"); diff --git a/java/src/json/ext/GeneratorState.java b/java/src/json/ext/GeneratorState.java index dc99000..f04eda2 100644 --- a/java/src/json/ext/GeneratorState.java +++ b/java/src/json/ext/GeneratorState.java @@ -118,7 +118,7 @@ public class GeneratorState extends RubyObject { static GeneratorState fromState(ThreadContext context, RuntimeInfo info, IRubyObject opts) { - RubyClass klass = info.generatorStateClass; + RubyClass klass = info.generatorStateClass.get(); if (opts != null) { // if the given parameter is a Generator::State, return itself if (klass.isInstance(opts)) return (GeneratorState)opts; @@ -382,8 +382,8 @@ public class GeneratorState extends RubyObject { private ByteList prepareByteList(ThreadContext context, IRubyObject value) { RubyString str = value.convertToString(); RuntimeInfo info = RuntimeInfo.forRuntime(context.getRuntime()); - if (info.encodingsSupported() && str.encoding(context) != info.utf8) { - str = (RubyString)str.encode(context, info.utf8); + if (info.encodingsSupported() && str.encoding(context) != info.utf8.get()) { + str = (RubyString)str.encode(context, info.utf8.get()); } return str.getByteList().dup(); } diff --git a/java/src/json/ext/OptionsReader.java b/java/src/json/ext/OptionsReader.java index 018ace4..c9c9c94 100644 --- a/java/src/json/ext/OptionsReader.java +++ b/java/src/json/ext/OptionsReader.java @@ -84,8 +84,8 @@ final class OptionsReader { RubyString str = value.convertToString(); RuntimeInfo info = getRuntimeInfo(); - if (info.encodingsSupported() && str.encoding(context) != info.utf8) { - str = (RubyString)str.encode(context, info.utf8); + if (info.encodingsSupported() && str.encoding(context) != info.utf8.get()) { + str = (RubyString)str.encode(context, info.utf8.get()); } return str; } diff --git a/java/src/json/ext/Parser.java b/java/src/json/ext/Parser.java index c92600e..cea42d4 100644 --- a/java/src/json/ext/Parser.java +++ b/java/src/json/ext/Parser.java @@ -144,7 +144,10 @@ public class Parser extends RubyObject { @JRubyMethod(required = 1, optional = 1, visibility = Visibility.PRIVATE) public IRubyObject initialize(ThreadContext context, IRubyObject[] args) { - Ruby runtime = context.getRuntime(); + Ruby runtime = context.getRuntime(); + if (this.vSource != null) { + throw runtime.newTypeError("already initialized instance"); + } RubyString source = convertEncoding(context, args[0].convertToString()); OptionsReader opts = new OptionsReader(context, args.length > 1 ? args[1] : null); @@ -176,8 +179,8 @@ public class Parser extends RubyObject { if (info.encodingsSupported()) { RubyEncoding encoding = (RubyEncoding)source.encoding(context); - if (encoding != info.ascii8bit) { - return (RubyString)source.encode(context, info.utf8); + if (encoding != info.ascii8bit.get()) { + return (RubyString)source.encode(context, info.utf8.get()); } String sniffedEncoding = sniffByteList(bl); @@ -188,7 +191,7 @@ public class Parser extends RubyObject { String sniffedEncoding = sniffByteList(bl); if (sniffedEncoding == null) return source; // assume UTF-8 Ruby runtime = context.getRuntime(); - return (RubyString)info.jsonModule. + return (RubyString)info.jsonModule.get(). callMethod(context, "iconv", new IRubyObject[] { runtime.newString("utf-8"), @@ -218,7 +221,7 @@ public class Parser extends RubyObject { private RubyString reinterpretEncoding(ThreadContext context, RubyString str, String sniffedEncoding) { RubyEncoding actualEncoding = info.getEncoding(context, sniffedEncoding); - RubyEncoding targetEncoding = info.utf8; + RubyEncoding targetEncoding = info.utf8.get(); RubyString dup = (RubyString)str.dup(); dup.force_encoding(context, actualEncoding); return (RubyString)dup.encode_bang(context, targetEncoding); @@ -243,7 +246,15 @@ public class Parser extends RubyObject { */ @JRubyMethod(name = "source") public IRubyObject source_get() { - return vSource.dup(); + return checkAndGetSource().dup(); + } + + public RubyString checkAndGetSource() { + if (vSource != null) { + return vSource; + } else { + throw getRuntime().newTypeError("uninitialized instance"); + } } /** @@ -251,7 +262,7 @@ public class Parser extends RubyObject { * set to <code>nil</code> or <code>false</code>, and a String if not. */ private RubyString getCreateId(ThreadContext context) { - IRubyObject v = info.jsonModule.callMethod(context, "create_id"); + IRubyObject v = info.jsonModule.get().callMethod(context, "create_id"); return v.isTrue() ? v.convertToString() : null; } @@ -281,7 +292,7 @@ public class Parser extends RubyObject { private ParserSession(Parser parser, ThreadContext context) { this.parser = parser; this.context = context; - this.byteList = parser.vSource.getByteList(); + this.byteList = parser.checkAndGetSource().getByteList(); this.data = byteList.unsafeBytes(); this.decoder = new StringDecoder(context); } @@ -298,11 +309,11 @@ public class Parser extends RubyObject { } -// line 324 "Parser.rl" +// line 335 "Parser.rl" -// line 306 "Parser.java" +// line 317 "Parser.java" private static byte[] init__JSON_value_actions_0() { return new byte [] { @@ -416,7 +427,7 @@ static final int JSON_value_error = 0; static final int JSON_value_en_main = 1; -// line 430 "Parser.rl" +// line 441 "Parser.rl" ParserResult parseValue(int p, int pe) { @@ -424,14 +435,14 @@ static final int JSON_value_en_main = 1; IRubyObject result = null; -// line 428 "Parser.java" +// line 439 "Parser.java" { cs = JSON_value_start; } -// line 437 "Parser.rl" +// line 448 "Parser.rl" -// line 435 "Parser.java" +// line 446 "Parser.java" { int _klen; int _trans = 0; @@ -457,13 +468,13 @@ case 1: while ( _nacts-- > 0 ) { switch ( _JSON_value_actions[_acts++] ) { case 9: -// line 415 "Parser.rl" +// line 426 "Parser.rl" { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } break; -// line 467 "Parser.java" +// line 478 "Parser.java" } } @@ -526,25 +537,25 @@ case 1: switch ( _JSON_value_actions[_acts++] ) { case 0: -// line 332 "Parser.rl" +// line 343 "Parser.rl" { result = getRuntime().getNil(); } break; case 1: -// line 335 "Parser.rl" +// line 346 "Parser.rl" { result = getRuntime().getFalse(); } break; case 2: -// line 338 "Parser.rl" +// line 349 "Parser.rl" { result = getRuntime().getTrue(); } break; case 3: -// line 341 "Parser.rl" +// line 352 "Parser.rl" { if (parser.allowNaN) { result = getConstant(CONST_NAN); @@ -554,7 +565,7 @@ case 1: } break; case 4: -// line 348 "Parser.rl" +// line 359 "Parser.rl" { if (parser.allowNaN) { result = getConstant(CONST_INFINITY); @@ -564,7 +575,7 @@ case 1: } break; case 5: -// line 355 "Parser.rl" +// line 366 "Parser.rl" { if (pe > p + 9 && absSubSequence(p, p + 9).toString().equals(JSON_MINUS_INFINITY)) { @@ -593,7 +604,7 @@ case 1: } break; case 6: -// line 381 "Parser.rl" +// line 392 "Parser.rl" { ParserResult res = parseString(p, pe); if (res == null) { @@ -606,7 +617,7 @@ case 1: } break; case 7: -// line 391 "Parser.rl" +// line 402 "Parser.rl" { currentNesting++; ParserResult res = parseArray(p, pe); @@ -621,7 +632,7 @@ case 1: } break; case 8: -// line 403 "Parser.rl" +// line 414 "Parser.rl" { currentNesting++; ParserResult res = parseObject(p, pe); @@ -635,7 +646,7 @@ case 1: } } break; -// line 639 "Parser.java" +// line 650 "Parser.java" } } } @@ -655,7 +666,7 @@ case 5: break; } } -// line 438 "Parser.rl" +// line 449 "Parser.rl" if (cs >= JSON_value_first_final && result != null) { return new ParserResult(result, p); @@ -665,7 +676,7 @@ case 5: } -// line 669 "Parser.java" +// line 680 "Parser.java" private static byte[] init__JSON_integer_actions_0() { return new byte [] { @@ -764,22 +775,22 @@ static final int JSON_integer_error = 0; static final int JSON_integer_en_main = 1; -// line 457 "Parser.rl" +// line 468 "Parser.rl" ParserResult parseInteger(int p, int pe) { int cs = EVIL; -// line 775 "Parser.java" +// line 786 "Parser.java" { cs = JSON_integer_start; } -// line 463 "Parser.rl" +// line 474 "Parser.rl" int memo = p; -// line 783 "Parser.java" +// line 794 "Parser.java" { int _klen; int _trans = 0; @@ -860,13 +871,13 @@ case 1: switch ( _JSON_integer_actions[_acts++] ) { case 0: -// line 451 "Parser.rl" +// line 462 "Parser.rl" { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } break; -// line 870 "Parser.java" +// line 881 "Parser.java" } } } @@ -886,7 +897,7 @@ case 5: break; } } -// line 465 "Parser.rl" +// line 476 "Parser.rl" if (cs < JSON_integer_first_final) { return null; @@ -901,7 +912,7 @@ case 5: } -// line 905 "Parser.java" +// line 916 "Parser.java" private static byte[] init__JSON_float_actions_0() { return new byte [] { @@ -1003,22 +1014,22 @@ static final int JSON_float_error = 0; static final int JSON_float_en_main = 1; -// line 493 "Parser.rl" +// line 504 "Parser.rl" ParserResult parseFloat(int p, int pe) { int cs = EVIL; -// line 1014 "Parser.java" +// line 1025 "Parser.java" { cs = JSON_float_start; } -// line 499 "Parser.rl" +// line 510 "Parser.rl" int memo = p; -// line 1022 "Parser.java" +// line 1033 "Parser.java" { int _klen; int _trans = 0; @@ -1099,13 +1110,13 @@ case 1: switch ( _JSON_float_actions[_acts++] ) { case 0: -// line 484 "Parser.rl" +// line 495 "Parser.rl" { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } break; -// line 1109 "Parser.java" +// line 1120 "Parser.java" } } } @@ -1125,7 +1136,7 @@ case 5: break; } } -// line 501 "Parser.rl" +// line 512 "Parser.rl" if (cs < JSON_float_first_final) { return null; @@ -1140,7 +1151,7 @@ case 5: } -// line 1144 "Parser.java" +// line 1155 "Parser.java" private static byte[] init__JSON_string_actions_0() { return new byte [] { @@ -1242,7 +1253,7 @@ static final int JSON_string_error = 0; static final int JSON_string_en_main = 1; -// line 545 "Parser.rl" +// line 556 "Parser.rl" ParserResult parseString(int p, int pe) { @@ -1250,15 +1261,15 @@ static final int JSON_string_en_main = 1; IRubyObject result = null; -// line 1254 "Parser.java" +// line 1265 "Parser.java" { cs = JSON_string_start; } -// line 552 "Parser.rl" +// line 563 "Parser.rl" int memo = p; -// line 1262 "Parser.java" +// line 1273 "Parser.java" { int _klen; int _trans = 0; @@ -1339,7 +1350,7 @@ case 1: switch ( _JSON_string_actions[_acts++] ) { case 0: -// line 520 "Parser.rl" +// line 531 "Parser.rl" { int offset = byteList.begin(); ByteList decoded = decoder.decode(byteList, memo + 1 - offset, @@ -1354,13 +1365,13 @@ case 1: } break; case 1: -// line 533 "Parser.rl" +// line 544 "Parser.rl" { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } break; -// line 1364 "Parser.java" +// line 1375 "Parser.java" } } } @@ -1380,7 +1391,7 @@ case 5: break; } } -// line 554 "Parser.rl" +// line 565 "Parser.rl" if (parser.createAdditions) { RubyHash match_string = parser.match_string; @@ -1415,7 +1426,7 @@ case 5: } -// line 1419 "Parser.java" +// line 1430 "Parser.java" private static byte[] init__JSON_array_actions_0() { return new byte [] { @@ -1528,7 +1539,7 @@ static final int JSON_array_error = 0; static final int JSON_array_en_main = 1; -// line 620 "Parser.rl" +// line 635 "Parser.rl" ParserResult parseArray(int p, int pe) { @@ -1546,14 +1557,14 @@ static final int JSON_array_en_main = 1; IRubyObject.NULL_ARRAY, Block.NULL_BLOCK); -// line 1550 "Parser.java" +// line 1561 "Parser.java" { cs = JSON_array_start; } -// line 637 "Parser.rl" +// line 652 "Parser.rl" -// line 1557 "Parser.java" +// line 1568 "Parser.java" { int _klen; int _trans = 0; @@ -1634,26 +1645,30 @@ case 1: switch ( _JSON_array_actions[_acts++] ) { case 0: -// line 593 "Parser.rl" +// line 604 "Parser.rl" { ParserResult res = parseValue(p, pe); if (res == null) { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } else { - result.append(res.result); + if (!parser.arrayClass.getName().equals("Array")) { + result.callMethod(context, "<<", res.result); + } else { + result.append(res.result); + } {p = (( res.p))-1;} } } break; case 1: -// line 604 "Parser.rl" +// line 619 "Parser.rl" { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } break; -// line 1657 "Parser.java" +// line 1672 "Parser.java" } } } @@ -1673,7 +1688,7 @@ case 5: break; } } -// line 638 "Parser.rl" +// line 653 "Parser.rl" if (cs >= JSON_array_first_final) { return new ParserResult(result, p + 1); @@ -1683,7 +1698,7 @@ case 5: } -// line 1687 "Parser.java" +// line 1702 "Parser.java" private static byte[] init__JSON_object_actions_0() { return new byte [] { @@ -1806,7 +1821,7 @@ static final int JSON_object_error = 0; static final int JSON_object_en_main = 1; -// line 694 "Parser.rl" +// line 713 "Parser.rl" ParserResult parseObject(int p, int pe) { @@ -1825,14 +1840,14 @@ static final int JSON_object_en_main = 1; IRubyObject.NULL_ARRAY, Block.NULL_BLOCK); -// line 1829 "Parser.java" +// line 1844 "Parser.java" { cs = JSON_object_start; } -// line 712 "Parser.rl" +// line 731 "Parser.rl" -// line 1836 "Parser.java" +// line 1851 "Parser.java" { int _klen; int _trans = 0; @@ -1913,20 +1928,24 @@ case 1: switch ( _JSON_object_actions[_acts++] ) { case 0: -// line 652 "Parser.rl" +// line 667 "Parser.rl" { ParserResult res = parseValue(p, pe); if (res == null) { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } else { - result.op_aset(context, lastName, res.result); + if (!parser.objectClass.getName().equals("Hash")) { + result.callMethod(context, "[]=", new IRubyObject[] { lastName, res.result }); + } else { + result.op_aset(context, lastName, res.result); + } {p = (( res.p))-1;} } } break; case 1: -// line 663 "Parser.rl" +// line 682 "Parser.rl" { ParserResult res = parseString(p, pe); if (res == null) { @@ -1946,13 +1965,13 @@ case 1: } break; case 2: -// line 681 "Parser.rl" +// line 700 "Parser.rl" { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } break; -// line 1956 "Parser.java" +// line 1975 "Parser.java" } } } @@ -1972,7 +1991,7 @@ case 5: break; } } -// line 713 "Parser.rl" +// line 732 "Parser.rl" if (cs < JSON_object_first_final) { return null; @@ -1985,7 +2004,7 @@ case 5: IRubyObject vKlassName = result.op_aref(context, parser.createId); if (!vKlassName.isNil()) { // might throw ArgumentError, we let it propagate - IRubyObject klass = parser.info.jsonModule. + IRubyObject klass = parser.info.jsonModule.get(). callMethod(context, "deep_const_get", vKlassName); if (klass.respondsTo("json_creatable?") && klass.callMethod(context, "json_creatable?").isTrue()) { @@ -1998,7 +2017,7 @@ case 5: } -// line 2002 "Parser.java" +// line 2021 "Parser.java" private static byte[] init__JSON_actions_0() { return new byte [] { @@ -2102,7 +2121,7 @@ static final int JSON_error = 0; static final int JSON_en_main = 1; -// line 771 "Parser.rl" +// line 790 "Parser.rl" public IRubyObject parse() { @@ -2111,16 +2130,16 @@ static final int JSON_en_main = 1; IRubyObject result = null; -// line 2115 "Parser.java" +// line 2134 "Parser.java" { cs = JSON_start; } -// line 779 "Parser.rl" +// line 798 "Parser.rl" p = byteList.begin(); pe = p + byteList.length(); -// line 2124 "Parser.java" +// line 2143 "Parser.java" { int _klen; int _trans = 0; @@ -2201,7 +2220,7 @@ case 1: switch ( _JSON_actions[_acts++] ) { case 0: -// line 743 "Parser.rl" +// line 762 "Parser.rl" { currentNesting = 1; ParserResult res = parseObject(p, pe); @@ -2215,7 +2234,7 @@ case 1: } break; case 1: -// line 755 "Parser.rl" +// line 774 "Parser.rl" { currentNesting = 1; ParserResult res = parseArray(p, pe); @@ -2228,7 +2247,7 @@ case 1: } } break; -// line 2232 "Parser.java" +// line 2251 "Parser.java" } } } @@ -2248,7 +2267,7 @@ case 5: break; } } -// line 782 "Parser.rl" +// line 801 "Parser.rl" if (cs >= JSON_first_final && p == pe) { return result; @@ -2275,7 +2294,7 @@ case 5: * @param name The constant name */ private IRubyObject getConstant(String name) { - return parser.info.jsonModule.getConstant(name); + return parser.info.jsonModule.get().getConstant(name); } private RaiseException newException(String className, String message) { diff --git a/java/src/json/ext/Parser.rl b/java/src/json/ext/Parser.rl index e576b97..779d3f3 100644 --- a/java/src/json/ext/Parser.rl +++ b/java/src/json/ext/Parser.rl @@ -142,7 +142,10 @@ public class Parser extends RubyObject { @JRubyMethod(required = 1, optional = 1, visibility = Visibility.PRIVATE) public IRubyObject initialize(ThreadContext context, IRubyObject[] args) { - Ruby runtime = context.getRuntime(); + Ruby runtime = context.getRuntime(); + if (this.vSource != null) { + throw runtime.newTypeError("already initialized instance"); + } RubyString source = convertEncoding(context, args[0].convertToString()); OptionsReader opts = new OptionsReader(context, args.length > 1 ? args[1] : null); @@ -174,8 +177,8 @@ public class Parser extends RubyObject { if (info.encodingsSupported()) { RubyEncoding encoding = (RubyEncoding)source.encoding(context); - if (encoding != info.ascii8bit) { - return (RubyString)source.encode(context, info.utf8); + if (encoding != info.ascii8bit.get()) { + return (RubyString)source.encode(context, info.utf8.get()); } String sniffedEncoding = sniffByteList(bl); @@ -186,7 +189,7 @@ public class Parser extends RubyObject { String sniffedEncoding = sniffByteList(bl); if (sniffedEncoding == null) return source; // assume UTF-8 Ruby runtime = context.getRuntime(); - return (RubyString)info.jsonModule. + return (RubyString)info.jsonModule.get(). callMethod(context, "iconv", new IRubyObject[] { runtime.newString("utf-8"), @@ -216,7 +219,7 @@ public class Parser extends RubyObject { private RubyString reinterpretEncoding(ThreadContext context, RubyString str, String sniffedEncoding) { RubyEncoding actualEncoding = info.getEncoding(context, sniffedEncoding); - RubyEncoding targetEncoding = info.utf8; + RubyEncoding targetEncoding = info.utf8.get(); RubyString dup = (RubyString)str.dup(); dup.force_encoding(context, actualEncoding); return (RubyString)dup.encode_bang(context, targetEncoding); @@ -241,7 +244,15 @@ public class Parser extends RubyObject { */ @JRubyMethod(name = "source") public IRubyObject source_get() { - return vSource.dup(); + return checkAndGetSource().dup(); + } + + public RubyString checkAndGetSource() { + if (vSource != null) { + return vSource; + } else { + throw getRuntime().newTypeError("uninitialized instance"); + } } /** @@ -249,7 +260,7 @@ public class Parser extends RubyObject { * set to <code>nil</code> or <code>false</code>, and a String if not. */ private RubyString getCreateId(ThreadContext context) { - IRubyObject v = info.jsonModule.callMethod(context, "create_id"); + IRubyObject v = info.jsonModule.get().callMethod(context, "create_id"); return v.isTrue() ? v.convertToString() : null; } @@ -279,7 +290,7 @@ public class Parser extends RubyObject { private ParserSession(Parser parser, ThreadContext context) { this.parser = parser; this.context = context; - this.byteList = parser.vSource.getByteList(); + this.byteList = parser.checkAndGetSource().getByteList(); this.data = byteList.unsafeBytes(); this.decoder = new StringDecoder(context); } @@ -596,7 +607,11 @@ public class Parser extends RubyObject { fhold; fbreak; } else { - result.append(res.result); + if (!parser.arrayClass.getName().equals("Array")) { + result.callMethod(context, "<<", res.result); + } else { + result.append(res.result); + } fexec res.p; } } @@ -655,7 +670,11 @@ public class Parser extends RubyObject { fhold; fbreak; } else { - result.op_aset(context, lastName, res.result); + if (!parser.objectClass.getName().equals("Hash")) { + result.callMethod(context, "[]=", new IRubyObject[] { lastName, res.result }); + } else { + result.op_aset(context, lastName, res.result); + } fexec res.p; } } @@ -722,7 +741,7 @@ public class Parser extends RubyObject { IRubyObject vKlassName = result.op_aref(context, parser.createId); if (!vKlassName.isNil()) { // might throw ArgumentError, we let it propagate - IRubyObject klass = parser.info.jsonModule. + IRubyObject klass = parser.info.jsonModule.get(). callMethod(context, "deep_const_get", vKlassName); if (klass.respondsTo("json_creatable?") && klass.callMethod(context, "json_creatable?").isTrue()) { @@ -805,7 +824,7 @@ public class Parser extends RubyObject { * @param name The constant name */ private IRubyObject getConstant(String name) { - return parser.info.jsonModule.getConstant(name); + return parser.info.jsonModule.get().getConstant(name); } private RaiseException newException(String className, String message) { diff --git a/java/src/json/ext/ParserService.java b/java/src/json/ext/ParserService.java index e0805a7..f2ecae1 100644 --- a/java/src/json/ext/ParserService.java +++ b/java/src/json/ext/ParserService.java @@ -7,6 +7,7 @@ package json.ext; import java.io.IOException; +import java.lang.ref.WeakReference; import org.jruby.Ruby; import org.jruby.RubyClass; @@ -23,8 +24,8 @@ public class ParserService implements BasicLibraryService { runtime.getLoadService().require("json/common"); RuntimeInfo info = RuntimeInfo.initRuntime(runtime); - info.jsonModule = runtime.defineModule("JSON"); - RubyModule jsonExtModule = info.jsonModule.defineModuleUnder("Ext"); + info.jsonModule = new WeakReference<RubyModule>(runtime.defineModule("JSON")); + RubyModule jsonExtModule = info.jsonModule.get().defineModuleUnder("Ext"); RubyClass parserClass = jsonExtModule.defineClassUnder("Parser", runtime.getObject(), Parser.ALLOCATOR); diff --git a/java/src/json/ext/RuntimeInfo.java b/java/src/json/ext/RuntimeInfo.java index f446afe..5de5740 100644 --- a/java/src/json/ext/RuntimeInfo.java +++ b/java/src/json/ext/RuntimeInfo.java @@ -27,19 +27,21 @@ final class RuntimeInfo { private static Map<Ruby, RuntimeInfo> runtimes; // these fields are filled by the service loaders + // Use WeakReferences so that RuntimeInfo doesn't indirectly hold a hard reference to + // the Ruby runtime object, which would cause memory leaks in the runtimes map above. /** JSON */ - RubyModule jsonModule; + WeakReference<RubyModule> jsonModule; /** JSON::Ext::Generator::GeneratorMethods::String::Extend */ - RubyModule stringExtendModule; + WeakReference<RubyModule> stringExtendModule; /** JSON::Ext::Generator::State */ - RubyClass generatorStateClass; + WeakReference<RubyClass> generatorStateClass; /** JSON::SAFE_STATE_PROTOTYPE */ - GeneratorState safeStatePrototype; + WeakReference<GeneratorState> safeStatePrototype; - final RubyEncoding utf8; - final RubyEncoding ascii8bit; + final WeakReference<RubyEncoding> utf8; + final WeakReference<RubyEncoding> ascii8bit; // other encodings - private final Map<String, RubyEncoding> encodings; + private final Map<String, WeakReference<RubyEncoding>> encodings; private RuntimeInfo(Ruby runtime) { RubyClass encodingClass = runtime.getEncoding(); @@ -49,11 +51,11 @@ final class RuntimeInfo { } else { ThreadContext context = runtime.getCurrentContext(); - utf8 = (RubyEncoding)RubyEncoding.find(context, - encodingClass, runtime.newString("utf-8")); - ascii8bit = (RubyEncoding)RubyEncoding.find(context, - encodingClass, runtime.newString("ascii-8bit")); - encodings = new HashMap<String, RubyEncoding>(); + utf8 = new WeakReference<RubyEncoding>((RubyEncoding)RubyEncoding.find(context, + encodingClass, runtime.newString("utf-8"))); + ascii8bit = new WeakReference<RubyEncoding>((RubyEncoding)RubyEncoding.find(context, + encodingClass, runtime.newString("ascii-8bit"))); + encodings = new HashMap<String, WeakReference<RubyEncoding>>(); } } @@ -90,30 +92,30 @@ final class RuntimeInfo { } public boolean encodingsSupported() { - return utf8 != null; + return utf8 != null && utf8.get() != null; } public RubyEncoding getEncoding(ThreadContext context, String name) { synchronized (encodings) { - RubyEncoding encoding = encodings.get(name); + WeakReference<RubyEncoding> encoding = encodings.get(name); if (encoding == null) { Ruby runtime = context.getRuntime(); - encoding = (RubyEncoding)RubyEncoding.find(context, - runtime.getEncoding(), runtime.newString(name)); + encoding = new WeakReference<RubyEncoding>((RubyEncoding)RubyEncoding.find(context, + runtime.getEncoding(), runtime.newString(name))); encodings.put(name, encoding); } - return encoding; + return encoding.get(); } } public GeneratorState getSafeStatePrototype(ThreadContext context) { if (safeStatePrototype == null) { - IRubyObject value = jsonModule.getConstant("SAFE_STATE_PROTOTYPE"); + IRubyObject value = jsonModule.get().getConstant("SAFE_STATE_PROTOTYPE"); if (!(value instanceof GeneratorState)) { - throw context.getRuntime().newTypeError(value, generatorStateClass); + throw context.getRuntime().newTypeError(value, generatorStateClass.get()); } - safeStatePrototype = (GeneratorState)value; + safeStatePrototype = new WeakReference<GeneratorState>((GeneratorState)value); } - return safeStatePrototype; + return safeStatePrototype.get(); } } diff --git a/java/src/json/ext/Utils.java b/java/src/json/ext/Utils.java index 7a1dfee..f52ac8d 100644 --- a/java/src/json/ext/Utils.java +++ b/java/src/json/ext/Utils.java @@ -66,7 +66,7 @@ final class Utils { static RaiseException newException(ThreadContext context, String className, RubyString message) { RuntimeInfo info = RuntimeInfo.forRuntime(context.getRuntime()); - RubyClass klazz = info.jsonModule.getClass(className); + RubyClass klazz = info.jsonModule.get().getClass(className); RubyException excptn = (RubyException)klazz.newInstance(context, new IRubyObject[] {message}, Block.NULL_BLOCK); diff --git a/json-java.gemspec b/json-java.gemspec index ee04f04..35bfdf3 100644 --- a/json-java.gemspec +++ b/json-java.gemspec @@ -1,4 +1,4 @@ -#! /usr/bin/env jruby +#!/usr/bin/env jruby require "rubygems" spec = Gem::Specification.new do |s| @@ -17,5 +17,7 @@ end if $0 == __FILE__ Gem::Builder.new(spec).build +else + spec end spec diff --git a/json.gemspec b/json.gemspec index a155d6f..6220990 100644 --- a/json.gemspec +++ b/json.gemspec @@ -1,39 +1,41 @@ -#! /usr/bin/env jruby -require "rubygems" -require 'rake/gempackagetask' -require 'rbconfig' - -spec_ext = Gem::Specification.new do |s| - s.name = 'json' - s.version = File.read("VERSION").chomp - s.summary = 'JSON Implementation for Ruby' - s.description = "This is a JSON implementation as a Ruby extension in C." - - s.files = FileList["**/*"].exclude(/CVS|pkg|tmp|coverage|Makefile|\.nfs\.|\.iml\Z/).exclude(/\.(so|bundle|o|class|#{RbConfig::CONFIG['DLEXT']})$/) - - s.extensions = FileList['ext/**/extconf.rb'] - - s.require_path = 'ext/json/ext' - s.require_paths << 'ext' - s.require_paths << 'lib' - - s.bindir = "bin" - s.executables = [ "edit_json.rb", "prettify_json.rb" ] - s.default_executable = "edit_json.rb" - - s.has_rdoc = true - s.extra_rdoc_files << 'README' - s.rdoc_options << - '--title' << 'JSON implemention for Ruby' << '--main' << 'README' - s.test_files.concat Dir['./tests/test_*.rb'] - - s.author = "Florian Frank" - s.email = "flori@ping.de" - s.homepage = "http://flori.github.com/json" - s.rubyforge_project = "json" -end - -if $0 == __FILE__ - Gem::Builder.new(spec_ext).build +# -*- encoding: utf-8 -*- + +Gem::Specification.new do |s| + s.name = %q{json} + s.version = "1.5.4" + + s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= + s.authors = [%q{Florian Frank}] + s.date = %q{2011-07-08} + s.description = %q{This is a JSON implementation as a Ruby extension in C.} + s.email = %q{flori@ping.de} + s.executables = [%q{edit_json.rb}, %q{prettify_json.rb}] + s.extensions = [%q{ext/json/ext/parser/extconf.rb}, %q{ext/json/ext/generator/extconf.rb}] + s.extra_rdoc_files = [%q{README.rdoc}] + s.files = [%q{tests}, %q{tests/test_json_string_matching.rb}, %q{tests/test_json_fixtures.rb}, %q{tests/setup_variant.rb}, %q{tests/fixtures}, %q{tests/fixtures/fail6.json}, %q{tests/fixtures/fail9.json}, %q{tests/fixtures/fail10.json}, %q{tests/fixtures/fail24.json}, %q{tests/fixtures/fail28.json}, %q{tests/fixtures/fail13.json}, %q{tests/fixtures/fail4.json}, %q{tests/fixtures/pass3.json}, %q{tests/fixtures/fail11.json}, %q{tests/fixtures/fail14.json}, %q{tests/fixtures/fail3.json}, %q{tests/fixtures/fail12.json}, %q{tests/fixtures/pass16.json}, %q{tests/fixtures/pass15.json}, %q{tests/fixtures/fail20.json}, %q{tests/fixtures/fail8.json}, %q{tests/fixtures/pass2.json}, %q{tests/fixtures/fail5.json}, %q{tests/fixtures/fail1.json}, %q{tests/fixtures/fail25.json}, %q{tests/fixtures/pass17.json}, %q{tests/fixtures/fail7.json}, %q{tests/fixtures/pass26.json}, %q{tests/fixtures/fail21.json}, %q{tests/fixtures/pass1.json}, %q{tests/fixtures/fail23.json}, %q{tests/fixtures/fail18.json}, %q{tests/fixtures/fail2.json}, %q{tests/fixtures/fail22.json}, %q{tests/fixtures/fail27.json}, %q{tests/fixtures/fail19.json}, %q{tests/test_json_unicode.rb}, %q{tests/test_json_addition.rb}, %q{tests/test_json_generate.rb}, %q{tests/test_json_encoding.rb}, %q{tests/test_json.rb}, %q{COPYING}, %q{TODO}, %q{Rakefile}, %q{benchmarks}, %q{benchmarks/data-p4-3GHz-ruby18}, %q{benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser.dat}, %q{benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure.log}, %q{benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML.log}, %q{benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails.log}, %q{benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails.log}, %q{benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe.dat}, %q{benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser.dat}, %q{benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator.dat}, %q{benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure.log}, %q{benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty-autocorrelation.dat}, %q{benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser-autocorrelation.dat}, %q{benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser-autocorrelation.dat}, %q{benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser.dat}, %q{benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser-autocorrelation.dat}, %q{benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt.log}, %q{benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast.dat}, %q{benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe.dat}, %q{benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty.dat}, %q{benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkComparison.log}, %q{benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser.dat}, %q{benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt.log}, %q{benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe-autocorrelation.dat}, %q{benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator-autocorrelation.dat}, %q{benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast-autocorrelation.dat}, %q{benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty.dat}, %q{benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast-autocorrelation.dat}, %q{benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast.dat}, %q{benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkComparison.log}, %q{benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty-autocorrelation.dat}, %q{benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser-autocorrelation.dat}, %q{benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe-autocorrelation.dat}, %q{benchmarks/parser2_benchmark.rb}, %q{benchmarks/parser_benchmark.rb}, %q{benchmarks/generator2_benchmark.rb}, %q{benchmarks/generator_benchmark.rb}, %q{benchmarks/ohai.ruby}, %q{benchmarks/data}, %q{benchmarks/ohai.json}, %q{lib}, %q{lib/json}, %q{lib/json/json.xpm}, %q{lib/json/TrueClass.xpm}, %q{lib/json/version.rb}, %q{lib/json/Array.xpm}, %q{lib/json/add}, %q{lib/json/add/core.rb}, %q{lib/json/add/rails.rb}, %q{lib/json/common.rb}, %q{lib/json/pure}, %q{lib/json/pure/generator.rb}, %q{lib/json/pure/parser.rb}, %q{lib/json/ext.rb}, %q{lib/json/pure.rb}, %q{lib/json/Key.xpm}, %q{lib/json/FalseClass.xpm}, %q{lib/json/editor.rb}, %q{lib/json/Numeric.xpm}, %q{lib/json/ext}, %q{lib/json/NilClass.xpm}, %q{lib/json/String.xpm}, %q{lib/json/Hash.xpm}, %q{lib/json.rb}, %q{Gemfile}, %q{README.rdoc}, %q{json_pure.gemspec}, %q{GPL}, %q{CHANGES}, %q{bin}, %q{bin/prettify_json.rb}, %q{bin/edit_json.rb}, %q{COPYING-json-jruby}, %q{ext}, %q{ext/json}, %q{ext/json/ext}, %q{ext/json/ext/parser}, %q{ext/json/ext/parser/parser.h}, %q{ext/json/ext/parser/extconf.rb}, %q{ext/json/ext/parser/parser.rl}, %q{ext/json/ext/parser/mkmf.log}, %q{ext/json/ext/parser/parser.c}, %q{ext/json/ext/generator}, %q{ext/json/ext/generator/generator.c}, %q{ext/json/ext/generator/extconf.rb}, %q{ext/json/ext/generator/generator.h}, %q{VERSION}, %q{data}, %q{data/prototype.js}, %q{data/index.html}, %q{data/example.json}, %q{json.gemspec}, %q{java}, %q{java/src}, %q{java/src/json}, %q{java/src/json/ext}, %q{java/src/json/ext/Parser.java}, %q{java/src/json/ext/RuntimeInfo.java}, %q{java/src/json/ext/GeneratorState.java}, %q{java/src/json/ext/OptionsReader.java}, %q{java/src/json/ext/ParserService.java}, %q{java/src/json/ext/Parser.rl}, %q{java/src/json/ext/StringEncoder.java}, %q{java/src/json/ext/GeneratorService.java}, %q{java/src/json/ext/Utils.java}, %q{java/src/json/ext/StringDecoder.java}, %q{java/src/json/ext/Generator.java}, %q{java/src/json/ext/ByteListTranscoder.java}, %q{java/src/json/ext/GeneratorMethods.java}, %q{java/lib}, %q{java/lib/bytelist-1.0.6.jar}, %q{java/lib/jcodings.jar}, %q{diagrams}, %q{README-json-jruby.markdown}, %q{install.rb}, %q{json-java.gemspec}, %q{tools}, %q{tools/fuzz.rb}, %q{tools/server.rb}, %q{./tests/test_json_string_matching.rb}, %q{./tests/test_json_fixtures.rb}, %q{./tests/test_json_unicode.rb}, %q{./tests/test_json_addition.rb}, %q{./tests/test_json_generate.rb}, %q{./tests/test_json_encoding.rb}, %q{./tests/test_json.rb}] + s.homepage = %q{http://flori.github.com/json} + s.rdoc_options = [%q{--title}, %q{JSON implemention for Ruby}, %q{--main}, %q{README.rdoc}] + s.require_paths = [%q{ext/json/ext}, %q{ext}, %q{lib}] + s.rubyforge_project = %q{json} + s.rubygems_version = %q{1.8.5} + s.summary = %q{JSON Implementation for Ruby} + s.test_files = [%q{./tests/test_json_string_matching.rb}, %q{./tests/test_json_fixtures.rb}, %q{./tests/test_json_unicode.rb}, %q{./tests/test_json_addition.rb}, %q{./tests/test_json_generate.rb}, %q{./tests/test_json_encoding.rb}, %q{./tests/test_json.rb}] + + if s.respond_to? :specification_version then + s.specification_version = 4 + + if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then + s.add_development_dependency(%q<permutation>, [">= 0"]) + s.add_development_dependency(%q<bullshit>, [">= 0"]) + s.add_development_dependency(%q<sdoc>, [">= 0"]) + else + s.add_dependency(%q<permutation>, [">= 0"]) + s.add_dependency(%q<bullshit>, [">= 0"]) + s.add_dependency(%q<sdoc>, [">= 0"]) + end + else + s.add_dependency(%q<permutation>, [">= 0"]) + s.add_dependency(%q<bullshit>, [">= 0"]) + s.add_dependency(%q<sdoc>, [">= 0"]) + end end -spec_ext diff --git a/json_pure.gemspec b/json_pure.gemspec index a55d202..a3be22a 100644 --- a/json_pure.gemspec +++ b/json_pure.gemspec @@ -1,35 +1,46 @@ -#!/usr/bin/env ruby -require 'rubygems' -require 'rake/packagetask' -require 'rbconfig' +# -*- encoding: utf-8 -*- -spec_pure = Gem::Specification.new do |s| - s.name = 'json_pure' - s.version = File.read("VERSION").chomp - s.summary = 'JSON Implementation for Ruby' - s.description = "This is a JSON implementation in pure Ruby." +Gem::Specification.new do |s| + s.name = %q{json_pure} + s.version = "1.5.4" - s.files = FileList["**/*"].exclude(/CVS|pkg|tmp|coverage|Makefile|\.nfs\.|\.iml\Z/).exclude(/\.(so|bundle|o|class|#{RbConfig::CONFIG['DLEXT']})$/) + s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= + s.authors = [%q{Florian Frank}] + s.date = %q{2011-07-08} + s.description = %q{This is a JSON implementation in pure Ruby.} + s.email = %q{flori@ping.de} + s.executables = [%q{edit_json.rb}, %q{prettify_json.rb}] + s.extra_rdoc_files = [%q{README.rdoc}] + s.files = [%q{tests}, %q{tests/test_json_string_matching.rb}, %q{tests/test_json_fixtures.rb}, %q{tests/setup_variant.rb}, %q{tests/fixtures}, %q{tests/fixtures/fail6.json}, %q{tests/fixtures/fail9.json}, %q{tests/fixtures/fail10.json}, %q{tests/fixtures/fail24.json}, %q{tests/fixtures/fail28.json}, %q{tests/fixtures/fail13.json}, %q{tests/fixtures/fail4.json}, %q{tests/fixtures/pass3.json}, %q{tests/fixtures/fail11.json}, %q{tests/fixtures/fail14.json}, %q{tests/fixtures/fail3.json}, %q{tests/fixtures/fail12.json}, %q{tests/fixtures/pass16.json}, %q{tests/fixtures/pass15.json}, %q{tests/fixtures/fail20.json}, %q{tests/fixtures/fail8.json}, %q{tests/fixtures/pass2.json}, %q{tests/fixtures/fail5.json}, %q{tests/fixtures/fail1.json}, %q{tests/fixtures/fail25.json}, %q{tests/fixtures/pass17.json}, %q{tests/fixtures/fail7.json}, %q{tests/fixtures/pass26.json}, %q{tests/fixtures/fail21.json}, %q{tests/fixtures/pass1.json}, %q{tests/fixtures/fail23.json}, %q{tests/fixtures/fail18.json}, %q{tests/fixtures/fail2.json}, %q{tests/fixtures/fail22.json}, %q{tests/fixtures/fail27.json}, %q{tests/fixtures/fail19.json}, %q{tests/test_json_unicode.rb}, %q{tests/test_json_addition.rb}, %q{tests/test_json_generate.rb}, %q{tests/test_json_encoding.rb}, %q{tests/test_json.rb}, %q{COPYING}, %q{TODO}, %q{Rakefile}, %q{benchmarks}, %q{benchmarks/data-p4-3GHz-ruby18}, %q{benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser.dat}, %q{benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure.log}, %q{benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML.log}, %q{benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails.log}, %q{benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails.log}, %q{benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe.dat}, %q{benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser.dat}, %q{benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator.dat}, %q{benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure.log}, %q{benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty-autocorrelation.dat}, %q{benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser-autocorrelation.dat}, %q{benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser-autocorrelation.dat}, %q{benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser.dat}, %q{benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser-autocorrelation.dat}, %q{benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt.log}, %q{benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast.dat}, %q{benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe.dat}, %q{benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty.dat}, %q{benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkComparison.log}, %q{benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser.dat}, %q{benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt.log}, %q{benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe-autocorrelation.dat}, %q{benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator-autocorrelation.dat}, %q{benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast-autocorrelation.dat}, %q{benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty.dat}, %q{benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast-autocorrelation.dat}, %q{benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast.dat}, %q{benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkComparison.log}, %q{benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty-autocorrelation.dat}, %q{benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser-autocorrelation.dat}, %q{benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe-autocorrelation.dat}, %q{benchmarks/parser2_benchmark.rb}, %q{benchmarks/parser_benchmark.rb}, %q{benchmarks/generator2_benchmark.rb}, %q{benchmarks/generator_benchmark.rb}, %q{benchmarks/ohai.ruby}, %q{benchmarks/data}, %q{benchmarks/ohai.json}, %q{lib}, %q{lib/json}, %q{lib/json/json.xpm}, %q{lib/json/TrueClass.xpm}, %q{lib/json/version.rb}, %q{lib/json/Array.xpm}, %q{lib/json/add}, %q{lib/json/add/core.rb}, %q{lib/json/add/rails.rb}, %q{lib/json/common.rb}, %q{lib/json/pure}, %q{lib/json/pure/generator.rb}, %q{lib/json/pure/parser.rb}, %q{lib/json/ext.rb}, %q{lib/json/pure.rb}, %q{lib/json/Key.xpm}, %q{lib/json/FalseClass.xpm}, %q{lib/json/editor.rb}, %q{lib/json/Numeric.xpm}, %q{lib/json/ext}, %q{lib/json/NilClass.xpm}, %q{lib/json/String.xpm}, %q{lib/json/Hash.xpm}, %q{lib/json.rb}, %q{Gemfile}, %q{README.rdoc}, %q{json_pure.gemspec}, %q{GPL}, %q{CHANGES}, %q{bin}, %q{bin/prettify_json.rb}, %q{bin/edit_json.rb}, %q{COPYING-json-jruby}, %q{ext}, %q{ext/json}, %q{ext/json/ext}, %q{ext/json/ext/parser}, %q{ext/json/ext/parser/parser.h}, %q{ext/json/ext/parser/extconf.rb}, %q{ext/json/ext/parser/parser.rl}, %q{ext/json/ext/parser/mkmf.log}, %q{ext/json/ext/parser/parser.c}, %q{ext/json/ext/generator}, %q{ext/json/ext/generator/generator.c}, %q{ext/json/ext/generator/extconf.rb}, %q{ext/json/ext/generator/generator.h}, %q{VERSION}, %q{data}, %q{data/prototype.js}, %q{data/index.html}, %q{data/example.json}, %q{json.gemspec}, %q{java}, %q{java/src}, %q{java/src/json}, %q{java/src/json/ext}, %q{java/src/json/ext/Parser.java}, %q{java/src/json/ext/RuntimeInfo.java}, %q{java/src/json/ext/GeneratorState.java}, %q{java/src/json/ext/OptionsReader.java}, %q{java/src/json/ext/ParserService.java}, %q{java/src/json/ext/Parser.rl}, %q{java/src/json/ext/StringEncoder.java}, %q{java/src/json/ext/GeneratorService.java}, %q{java/src/json/ext/Utils.java}, %q{java/src/json/ext/StringDecoder.java}, %q{java/src/json/ext/Generator.java}, %q{java/src/json/ext/ByteListTranscoder.java}, %q{java/src/json/ext/GeneratorMethods.java}, %q{java/lib}, %q{java/lib/bytelist-1.0.6.jar}, %q{java/lib/jcodings.jar}, %q{diagrams}, %q{README-json-jruby.markdown}, %q{install.rb}, %q{json-java.gemspec}, %q{tools}, %q{tools/fuzz.rb}, %q{tools/server.rb}, %q{./tests/test_json_string_matching.rb}, %q{./tests/test_json_fixtures.rb}, %q{./tests/test_json_unicode.rb}, %q{./tests/test_json_addition.rb}, %q{./tests/test_json_generate.rb}, %q{./tests/test_json_encoding.rb}, %q{./tests/test_json.rb}] + s.homepage = %q{http://flori.github.com/json} + s.rdoc_options = [%q{--title}, %q{JSON implemention for ruby}, %q{--main}, %q{README.rdoc}] + s.require_paths = [%q{lib}] + s.rubyforge_project = %q{json} + s.rubygems_version = %q{1.8.5} + s.summary = %q{JSON Implementation for Ruby} + s.test_files = [%q{./tests/test_json_string_matching.rb}, %q{./tests/test_json_fixtures.rb}, %q{./tests/test_json_unicode.rb}, %q{./tests/test_json_addition.rb}, %q{./tests/test_json_generate.rb}, %q{./tests/test_json_encoding.rb}, %q{./tests/test_json.rb}] - s.require_path = 'lib' + if s.respond_to? :specification_version then + s.specification_version = 4 - s.bindir = "bin" - s.executables = [ "edit_json.rb", "prettify_json.rb" ] - s.default_executable = "edit_json.rb" - - s.has_rdoc = true - s.extra_rdoc_files << 'README' - s.rdoc_options << - '--title' << 'JSON implemention for ruby' << '--main' << 'README' - s.test_files.concat Dir['./tests/test_*.rb'] - - s.author = "Florian Frank" - s.email = "flori@ping.de" - s.homepage = "http://flori.github.com/json" - s.rubyforge_project = "json" -end - -if $0 == __FILE__ - Gem::Builder.new(spec_pure).build + if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then + s.add_development_dependency(%q<permutation>, [">= 0"]) + s.add_development_dependency(%q<bullshit>, [">= 0"]) + s.add_development_dependency(%q<sdoc>, [">= 0"]) + s.add_development_dependency(%q<rake>, ["~> 0.9.2"]) + s.add_runtime_dependency(%q<spruz>, ["~> 0.2.8"]) + else + s.add_dependency(%q<permutation>, [">= 0"]) + s.add_dependency(%q<bullshit>, [">= 0"]) + s.add_dependency(%q<sdoc>, [">= 0"]) + s.add_dependency(%q<rake>, ["~> 0.9.2"]) + s.add_dependency(%q<spruz>, ["~> 0.2.8"]) + end + else + s.add_dependency(%q<permutation>, [">= 0"]) + s.add_dependency(%q<bullshit>, [">= 0"]) + s.add_dependency(%q<sdoc>, [">= 0"]) + s.add_dependency(%q<rake>, ["~> 0.9.2"]) + s.add_dependency(%q<spruz>, ["~> 0.2.8"]) + end end -spec_pure diff --git a/lib/json.rb b/lib/json.rb index 789b0de..d7bc1a2 100644 --- a/lib/json.rb +++ b/lib/json.rb @@ -1,3 +1,55 @@ +## +# = JavaScript Object Notation (JSON) +# +# JSON is a lightweight data-interchange format. It is easy for us +# humans to read and write. Plus, equally simple for machines to generate or parse. +# JSON is completely language agnostic, making it the ideal interchange format. +# +# Built on two universally available structures: +# 1. A collection of name/value pairs. Often referred to as an _object_, hash table, record, struct, keyed list, or associative array. +# 2. An orderd list of values. More commonly named as an _array_, vector, sequence, or list. +# +# To read more about JSON visit: http://json.org +# +# == Parsing JSON +# +# To parse a JSON string received by another application, or generated within +# your existing application: +# +# require 'json' +# +# my_hash = JSON.parse('{"hello": "goodbye"}') +# puts my_hash["hello"] => "goodbye" +# +# Notice the extra quotes <tt>''</tt> around the hash notation. Ruby expects +# the argument to be a string and can't convert objects like a hash or array. +# +# Ruby converts your string into a hash +# +# == Generating JSON +# +# Creating a JSON string for communication or serialization is +# just as simple. +# +# require 'json' +# +# my_hash = {:hello => "goodbye"} +# puts JSON.generate(my_hash) => "{\"hello\":\"goodbye\"}" +# +# Or an alternative way: +# +# require 'json' +# puts {:hello => "goodbye"}.to_json => "{\"hello\":\"goodbye\"}" +# +# <tt>JSON.generate</tt> only allows objects or arrays to be converted +# to JSON syntax. While <tt>to_json</tt> accepts many Ruby classes +# even though it only acts a method for serialization: +# +# require 'json' +# +# 1.to_json => "1" +# + require 'json/common' module JSON require 'json/version' diff --git a/lib/json/add/core.rb b/lib/json/add/core.rb index 7a901d0..e9850af 100644 --- a/lib/json/add/core.rb +++ b/lib/json/add/core.rb @@ -6,20 +6,32 @@ unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED end require 'date' +# Symbol serialization/deserialization class Symbol - def to_json(*a) + # Returns a hash, that will be turned into a JSON object and represent this + # object. + def as_json(*) { JSON.create_id => self.class.name, - 's' => to_s, - }.to_json(*a) + 's' => to_s, + } end + # Stores class name (Symbol) with String representation of Symbol as a JSON string. + def to_json(*a) + as_json.to_json(*a) + end + + # Deserializes JSON string by converting the <tt>string</tt> value stored in the object to a Symbol def self.json_create(o) o['s'].to_sym end end +# Time serialization/deserialization class Time + + # Deserializes JSON string by converting time since epoch to Time def self.json_create(object) if usec = object.delete('u') # used to be tv_usec -> tv_nsec object['n'] = usec * 1000 @@ -31,34 +43,59 @@ class Time end end - def to_json(*args) + # Returns a hash, that will be turned into a JSON object and represent this + # object. + def as_json(*) { JSON.create_id => self.class.name, - 's' => tv_sec, - 'n' => respond_to?(:tv_nsec) ? tv_nsec : tv_usec * 1000 - }.to_json(*args) + 's' => tv_sec, + 'n' => respond_to?(:tv_nsec) ? tv_nsec : tv_usec * 1000 + } + end + + # Stores class name (Time) with number of seconds since epoch and number of + # microseconds for Time as JSON string + def to_json(*args) + as_json.to_json(*args) end end +# Date serialization/deserialization class Date + + # Deserializes JSON string by converting Julian year <tt>y</tt>, month + # <tt>m</tt>, day <tt>d</tt> and Day of Calendar Reform <tt>sg</tt> to Date. def self.json_create(object) civil(*object.values_at('y', 'm', 'd', 'sg')) end alias start sg unless method_defined?(:start) - def to_json(*args) + # Returns a hash, that will be turned into a JSON object and represent this + # object. + def as_json(*) { JSON.create_id => self.class.name, 'y' => year, 'm' => month, 'd' => day, 'sg' => start, - }.to_json(*args) + } + end + + # Stores class name (Date) with Julian year <tt>y</tt>, month <tt>m</tt>, day + # <tt>d</tt> and Day of Calendar Reform <tt>sg</tt> as JSON string + def to_json(*args) + as_json.to_json(*args) end end +# DateTime serialization/deserialization class DateTime + + # Deserializes JSON string by converting year <tt>y</tt>, month <tt>m</tt>, + # day <tt>d</tt>, hour <tt>H</tt>, minute <tt>M</tt>, second <tt>S</tt>, + # offset <tt>of</tt> and Day of Calendar Reform <tt>sg</tt> to DateTime. def self.json_create(object) args = object.values_at('y', 'm', 'd', 'H', 'M', 'S') of_a, of_b = object['of'].split('/') @@ -73,7 +110,9 @@ class DateTime alias start sg unless method_defined?(:start) - def to_json(*args) + # Returns a hash, that will be turned into a JSON object and represent this + # object. + def as_json(*) { JSON.create_id => self.class.name, 'y' => year, @@ -84,64 +123,121 @@ class DateTime 'S' => sec, 'of' => offset.to_s, 'sg' => start, - }.to_json(*args) + } + end + + # Stores class name (DateTime) with Julian year <tt>y</tt>, month <tt>m</tt>, + # day <tt>d</tt>, hour <tt>H</tt>, minute <tt>M</tt>, second <tt>S</tt>, + # offset <tt>of</tt> and Day of Calendar Reform <tt>sg</tt> as JSON string + def to_json(*args) + as_json.to_json(*args) end end +# Range serialization/deserialization class Range + + # Deserializes JSON string by constructing new Range object with arguments + # <tt>a</tt> serialized by <tt>to_json</tt>. def self.json_create(object) new(*object['a']) end - def to_json(*args) + # Returns a hash, that will be turned into a JSON object and represent this + # object. + def as_json(*) { - JSON.create_id => self.class.name, - 'a' => [ first, last, exclude_end? ] - }.to_json(*args) + JSON.create_id => self.class.name, + 'a' => [ first, last, exclude_end? ] + } + end + + # Stores class name (Range) with JSON array of arguments <tt>a</tt> which + # include <tt>first</tt> (integer), <tt>last</tt> (integer), and + # <tt>exclude_end?</tt> (boolean) as JSON string. + def to_json(*args) + as_json.to_json(*args) end end +# Struct serialization/deserialization class Struct + + # Deserializes JSON string by constructing new Struct object with values + # <tt>v</tt> serialized by <tt>to_json</tt>. def self.json_create(object) new(*object['v']) end - def to_json(*args) + # Returns a hash, that will be turned into a JSON object and represent this + # object. + def as_json(*) klass = self.class.name klass.to_s.empty? and raise JSON::JSONError, "Only named structs are supported!" { JSON.create_id => klass, - 'v' => values, - }.to_json(*args) + 'v' => values, + } + end + + # Stores class name (Struct) with Struct values <tt>v</tt> as a JSON string. + # Only named structs are supported. + def to_json(*args) + as_json.to_json(*args) end end +# Exception serialization/deserialization class Exception + + # Deserializes JSON string by constructing new Exception object with message + # <tt>m</tt> and backtrace <tt>b</tt> serialized with <tt>to_json</tt> def self.json_create(object) result = new(object['m']) result.set_backtrace object['b'] result end - def to_json(*args) + # Returns a hash, that will be turned into a JSON object and represent this + # object. + def as_json(*) { JSON.create_id => self.class.name, - 'm' => message, - 'b' => backtrace, - }.to_json(*args) + 'm' => message, + 'b' => backtrace, + } + end + + # Stores class name (Exception) with message <tt>m</tt> and backtrace array + # <tt>b</tt> as JSON string + def to_json(*args) + as_json.to_json(*args) end end +# Regexp serialization/deserialization class Regexp + + # Deserializes JSON string by constructing new Regexp object with source + # <tt>s</tt> (Regexp or String) and options <tt>o</tt> serialized by + # <tt>to_json</tt> def self.json_create(object) new(object['s'], object['o']) end - def to_json(*) + # Returns a hash, that will be turned into a JSON object and represent this + # object. + def as_json(*) { JSON.create_id => self.class.name, 'o' => options, 's' => source, - }.to_json + } + end + + # Stores class name (Regexp) with options <tt>o</tt> and source <tt>s</tt> + # (Regexp or String) as JSON string + def to_json(*) + as_json.to_json end end diff --git a/lib/json/common.rb b/lib/json/common.rb index f8ce2da..1a5f048 100644 --- a/lib/json/common.rb +++ b/lib/json/common.rb @@ -185,7 +185,7 @@ module JSON # * *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: ''), + # * *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: ''), # * *allow_nan*: true if NaN, Infinity, and -Infinity should be # generated, otherwise an exception is thrown, if these values are @@ -292,6 +292,7 @@ module JSON result end + # Recursively calls passed _Proc_ if the parsed data structure is an _Array_ or _Hash_ def recurse_proc(result, &proc) case result when Array @@ -351,13 +352,15 @@ module JSON # Shortuct for iconv. if ::String.method_defined?(:encode) + # Encodes string using Ruby's _String.encode_ def self.iconv(to, from, string) string.encode(to, from) end else require 'iconv' + # Encodes string using _iconv_ library def self.iconv(to, from, string) - Iconv.iconv(to, from, string).first + Iconv.conv(to, from, string) end end @@ -408,6 +411,7 @@ module ::Kernel end end +# Extends any Class to include _json_creatable?_ method. class ::Class # Returns true, if this class can be used to create an instance # from a serialised JSON string. The class has to implement a class diff --git a/lib/json/editor.rb b/lib/json/editor.rb index 3450455..985a554 100644 --- a/lib/json/editor.rb +++ b/lib/json/editor.rb @@ -47,14 +47,14 @@ module JSON # Opens an error dialog on top of _window_ showing the error message # _text_. def Editor.error_dialog(window, text) - dialog = MessageDialog.new(window, Dialog::MODAL, - MessageDialog::ERROR, + dialog = MessageDialog.new(window, Dialog::MODAL, + MessageDialog::ERROR, MessageDialog::BUTTONS_CLOSE, text) dialog.show_all dialog.run rescue TypeError - dialog = MessageDialog.new(Editor.window, Dialog::MODAL, - MessageDialog::ERROR, + dialog = MessageDialog.new(Editor.window, Dialog::MODAL, + MessageDialog::ERROR, MessageDialog::BUTTONS_CLOSE, text) dialog.show_all dialog.run @@ -66,8 +66,8 @@ module JSON # message _text_. If yes was answered _true_ is returned, otherwise # _false_. def Editor.question_dialog(window, text) - dialog = MessageDialog.new(window, Dialog::MODAL, - MessageDialog::QUESTION, + dialog = MessageDialog.new(window, Dialog::MODAL, + MessageDialog::QUESTION, MessageDialog::BUTTONS_YES_NO, text) dialog.show_all dialog.run do |response| @@ -464,7 +464,7 @@ module JSON add_separator add_item("Append new node", ?a, &method(:append_new_node)) add_item("Insert new node before", ?i, &method(:insert_new_node)) - add_separator + add_separator add_item("Collapse/Expand node (recursively)", ?e, &method(:collapse_expand)) @@ -503,7 +503,7 @@ module JSON # Revert the current JSON document in the editor to the saved version. def revert(item) window.instance_eval do - @filename and file_open(@filename) + @filename and file_open(@filename) end end @@ -665,7 +665,7 @@ module JSON collapse_all else self.expanded = true - expand_all + expand_all end end @@ -884,7 +884,7 @@ module JSON dialog.signal_connect(:'key-press-event', &DEFAULT_DIALOG_KEY_PRESS_HANDLER) dialog.show_all self.focus = dialog - dialog.run do |response| + dialog.run do |response| if response == Dialog::RESPONSE_ACCEPT @key = key_input.text type = ALL_TYPES[@type = type_input.active] @@ -936,7 +936,7 @@ module JSON dialog.signal_connect(:'key-press-event', &DEFAULT_DIALOG_KEY_PRESS_HANDLER) dialog.show_all self.focus = dialog - dialog.run do |response| + dialog.run do |response| if response == Dialog::RESPONSE_ACCEPT type = types[type_input.active] @content = case type @@ -981,7 +981,7 @@ module JSON dialog.signal_connect(:'key-press-event', &DEFAULT_DIALOG_KEY_PRESS_HANDLER) dialog.show_all self.focus = dialog - dialog.run do |response| + dialog.run do |response| if response == Dialog::RESPONSE_ACCEPT return @order = order_input.text, reverse_checkbox.active? end @@ -1016,7 +1016,7 @@ module JSON dialog.signal_connect(:'key-press-event', &DEFAULT_DIALOG_KEY_PRESS_HANDLER) dialog.show_all self.focus = dialog - dialog.run do |response| + dialog.run do |response| if response == Dialog::RESPONSE_ACCEPT begin return Regexp.new(regex_input.text, icase_checkbox.active? ? Regexp::IGNORECASE : 0) @@ -1215,7 +1215,7 @@ module JSON end end - # Save the current file as the filename + # Save the current file as the filename def file_save_as filename = select_file('Save as a JSON file') store_file(filename) @@ -1241,7 +1241,7 @@ module JSON rescue SystemCallError => e Editor.error_dialog(self, "Failed to store JSON file: #{e}!") end - + # Load the file named _filename_ into the editor as a JSON document. def load_file(filename) if filename @@ -1333,7 +1333,7 @@ module JSON dialog.signal_connect(:'key-press-event', &DEFAULT_DIALOG_KEY_PRESS_HANDLER) dialog.show_all - dialog.run do |response| + dialog.run do |response| if response == Dialog::RESPONSE_ACCEPT return @location = location_input.text end diff --git a/lib/json/pure/generator.rb b/lib/json/pure/generator.rb index 44cca60..3687e30 100644 --- a/lib/json/pure/generator.rb +++ b/lib/json/pure/generator.rb @@ -99,7 +99,7 @@ module JSON module Pure module Generator # This class is used to create State instances, that are use to hold data - # while generating a JSON text from a a Ruby data structure. + # while generating a JSON text from a Ruby data structure. class State # Creates a State object from _opts_, which ought to be Hash to create # a new State instance configured by _opts_, something else to create @@ -125,7 +125,7 @@ module JSON # * *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: ''), + # * *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*: is deprecated now, use the :max_nesting option instead, # * *max_nesting*: sets the maximum level of data structure nesting in @@ -212,6 +212,7 @@ module JSON end self end + alias merge configure # Returns the configuration instance variables as a hash, that can be # passed to the configure method. diff --git a/lib/json/pure/parser.rb b/lib/json/pure/parser.rb index 8043e67..d612018 100644 --- a/lib/json/pure/parser.rb +++ b/lib/json/pure/parser.rb @@ -41,7 +41,7 @@ module JSON [^*/]| # normal chars /[^*]| # slashes that do not start a nested comment \*[^/]| # asterisks that do not end this comment - /(?=\*/) # single slash before this comment's end + /(?=\*/) # single slash before this comment's end )* \*/ # the End of this comment |[ \t\r\n]+ # whitespaces: space, horicontal tab, lf, cr @@ -162,12 +162,12 @@ module JSON ?n => "\n", ?r => "\r", ?t => "\t", - ?u => nil, + ?u => nil, }) EMPTY_8BIT_STRING = '' if ::String.method_defined?(:encode) - EMPTY_8BIT_STRING.force_encoding Encoding::ASCII_8BIT + EMPTY_8BIT_STRING.force_encoding Encoding::ASCII_8BIT end def parse_string diff --git a/lib/json/version.rb b/lib/json/version.rb index 86a741a..2175ac0 100644 --- a/lib/json/version.rb +++ b/lib/json/version.rb @@ -1,6 +1,6 @@ module JSON # JSON version - VERSION = '1.5.1' + VERSION = '1.5.4' VERSION_ARRAY = VERSION.split(/\./).map { |x| x.to_i } # :nodoc: VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc: VERSION_MINOR = VERSION_ARRAY[1] # :nodoc: diff --git a/tests/test_json.rb b/tests/test_json.rb index 2fc3c09..b367e90 100755 --- a/tests/test_json.rb +++ b/tests/test_json.rb @@ -150,11 +150,20 @@ class TC_JSON < Test::Unit::TestCase assert_equal(@ary, parse('[[1],["foo"],[3.14],[47.11e+2],[2718.0E-3],[null],[[1,-2,3]]'\ ',[false],[true]]')) - assert_equal(@ary, parse(%Q{ [ [1] , ["foo"] , [3.14] \t , [47.11e+2] + assert_equal(@ary, parse(%Q{ [ [1] , ["foo"] , [3.14] \t , [47.11e+2]\s , [2718.0E-3 ],\r[ null] , [[1, -2, 3 ]], [false ],[ true]\n ] })) end - class SubArray < Array; end + class SubArray < Array + def <<(v) + @shifted = true + super + end + + def shifted? + @shifted + end + end class SubArray2 < Array def to_json(*a) @@ -171,9 +180,10 @@ class TC_JSON < Test::Unit::TestCase end def test_parse_array_custom_class - res = parse('[]', :array_class => SubArray) - assert_equal([], res) + res = parse('[1,2]', :array_class => SubArray) + assert_equal([1,2], res) assert_equal(SubArray, res.class) + assert res.shifted? end def test_parse_object @@ -184,6 +194,14 @@ class TC_JSON < Test::Unit::TestCase end class SubHash < Hash + def []=(k, v) + @item_set = true + super + end + + def item_set? + @item_set + end end class SubHash2 < Hash @@ -200,9 +218,10 @@ class TC_JSON < Test::Unit::TestCase end def test_parse_object_custom_class - res = parse('{}', :object_class => SubHash2) - assert_equal({}, res) - assert_equal(SubHash2, res.class) + res = parse('{"foo":"bar"}', :object_class => SubHash) + assert_equal({"foo" => "bar"}, res) + assert_equal(SubHash, res.class) + assert res.item_set? end def test_generation_of_core_subclasses_with_new_to_json @@ -387,4 +406,19 @@ EOT json5 = JSON([orig = 1 << 64]) assert_equal orig, JSON[json5][0] end + + if defined?(JSON::Ext::Parser) + def test_allocate + parser = JSON::Ext::Parser.new("{}") + assert_raise(TypeError, '[ruby-core:35079]') {parser.__send__(:initialize, "{}")} + parser = JSON::Ext::Parser.allocate + assert_raise(TypeError, '[ruby-core:35079]') {parser.source} + end + end + + def test_argument_encoding + source = "{}".force_encoding("ascii-8bit") + JSON::Parser.new(source) + assert_equal Encoding::ASCII_8BIT, source.encoding + end if defined?(Encoding::ASCII_8BIT) end diff --git a/tests/test_json_addition.rb b/tests/test_json_addition.rb index c8bfb41..a8181e8 100755 --- a/tests/test_json_addition.rb +++ b/tests/test_json_addition.rb @@ -19,7 +19,7 @@ class TC_JSONAddition < Test::Unit::TestCase def ==(other) a == other.a end - + def self.json_create(object) new(*object['args']) end diff --git a/tests/test_json_generate.rb b/tests/test_json_generate.rb index e6219df..9b0cff4 100755 --- a/tests/test_json_generate.rb +++ b/tests/test_json_generate.rb @@ -54,6 +54,7 @@ EOT def test_generate_pretty json = pretty_generate(@hash) + # hashes aren't (insertion) ordered on every ruby implementation assert_equal(@json3, json) assert_equal(JSON.parse(@json3), JSON.parse(json)) parsed_json = parse(json) assert_equal(@hash, parsed_json) diff --git a/tools/fuzz.rb b/tools/fuzz.rb index 4dacd95..c0fae12 100755 --- a/tools/fuzz.rb +++ b/tools/fuzz.rb @@ -120,7 +120,7 @@ loop do if $DEBUG puts "-" * 80 puts json, json.size - else + else puts json.size end begin |