Skip to content

Commit 1c6b36a

Browse files
byroothsbt
authored andcommitted
Only define String.json_create & al when json/add is required
All the `json/add` related methods for string were always defined unconditionally from the generators. It's preferable to only define them if `json/add` is actually used.
1 parent 65612db commit 1c6b36a

File tree

3 files changed

+37
-64
lines changed

3 files changed

+37
-64
lines changed

ext/json/generator/generator.c

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ typedef struct JSON_Generator_StateStruct {
3131
#define RB_UNLIKELY(cond) (cond)
3232
#endif
3333

34-
static VALUE mJSON, cState, cFragment, mString_Extend, eGeneratorError, eNestingError, Encoding_UTF_8;
34+
static VALUE mJSON, cState, cFragment, eGeneratorError, eNestingError, Encoding_UTF_8;
3535

3636
static ID i_to_s, i_to_json, i_new, i_pack, i_unpack, i_create_id, i_extend, i_encode;
3737
static VALUE sym_indent, sym_space, sym_space_before, sym_object_nl, sym_array_nl, sym_max_nesting, sym_allow_nan,
@@ -835,18 +835,6 @@ static VALUE mFloat_to_json(int argc, VALUE *argv, VALUE self)
835835
return cState_partial_generate(Vstate, self, generate_json_float, Qfalse);
836836
}
837837

838-
/*
839-
* call-seq: String.included(modul)
840-
*
841-
* Extends _modul_ with the String::Extend module.
842-
*/
843-
static VALUE mString_included_s(VALUE self, VALUE modul)
844-
{
845-
VALUE result = rb_funcall(modul, i_extend, 1, mString_Extend);
846-
rb_call_super(1, &modul);
847-
return result;
848-
}
849-
850838
/*
851839
* call-seq: to_json(*)
852840
*
@@ -861,51 +849,6 @@ static VALUE mString_to_json(int argc, VALUE *argv, VALUE self)
861849
return cState_partial_generate(Vstate, self, generate_json_string, Qfalse);
862850
}
863851

864-
/*
865-
* call-seq: to_json_raw_object()
866-
*
867-
* This method creates a raw object hash, that can be nested into
868-
* other data structures and will be generated as a raw string. This
869-
* method should be used, if you want to convert raw strings to JSON
870-
* instead of UTF-8 strings, e. g. binary data.
871-
*/
872-
static VALUE mString_to_json_raw_object(VALUE self)
873-
{
874-
VALUE ary;
875-
VALUE result = rb_hash_new();
876-
rb_hash_aset(result, rb_funcall(mJSON, i_create_id, 0), rb_class_name(rb_obj_class(self)));
877-
ary = rb_funcall(self, i_unpack, 1, rb_str_new2("C*"));
878-
rb_hash_aset(result, rb_utf8_str_new_lit("raw"), ary);
879-
return result;
880-
}
881-
882-
/*
883-
* call-seq: to_json_raw(*args)
884-
*
885-
* This method creates a JSON text from the result of a call to
886-
* to_json_raw_object of this String.
887-
*/
888-
static VALUE mString_to_json_raw(int argc, VALUE *argv, VALUE self)
889-
{
890-
VALUE obj = mString_to_json_raw_object(self);
891-
Check_Type(obj, T_HASH);
892-
return mHash_to_json(argc, argv, obj);
893-
}
894-
895-
/*
896-
* call-seq: json_create(o)
897-
*
898-
* Raw Strings are JSON Objects (the raw bytes are stored in an array for the
899-
* key "raw"). The Ruby String can be created by this module method.
900-
*/
901-
static VALUE mString_Extend_json_create(VALUE self, VALUE o)
902-
{
903-
VALUE ary;
904-
Check_Type(o, T_HASH);
905-
ary = rb_hash_aref(o, rb_str_new2("raw"));
906-
return rb_funcall(ary, i_pack, 1, rb_str_new2("C*"));
907-
}
908-
909852
/*
910853
* call-seq: to_json(*)
911854
*
@@ -2091,13 +2034,7 @@ void Init_generator(void)
20912034
rb_define_method(mFloat, "to_json", mFloat_to_json, -1);
20922035

20932036
VALUE mString = rb_define_module_under(mGeneratorMethods, "String");
2094-
rb_define_singleton_method(mString, "included", mString_included_s, 1);
20952037
rb_define_method(mString, "to_json", mString_to_json, -1);
2096-
rb_define_method(mString, "to_json_raw", mString_to_json_raw, -1);
2097-
rb_define_method(mString, "to_json_raw_object", mString_to_json_raw_object, 0);
2098-
2099-
mString_Extend = rb_define_module_under(mString, "Extend");
2100-
rb_define_method(mString_Extend, "json_create", mString_Extend_json_create, 1);
21012038

21022039
VALUE mTrueClass = rb_define_module_under(mGeneratorMethods, "TrueClass");
21032040
rb_define_method(mTrueClass, "to_json", mTrueClass_to_json, -1);

ext/json/lib/json/add/core.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
require 'json/add/exception'
88
require 'json/add/range'
99
require 'json/add/regexp'
10+
require 'json/add/string'
1011
require 'json/add/struct'
1112
require 'json/add/symbol'
1213
require 'json/add/time'

ext/json/lib/json/add/string.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# frozen_string_literal: true
2+
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
3+
require 'json'
4+
end
5+
6+
class String
7+
# call-seq: json_create(o)
8+
#
9+
# Raw Strings are JSON Objects (the raw bytes are stored in an array for the
10+
# key "raw"). The Ruby String can be created by this class method.
11+
def self.json_create(object)
12+
object["raw"].pack("C*")
13+
end
14+
15+
# call-seq: to_json_raw_object()
16+
#
17+
# This method creates a raw object hash, that can be nested into
18+
# other data structures and will be generated as a raw string. This
19+
# method should be used, if you want to convert raw strings to JSON
20+
# instead of UTF-8 strings, e. g. binary data.
21+
def to_json_raw_object
22+
{
23+
JSON.create_id => self.class.name,
24+
"raw" => unpack("C*"),
25+
}
26+
end
27+
28+
# call-seq: to_json_raw(*args)
29+
#
30+
# This method creates a JSON text from the result of a call to
31+
# to_json_raw_object of this String.
32+
def to_json_raw(...)
33+
to_json_raw_object.to_json(...)
34+
end
35+
end

0 commit comments

Comments
 (0)