Skip to content

Commit 8c352e3

Browse files
authored
Merge pull request #727 from etiennebarrie/remove-State-_generate
Remove Generator::State#_generate
2 parents 6843ccf + b2fc583 commit 8c352e3

File tree

5 files changed

+30
-30
lines changed

5 files changed

+30
-30
lines changed

ext/json/ext/generator/generator.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,8 +1068,19 @@ static VALUE cState_partial_generate(VALUE self, VALUE obj, generator_func func,
10681068
return fbuffer_finalize(&buffer);
10691069
}
10701070

1071-
static VALUE cState_generate(VALUE self, VALUE obj, VALUE io)
1071+
/* call-seq:
1072+
* generate(obj) -> String
1073+
* generate(obj, anIO) -> anIO
1074+
*
1075+
* Generates a valid JSON document from object +obj+ and returns the
1076+
* result. If no valid JSON document can be created this method raises a
1077+
* GeneratorError exception.
1078+
*/
1079+
static VALUE cState_generate(int argc, VALUE *argv, VALUE self)
10721080
{
1081+
rb_check_arity(argc, 1, 2);
1082+
VALUE obj = argv[0];
1083+
VALUE io = argc > 1 ? argv[1] : Qnil;
10731084
VALUE result = cState_partial_generate(self, obj, generate_json, io);
10741085
GET_STATE(self);
10751086
(void)state;
@@ -1582,7 +1593,7 @@ void Init_generator(void)
15821593
rb_define_method(cState, "depth=", cState_depth_set, 1);
15831594
rb_define_method(cState, "buffer_initial_length", cState_buffer_initial_length, 0);
15841595
rb_define_method(cState, "buffer_initial_length=", cState_buffer_initial_length_set, 1);
1585-
rb_define_private_method(cState, "_generate", cState_generate, 2);
1596+
rb_define_method(cState, "generate", cState_generate, -1);
15861597

15871598
rb_define_singleton_method(cState, "generate", cState_m_generate, 3);
15881599

java/src/json/ext/GeneratorState.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public static IRubyObject from_state(ThreadContext context, IRubyObject klass, I
134134

135135
@JRubyMethod(meta=true)
136136
public static IRubyObject generate(ThreadContext context, IRubyObject klass, IRubyObject obj, IRubyObject opts, IRubyObject io) {
137-
return fromState(context, opts)._generate(context, obj, io);
137+
return fromState(context, opts).generate(context, obj, io);
138138
}
139139

140140
static GeneratorState fromState(ThreadContext context, IRubyObject opts) {
@@ -227,8 +227,8 @@ public IRubyObject initialize_copy(ThreadContext context, IRubyObject vOrig) {
227227
* the result. If no valid JSON document can be created this method raises
228228
* a GeneratorError exception.
229229
*/
230-
@JRubyMethod(visibility = Visibility.PRIVATE)
231-
public IRubyObject _generate(ThreadContext context, IRubyObject obj, IRubyObject io) {
230+
@JRubyMethod
231+
public IRubyObject generate(ThreadContext context, IRubyObject obj, IRubyObject io) {
232232
IRubyObject result = Generator.generateJson(context, obj, this, io);
233233
RuntimeInfo info = RuntimeInfo.forRuntime(context.runtime);
234234
if (!(result instanceof RubyString)) {
@@ -247,6 +247,11 @@ public IRubyObject _generate(ThreadContext context, IRubyObject obj, IRubyObject
247247
return resultString;
248248
}
249249

250+
@JRubyMethod
251+
public IRubyObject generate(ThreadContext context, IRubyObject obj) {
252+
return generate(context, obj, context.nil);
253+
}
254+
250255
@JRubyMethod(name="[]")
251256
public IRubyObject op_aref(ThreadContext context, IRubyObject vName) {
252257
String name = vName.asJavaString();

lib/json/common.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -818,11 +818,7 @@ def dump(obj, anIO = nil, limit = nil, kwargs = nil)
818818
opts = merge_dump_options(opts, **kwargs) if kwargs
819819

820820
begin
821-
if State === opts
822-
opts.generate(obj, anIO)
823-
else
824-
State.generate(obj, opts, anIO)
825-
end
821+
State.generate(obj, opts, anIO)
826822
rescue JSON::NestingError
827823
raise ArgumentError, "exceed depth limit"
828824
end

lib/json/ext/generator/state.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,6 @@ def configure(opts)
4747

4848
alias_method :merge, :configure
4949

50-
# call-seq:
51-
# generate(obj) -> String
52-
# generate(obj, anIO) -> anIO
53-
#
54-
# Generates a valid JSON document from object +obj+ and returns the
55-
# result. If no valid JSON document can be created this method raises a
56-
# GeneratorError exception.
57-
def generate(obj, io = nil)
58-
_generate(obj, io)
59-
end
60-
6150
# call-seq: to_h
6251
#
6352
# Returns the configuration instance variables as a hash, that can be

lib/json/truffle_ruby/generator.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,7 @@ def valid_utf8?(string)
9797
# while generating a JSON text from a Ruby data structure.
9898
class State
9999
def self.generate(obj, opts = nil, io = nil)
100-
string = new(opts).generate(obj)
101-
if io
102-
io.write(string)
103-
io
104-
else
105-
string
106-
end
100+
new(opts).generate(obj, io)
107101
end
108102

109103
# Creates a State object from _opts_, which ought to be Hash to create
@@ -299,7 +293,7 @@ def to_h
299293
# returns the result. If no valid JSON document can be
300294
# created this method raises a
301295
# GeneratorError exception.
302-
def generate(obj)
296+
def generate(obj, anIO = nil)
303297
if @indent.empty? and @space.empty? and @space_before.empty? and @object_nl.empty? and @array_nl.empty? and
304298
!@ascii_only and !@script_safe and @max_nesting == 0 and !@strict
305299
result = generate_json(obj, ''.dup)
@@ -310,7 +304,12 @@ def generate(obj)
310304
"source sequence #{result.inspect} is illegal/malformed utf-8",
311305
obj
312306
)
313-
result
307+
if anIO
308+
anIO.write(result)
309+
anIO
310+
else
311+
result
312+
end
314313
end
315314

316315
# Handles @allow_nan, @buffer_initial_length, other ivars must be the default value (see above)

0 commit comments

Comments
 (0)