Skip to content

Commit d3dc415

Browse files
committed
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 9e3efbf commit d3dc415

5 files changed

Lines changed: 37 additions & 189 deletions

File tree

ext/json/ext/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);

java/src/json/ext/GeneratorMethods.java

Lines changed: 0 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ static void populate(RuntimeInfo info, RubyModule module) {
4343
defineMethods(module, "Object", RbObject.class);
4444
defineMethods(module, "String", RbString.class);
4545
defineMethods(module, "TrueClass", RbTrue.class);
46-
47-
RubyModule stringExtend = module.defineModuleUnder("String").defineModuleUnder("Extend");
48-
stringExtend.defineAnnotatedMethods(StringExtend.class);
4946
}
5047

5148
/**
@@ -118,95 +115,6 @@ public static IRubyObject to_json(ThreadContext context, IRubyObject vSelf) {
118115
public static IRubyObject to_json(ThreadContext context, IRubyObject vSelf, IRubyObject arg0) {
119116
return Generator.generateJson(context, (RubyString)vSelf, Generator.STRING_HANDLER, arg0);
120117
}
121-
122-
/**
123-
* <code>{@link RubyString String}#to_json_raw(*)</code>
124-
*
125-
* <p>This method creates a JSON text from the result of a call to
126-
* {@link #to_json_raw_object} of this String.
127-
*/
128-
@JRubyMethod
129-
public static IRubyObject to_json_raw(ThreadContext context, IRubyObject vSelf) {
130-
RubyHash obj = toJsonRawObject(context, Utils.ensureString(vSelf));
131-
return Generator.generateJson(context, obj, Generator.HASH_HANDLER);
132-
}
133-
134-
@JRubyMethod
135-
public static IRubyObject to_json_raw(ThreadContext context, IRubyObject vSelf, IRubyObject arg0) {
136-
RubyHash obj = toJsonRawObject(context, Utils.ensureString(vSelf));
137-
return Generator.generateJson(context, obj, Generator.HASH_HANDLER, arg0);
138-
}
139-
140-
/**
141-
* <code>{@link RubyString String}#to_json_raw_object(*)</code>
142-
*
143-
* <p>This method creates a raw object Hash, that can be nested into
144-
* other data structures and will be unparsed as a raw string. This
145-
* method should be used if you want to convert raw strings to JSON
146-
* instead of UTF-8 strings, e.g. binary data.
147-
*/
148-
@JRubyMethod
149-
public static IRubyObject to_json_raw_object(ThreadContext context, IRubyObject vSelf) {
150-
return toJsonRawObject(context, Utils.ensureString(vSelf));
151-
}
152-
153-
private static RubyHash toJsonRawObject(ThreadContext context,
154-
RubyString self) {
155-
Ruby runtime = context.runtime;
156-
RubyHash result = RubyHash.newHash(runtime);
157-
158-
IRubyObject createId = RuntimeInfo.forRuntime(runtime)
159-
.jsonModule.get().callMethod(context, "create_id");
160-
result.op_aset(context, createId, self.getMetaClass().to_s());
161-
162-
ByteList bl = self.getByteList();
163-
byte[] uBytes = bl.unsafeBytes();
164-
RubyArray array = runtime.newArray(bl.length());
165-
for (int i = bl.begin(), t = bl.begin() + bl.length(); i < t; i++) {
166-
array.store(i, runtime.newFixnum(uBytes[i] & 0xff));
167-
}
168-
169-
result.op_aset(context, runtime.newString("raw"), array);
170-
return result;
171-
}
172-
173-
@JRubyMethod(module=true)
174-
public static IRubyObject included(ThreadContext context, IRubyObject extendModule, IRubyObject module) {
175-
RuntimeInfo info = RuntimeInfo.forRuntime(context.runtime);
176-
return module.callMethod(context, "extend", ((RubyModule) extendModule).getConstant("Extend"));
177-
}
178-
}
179-
180-
public static class StringExtend {
181-
/**
182-
* <code>{@link RubyString String}#json_create(o)</code>
183-
*
184-
* <p>Raw Strings are JSON Objects (the raw bytes are stored in an
185-
* array for the key "raw"). The Ruby String can be created by this
186-
* module method.
187-
*/
188-
@JRubyMethod
189-
public static IRubyObject json_create(ThreadContext context,
190-
IRubyObject vSelf, IRubyObject vHash) {
191-
Ruby runtime = context.runtime;
192-
RubyHash o = vHash.convertToHash();
193-
IRubyObject rawData = o.fastARef(runtime.newString("raw"));
194-
if (rawData == null) {
195-
throw runtime.newArgumentError("\"raw\" value not defined "
196-
+ "for encoded String");
197-
}
198-
RubyArray ary = Utils.ensureArray(rawData);
199-
byte[] bytes = new byte[ary.getLength()];
200-
for (int i = 0, t = ary.getLength(); i < t; i++) {
201-
IRubyObject element = ary.eltInternal(i);
202-
if (element instanceof RubyFixnum) {
203-
bytes[i] = (byte)RubyNumeric.fix2long(element);
204-
} else {
205-
throw runtime.newTypeError(element, runtime.getFixnum());
206-
}
207-
}
208-
return runtime.newString(new ByteList(bytes, false));
209-
}
210118
}
211119

212120
public static class RbTrue {

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'

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

lib/json/truffle_ruby/generator.rb

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -635,39 +635,6 @@ def to_json(state = nil, *args)
635635
rescue Encoding::UndefinedConversionError => error
636636
raise ::JSON::GeneratorError.new(error.message, self)
637637
end
638-
639-
# Module that holds the extending methods if, the String module is
640-
# included.
641-
module Extend
642-
# Raw Strings are JSON Objects (the raw bytes are stored in an
643-
# array for the key "raw"). The Ruby String can be created by this
644-
# module method.
645-
def json_create(o)
646-
o['raw'].pack('C*')
647-
end
648-
end
649-
650-
# Extends _modul_ with the String::Extend module.
651-
def self.included(modul)
652-
modul.extend Extend
653-
end
654-
655-
# This method creates a raw object hash, that can be nested into
656-
# other data structures and will be unparsed as a raw string. This
657-
# method should be used, if you want to convert raw strings to JSON
658-
# instead of UTF-8 strings, e. g. binary data.
659-
def to_json_raw_object
660-
{
661-
JSON.create_id => self.class.name,
662-
'raw' => self.unpack('C*'),
663-
}
664-
end
665-
666-
# This method creates a JSON text from the result of
667-
# a call to to_json_raw_object of this String.
668-
def to_json_raw(*args)
669-
to_json_raw_object.to_json(*args)
670-
end
671638
end
672639

673640
module TrueClass

0 commit comments

Comments
 (0)