[ruby/json] Remove Class#json_creatable?
monkey patch.
https://github.com/ruby/json/commit/1ca7efed1f
This commit is contained in:
parent
96ecac1e24
commit
756b75f242
Notes:
git
2025-03-28 03:45:13 +00:00
@ -951,14 +951,3 @@ module ::Kernel
|
|||||||
JSON.generate(object, args.first)
|
JSON.generate(object, args.first)
|
||||||
end
|
end
|
||||||
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
|
|
||||||
# method _json_create_ that expects a hash as first parameter. The hash
|
|
||||||
# should include the required data.
|
|
||||||
def json_creatable?
|
|
||||||
respond_to?(:json_create)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
@ -790,6 +790,15 @@ static inline VALUE json_decode_array(JSON_ParserState *state, JSON_ParserConfig
|
|||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool json_obj_creatable_p(VALUE klass)
|
||||||
|
{
|
||||||
|
if (rb_respond_to(klass, i_json_creatable_p)) {
|
||||||
|
return RTEST(rb_funcall(klass, i_json_creatable_p, 0));
|
||||||
|
} else {
|
||||||
|
return rb_respond_to(klass, i_json_create);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static inline VALUE json_decode_object(JSON_ParserState *state, JSON_ParserConfig *config, long count)
|
static inline VALUE json_decode_object(JSON_ParserState *state, JSON_ParserConfig *config, long count)
|
||||||
{
|
{
|
||||||
VALUE object;
|
VALUE object;
|
||||||
@ -818,7 +827,7 @@ static inline VALUE json_decode_object(JSON_ParserState *state, JSON_ParserConfi
|
|||||||
}
|
}
|
||||||
if (!NIL_P(klassname)) {
|
if (!NIL_P(klassname)) {
|
||||||
VALUE klass = rb_funcall(mJSON, i_deep_const_get, 1, klassname);
|
VALUE klass = rb_funcall(mJSON, i_deep_const_get, 1, klassname);
|
||||||
if (RTEST(rb_funcall(klass, i_json_creatable_p, 0))) {
|
if (json_obj_creatable_p(klass)) {
|
||||||
if (config->deprecated_create_additions) {
|
if (config->deprecated_create_additions) {
|
||||||
json_deprecated(deprecated_create_additions_warning);
|
json_deprecated(deprecated_create_additions_warning);
|
||||||
}
|
}
|
||||||
@ -837,7 +846,7 @@ static inline VALUE json_decode_object(JSON_ParserState *state, JSON_ParserConfi
|
|||||||
static int match_i(VALUE regexp, VALUE klass, VALUE memo)
|
static int match_i(VALUE regexp, VALUE klass, VALUE memo)
|
||||||
{
|
{
|
||||||
if (regexp == Qundef) return ST_STOP;
|
if (regexp == Qundef) return ST_STOP;
|
||||||
if (RTEST(rb_funcall(klass, i_json_creatable_p, 0)) &&
|
if (json_obj_creatable_p(klass) &&
|
||||||
RTEST(rb_funcall(regexp, i_match, 1, rb_ary_entry(memo, 0)))) {
|
RTEST(rb_funcall(regexp, i_match, 1, rb_ary_entry(memo, 0)))) {
|
||||||
rb_ary_push(memo, klass);
|
rb_ary_push(memo, klass);
|
||||||
return ST_STOP;
|
return ST_STOP;
|
||||||
|
@ -44,10 +44,6 @@ class JSONAdditionTest < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
class B
|
class B
|
||||||
def self.json_creatable?
|
|
||||||
false
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_json(*args)
|
def to_json(*args)
|
||||||
{
|
{
|
||||||
'json_class' => self.class.name,
|
'json_class' => self.class.name,
|
||||||
@ -56,10 +52,6 @@ class JSONAdditionTest < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
class C
|
class C
|
||||||
def self.json_creatable?
|
|
||||||
false
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_json(*args)
|
def to_json(*args)
|
||||||
{
|
{
|
||||||
'json_class' => 'JSONAdditionTest::Nix',
|
'json_class' => 'JSONAdditionTest::Nix',
|
||||||
@ -69,7 +61,6 @@ class JSONAdditionTest < Test::Unit::TestCase
|
|||||||
|
|
||||||
def test_extended_json
|
def test_extended_json
|
||||||
a = A.new(666)
|
a = A.new(666)
|
||||||
assert A.json_creatable?
|
|
||||||
json = generate(a)
|
json = generate(a)
|
||||||
a_again = parse(json, :create_additions => true)
|
a_again = parse(json, :create_additions => true)
|
||||||
assert_kind_of a.class, a_again
|
assert_kind_of a.class, a_again
|
||||||
@ -78,7 +69,7 @@ class JSONAdditionTest < Test::Unit::TestCase
|
|||||||
|
|
||||||
def test_extended_json_default
|
def test_extended_json_default
|
||||||
a = A.new(666)
|
a = A.new(666)
|
||||||
assert A.json_creatable?
|
assert A.respond_to?(:json_create)
|
||||||
json = generate(a)
|
json = generate(a)
|
||||||
a_hash = parse(json)
|
a_hash = parse(json)
|
||||||
assert_kind_of Hash, a_hash
|
assert_kind_of Hash, a_hash
|
||||||
@ -86,7 +77,6 @@ class JSONAdditionTest < Test::Unit::TestCase
|
|||||||
|
|
||||||
def test_extended_json_disabled
|
def test_extended_json_disabled
|
||||||
a = A.new(666)
|
a = A.new(666)
|
||||||
assert A.json_creatable?
|
|
||||||
json = generate(a)
|
json = generate(a)
|
||||||
a_again = parse(json, :create_additions => true)
|
a_again = parse(json, :create_additions => true)
|
||||||
assert_kind_of a.class, a_again
|
assert_kind_of a.class, a_again
|
||||||
@ -101,14 +91,12 @@ class JSONAdditionTest < Test::Unit::TestCase
|
|||||||
|
|
||||||
def test_extended_json_fail1
|
def test_extended_json_fail1
|
||||||
b = B.new
|
b = B.new
|
||||||
assert !B.json_creatable?
|
|
||||||
json = generate(b)
|
json = generate(b)
|
||||||
assert_equal({ "json_class"=>"JSONAdditionTest::B" }, parse(json))
|
assert_equal({ "json_class"=>"JSONAdditionTest::B" }, parse(json))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_extended_json_fail2
|
def test_extended_json_fail2
|
||||||
c = C.new
|
c = C.new
|
||||||
assert !C.json_creatable?
|
|
||||||
json = generate(c)
|
json = generate(c)
|
||||||
assert_raise(ArgumentError, NameError) { parse(json, :create_additions => true) }
|
assert_raise(ArgumentError, NameError) { parse(json, :create_additions => true) }
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user