From ba49321d59da841b3a270cf664734b5f4d47e2b2 Mon Sep 17 00:00:00 2001 From: buty4649 Date: Tue, 13 Jun 2023 01:10:23 +0900 Subject: [PATCH] Use JSON.pretty_generate --- mrblib/rf/00filter/json.rb | 2 +- mrblib/rf/features.rb | 6 ++++++ mrblib/rf/runner.rb | 1 + spec/feature_spec.rb | 10 ++++++++++ 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/mrblib/rf/00filter/json.rb b/mrblib/rf/00filter/json.rb index fc9d543..2de2791 100644 --- a/mrblib/rf/00filter/json.rb +++ b/mrblib/rf/00filter/json.rb @@ -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 diff --git a/mrblib/rf/features.rb b/mrblib/rf/features.rb index e2c13ca..423d42d 100644 --- a/mrblib/rf/features.rb +++ b/mrblib/rf/features.rb @@ -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 diff --git a/mrblib/rf/runner.rb b/mrblib/rf/runner.rb index e3c47ba..d3646b8 100644 --- a/mrblib/rf/runner.rb +++ b/mrblib/rf/runner.rb @@ -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 diff --git a/spec/feature_spec.rb b/spec/feature_spec.rb index eaff31c..8e8d533 100644 --- a/spec/feature_spec.rb +++ b/spec/feature_spec.rb @@ -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