Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Generator::State#_generate #727

Merged
merged 1 commit into from
Jan 13, 2025
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
15 changes: 13 additions & 2 deletions ext/json/ext/generator/generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -1068,8 +1068,19 @@ static VALUE cState_partial_generate(VALUE self, VALUE obj, generator_func func,
return fbuffer_finalize(&buffer);
}

static VALUE cState_generate(VALUE self, VALUE obj, VALUE io)
/* call-seq:
* generate(obj) -> String
* generate(obj, anIO) -> anIO
*
* Generates a valid JSON document from object +obj+ and returns the
* result. If no valid JSON document can be created this method raises a
* GeneratorError exception.
*/
static VALUE cState_generate(int argc, VALUE *argv, VALUE self)
{
rb_check_arity(argc, 1, 2);
VALUE obj = argv[0];
VALUE io = argc > 1 ? argv[1] : Qnil;
VALUE result = cState_partial_generate(self, obj, generate_json, io);
GET_STATE(self);
(void)state;
Expand Down Expand Up @@ -1582,7 +1593,7 @@ void Init_generator(void)
rb_define_method(cState, "depth=", cState_depth_set, 1);
rb_define_method(cState, "buffer_initial_length", cState_buffer_initial_length, 0);
rb_define_method(cState, "buffer_initial_length=", cState_buffer_initial_length_set, 1);
rb_define_private_method(cState, "_generate", cState_generate, 2);
rb_define_method(cState, "generate", cState_generate, -1);

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

Expand Down
11 changes: 8 additions & 3 deletions java/src/json/ext/GeneratorState.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public static IRubyObject from_state(ThreadContext context, IRubyObject klass, I

@JRubyMethod(meta=true)
public static IRubyObject generate(ThreadContext context, IRubyObject klass, IRubyObject obj, IRubyObject opts, IRubyObject io) {
return fromState(context, opts)._generate(context, obj, io);
return fromState(context, opts).generate(context, obj, io);
}

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

@JRubyMethod
public IRubyObject generate(ThreadContext context, IRubyObject obj) {
return generate(context, obj, context.nil);
}

@JRubyMethod(name="[]")
public IRubyObject op_aref(ThreadContext context, IRubyObject vName) {
String name = vName.asJavaString();
Expand Down
6 changes: 1 addition & 5 deletions lib/json/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -818,11 +818,7 @@ def dump(obj, anIO = nil, limit = nil, kwargs = nil)
opts = merge_dump_options(opts, **kwargs) if kwargs

begin
if State === opts
opts.generate(obj, anIO)
else
State.generate(obj, opts, anIO)
end
State.generate(obj, opts, anIO)
rescue JSON::NestingError
raise ArgumentError, "exceed depth limit"
end
Expand Down
11 changes: 0 additions & 11 deletions lib/json/ext/generator/state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,6 @@ def configure(opts)

alias_method :merge, :configure

# call-seq:
# generate(obj) -> String
# generate(obj, anIO) -> anIO
#
# Generates a valid JSON document from object +obj+ and returns the
# result. If no valid JSON document can be created this method raises a
# GeneratorError exception.
def generate(obj, io = nil)
_generate(obj, io)
end

# call-seq: to_h
#
# Returns the configuration instance variables as a hash, that can be
Expand Down
17 changes: 8 additions & 9 deletions lib/json/truffle_ruby/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,7 @@ def valid_utf8?(string)
# while generating a JSON text from a Ruby data structure.
class State
def self.generate(obj, opts = nil, io = nil)
string = new(opts).generate(obj)
if io
io.write(string)
io
else
string
end
new(opts).generate(obj, io)
end

# Creates a State object from _opts_, which ought to be Hash to create
Expand Down Expand Up @@ -299,7 +293,7 @@ def to_h
# returns the result. If no valid JSON document can be
# created this method raises a
# GeneratorError exception.
def generate(obj)
def generate(obj, anIO = nil)
if @indent.empty? and @space.empty? and @space_before.empty? and @object_nl.empty? and @array_nl.empty? and
!@ascii_only and !@script_safe and @max_nesting == 0 and !@strict
result = generate_json(obj, ''.dup)
Expand All @@ -310,7 +304,12 @@ def generate(obj)
"source sequence #{result.inspect} is illegal/malformed utf-8",
obj
)
result
if anIO
anIO.write(result)
anIO
else
result
end
end

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