Skip to content

Commit

Permalink
Use JSON.pretty_generate
Browse files Browse the repository at this point in the history
  • Loading branch information
buty4649 committed Jun 12, 2023
1 parent 04c51c2 commit ba49321
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mrblib/rf/00filter/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def decorate(val)
if raw? && val.instance_of?(String)
val
else
JSON.pretty_generate(val)
val.to_json
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions mrblib/rf/features.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,11 @@ def add_features_to_hash
key?(sym.to_s) || super
end
end

def add_features_to_json
Object.define_method(:to_json) do
JSON.pretty_generate(self)
end
end
end
end
1 change: 1 addition & 0 deletions mrblib/rf/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def add_features
Rf.add_features_to_integer
Rf.add_features_to_float
Rf.add_features_to_hash
Rf.add_features_to_json
end

def do_action
Expand Down
10 changes: 10 additions & 0 deletions spec/feature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,14 @@
it { expect(last_command_started).to have_output output_string_eq output }
end
end

describe 'to_json' do
let(:input) { load_fixture('json/hash.json') }
let(:output) do
before { run_rf("-j -q 'puts _.to_json'", input) }

it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
end
end
end

0 comments on commit ba49321

Please sign in to comment.