Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ They are still available on rubygems.org and can be installed with
* 0.8.2 to [v0.9.0][io-console-v0.9.0], [v0.9.1][io-console-v0.9.1], [v0.9.2][io-console-v0.9.2]
* ipaddr 1.2.9
* 1.2.8 to [v1.2.9][ipaddr-v1.2.9]
* json 3.0.0
* json 3.0.1
* 2.18.0 to [v2.18.1][json-v2.18.1], [v2.19.0][json-v2.19.0], [v2.19.1][json-v2.19.1], [v2.19.2][json-v2.19.2], [v2.19.3][json-v2.19.3], [v2.19.4][json-v2.19.4], [v2.19.5][json-v2.19.5], [v2.19.6][json-v2.19.6], [v2.19.7][json-v2.19.7], [v2.19.8][json-v2.19.8], [v2.19.9][json-v2.19.9], [v2.20.0][json-v2.20.0], [v2.21.0][json-v2.21.0], [v2.21.2][json-v2.21.2], [v3.0.0.rc1][json-v3.0.0.rc1], [v3.0.0][json-v3.0.0]
* net-protocol 0.3.0
* 0.2.2 to [v0.3.0][net-protocol-v0.3.0]
Expand Down
44 changes: 22 additions & 22 deletions doc/extension.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -2369,7 +2369,7 @@ To make a "Ractor-safe" C extension, we need to check the following points:

== Appendix G. Embedded TypedData

Here is an example of how to use +RUBY_TYPED_EMBEDDABLE+::
Here is an example of how to use +RUBY_TYPED_EMBEDDABLE+:

struct my_data {
struct timespec created_at;
Expand All @@ -2383,25 +2383,25 @@ Here is an example of how to use +RUBY_TYPED_EMBEDDABLE+::
struct my_data *data = (struct my_data *)ptr;

// Deliberately don't free `ptr` if it is embeddable.
// Only auxiliary memory need to be freed.
// Only auxiliary memory needs to be freed.
ruby_xfree(data->buffer);
}

static size_t
my_data_size(const void *ptr)
{
const struct my_data *data = (const struct my_data *)ptr;
// We don't need to account for `sizeof(struct my_struct)` because it is embedded inside the Ruby object.
// Only auxiliary memory need to be reported.
return data->buffer_capa;
const struct my_data *data = (const struct my_data *)ptr;
// We don't need to account for `sizeof(struct my_data)` because it is embedded inside the Ruby object.
// Only auxiliary memory needs to be reported.
return data->buffer_capa;
}

static const rb_data_type_t my_type = {
.wrap_struct_name = "my_type",
.function = {
.dfree = my_data_free,
.dsize = my_data_size,
}
},
.flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_EMBEDDABLE,
};

Expand All @@ -2411,41 +2411,41 @@ Here is an example of how to use +RUBY_TYPED_EMBEDDABLE+::
struct my_data *data;
VALUE obj = TypedData_Make_Struct(klass, struct my_data, &my_type, data);

// Is it fine to pass pointers into the embedded struct, for as long as
// the called function won't use it after the Ruby object have left the stack.
// It is fine to pass pointers into the embedded struct, for as long as
// the called function won't use it after the Ruby object has left the stack.
clock_gettime(CLOCK_REALTIME, &data->created_at);
data->buffer_capa = 1024;
data->buffer = ZALLOC_N(char, data->buffer_capa);

return obj
return obj;
}

static VALUE
my_data_m_parse(VALUE klass)
my_data_read(VALUE self)
{
struct my_data *data;
VALUE my_data_obj = my_data_alloc(klass);
TypedData_Get_Struct(obj, struct my_data, &my_type, data);
TypedData_Get_Struct(self, struct my_data, &my_type, data);

// `my_data_obj` was allocated from C, `RB_GC_GUARD` must be used to
// ensure the compiler will keep its reference on the stack.
RB_GC_GUARD(my_data_obj)
// `self` is received from `rb_define_method` so `RB_GC_GUARD` isn't necessary.
return rb_str_new(data->buffer, data->buffer_capa);
}

static VALUE
my_data_read(VALUE self)
my_data_m_parse(VALUE klass)
{
struct my_data *data;
TypedData_Get_Struct(obj, struct my_data, &my_type, data);
VALUE my_data_obj = my_data_alloc(klass);
VALUE result = my_data_read(my_data_obj);

// `self` is received from `rb_define_method` so `RB_GC_GUARD` isn't necessary.
return rb_str_new(data->buffer, data->buffer_capa)
// `my_data_obj` was allocated from C, `RB_GC_GUARD` must be used to
// ensure the compiler will keep its reference on the stack.
RB_GC_GUARD(my_data_obj);
return result;
}

void
Init_my_data(void)
{
VALUE cMyData = rb_define_class("MyData");
VALUE cMyData = rb_define_class("MyData", rb_cObject);
rb_define_method(cMyData, "read", my_data_read, 0);
rb_define_singleton_method(cMyData, "parse", my_data_m_parse, 0);
}
Expand Down
36 changes: 22 additions & 14 deletions ext/json/lib/json/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,8 @@ def parse!(source, **options)
# parse(File.read(path), **)
#
# See method #parse.
def load_file(filespec, ...)
parse(File.read(filespec, encoding: Encoding::UTF_8), ...)
def load_file(filespec, **options)
parse(File.read(filespec, encoding: Encoding::UTF_8), **options)
end

# :call-seq:
Expand All @@ -335,8 +335,8 @@ def load_file(filespec, ...)
# JSON.parse!(File.read(path), **)
#
# See method #parse!
def load_file!(filespec, ...)
parse!(File.read(filespec, encoding: Encoding::UTF_8), ...)
def load_file!(filespec, **options)
parse!(File.read(filespec, encoding: Encoding::UTF_8), **options)
end

# :call-seq:
Expand Down Expand Up @@ -726,16 +726,14 @@ def load(source, proc = nil, allow_blank: true, **options)
end

# :call-seq:
# JSON.dump(obj, io = nil, options = nil)
# JSON.dump(obj, io = nil, _deprecated_limit = nil, options = nil)
#
# Dumps +obj+ as a \JSON string, i.e. calls generate on the object and returns the result.
#
# The default options can be changed via method JSON.dump_default_options.
#
# - Argument +io+, if given, should respond to method +write+;
# the \JSON \String is written to +io+, and +io+ is returned.
# If +io+ is not given, the \JSON \String is returned.
#
# - Argument +_deprecated_limit+ is deprecated, pass the +:max_nesting+ option instead.
# ---
#
# When argument +io+ is not given, returns the \JSON \String generated from +obj+:
Expand All @@ -751,21 +749,31 @@ def load(source, proc = nil, allow_blank: true, **options)
# puts File.read(path)
# Output:
# {"foo":[0,1],"bar":{"baz":2,"bat":3},"bam":"bad"}
def dump(obj, anIO = nil, kwargs = nil)
def dump(obj, anIO = nil, _deprecated_limit = nil, kwargs = nil)
if kwargs.nil?
if anIO.is_a?(Hash)
kwargs = anIO
anIO = nil
if _deprecated_limit.nil?
if anIO.is_a?(Hash)
kwargs = anIO
anIO = nil
end
elsif _deprecated_limit.is_a?(Hash)
kwargs = _deprecated_limit
_deprecated_limit = nil
end
end

if anIO&.respond_to?(:to_io)
anIO = anIO.to_io
unless anIO.nil?
if anIO.respond_to?(:to_io)
anIO = anIO.to_io
elsif _deprecated_limit.nil? && !anIO.respond_to?(:write)
anIO, _deprecated_limit = nil, anIO
end
end

opts = {
allow_nan: true,
}
opts[:max_nesting] = _deprecated_limit if _deprecated_limit
opts.merge!(kwargs) if kwargs

State.generate(obj, opts, anIO)
Expand Down
2 changes: 1 addition & 1 deletion ext/json/lib/json/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module JSON
VERSION = '3.0.0'
VERSION = '3.0.1'
end
1 change: 0 additions & 1 deletion test/json/json_coder_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require_relative 'test_helper'
Expand Down
2 changes: 0 additions & 2 deletions test/json/json_common_interface_test.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

require_relative 'test_helper'
require 'stringio'
require 'tempfile'

class JSONCommonInterfaceTest < Test::Unit::TestCase
include JSON
Expand Down
1 change: 1 addition & 0 deletions test/json/json_ext_parser_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

require_relative 'test_helper'

class JSONExtParserTest < Test::Unit::TestCase
Expand Down
1 change: 1 addition & 0 deletions test/json/json_fixtures_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

require_relative 'test_helper'

class JSONFixturesTest < Test::Unit::TestCase
Expand Down
5 changes: 1 addition & 4 deletions test/json/json_float_parser_fallback_test.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# frozen_string_literal: true

require_relative 'test_helper'
begin
require 'bigdecimal'
rescue LoadError
end

class JSONFloatParserFallbackTest < Test::Unit::TestCase
include JSON
Expand Down
7 changes: 6 additions & 1 deletion test/json/json_generator_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require_relative 'test_helper'
Expand Down Expand Up @@ -86,6 +85,12 @@ def test_dump_strict
assert_equal '{"hello":"world"}', dump({ hello: :world }, strict: true)
end

def test_dump_deprecated_limit
io = StringIO.new
JSON.dump([1], io, 0)
assert_equal '[1]', io.string
end

def test_not_frozen
[
[[], '[]'],
Expand Down
1 change: 1 addition & 0 deletions test/json/json_minefield_parser_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

require_relative 'test_helper'

class JSONMinefieldParserTest < Test::Unit::TestCase
Expand Down
9 changes: 2 additions & 7 deletions test/json/json_parser_test.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
# frozen_string_literal: true

require_relative 'test_helper'
require 'stringio'
require 'tempfile'
begin
require 'bigdecimal'
rescue LoadError
end

class JSONParserTest < Test::Unit::TestCase
include JSON
Expand Down Expand Up @@ -776,7 +771,7 @@ def []=(k, v)
@attrs[k.to_sym] = v
end

def method_missing(name, ...)
def method_missing(name, *)
@attrs.fetch(name) do
super
end
Expand Down
1 change: 1 addition & 0 deletions test/json/resumable_parser_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

require_relative 'test_helper'

class JSONResumageParserTest < Test::Unit::TestCase
Expand Down
6 changes: 6 additions & 0 deletions test/json/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@

require 'json'
require 'test/unit'
require 'stringio'
require 'tempfile'
begin
require 'bigdecimal'
rescue LoadError
end

if ENV["JSON_COMPACT"]
if GC.respond_to?(:verify_compaction_references)
Expand Down
2 changes: 1 addition & 1 deletion test/net/http/test_https.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def test_session_reuse_but_expire
http.get("/")

socket = http.instance_variable_get(:@socket).io
assert_equal false, socket.session_reused?, "NOTE: OpenSSL library version is #{OpenSSL::OPENSSL_LIBRARY_VERSION}"
assert !socket.session_reused?, "NOTE: OpenSSL library version is #{OpenSSL::OPENSSL_LIBRARY_VERSION}"

http.finish
end
Expand Down