From 981b15da8eb68306ec642790a43c24883062df23 Mon Sep 17 00:00:00 2001 From: Daniel Schadd Date: Mon, 24 Apr 2023 15:54:04 -0500 Subject: [PATCH 1/8] add json parser --- lib/decanter/parser/json_parser.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 lib/decanter/parser/json_parser.rb diff --git a/lib/decanter/parser/json_parser.rb b/lib/decanter/parser/json_parser.rb new file mode 100644 index 0000000..9ff0003 --- /dev/null +++ b/lib/decanter/parser/json_parser.rb @@ -0,0 +1,14 @@ +module Decanter + module Parser + class JsonParser < ValueParser + + allow String + + parser do |val, options| + raise Decanter::ParseError.new 'Expects a JSON string' if val.is_a? String + next if (val.nil? || val === '') + JSON.parse(val) + end + end + end +end From b95004029b5f920fd25d55cbca28f34168ee6e23 Mon Sep 17 00:00:00 2001 From: Daniel Schadd Date: Mon, 24 Apr 2023 17:33:56 -0500 Subject: [PATCH 2/8] include spec --- lib/decanter/parser/json_parser.rb | 2 +- spec/decanter/parser/json_parser_spec.rb | 26 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 spec/decanter/parser/json_parser_spec.rb diff --git a/lib/decanter/parser/json_parser.rb b/lib/decanter/parser/json_parser.rb index 9ff0003..4e6e6c3 100644 --- a/lib/decanter/parser/json_parser.rb +++ b/lib/decanter/parser/json_parser.rb @@ -5,7 +5,7 @@ class JsonParser < ValueParser allow String parser do |val, options| - raise Decanter::ParseError.new 'Expects a JSON string' if val.is_a? String + raise Decanter::ParseError.new 'Expects a single value' if val.is_a? Array next if (val.nil? || val === '') JSON.parse(val) end diff --git a/spec/decanter/parser/json_parser_spec.rb b/spec/decanter/parser/json_parser_spec.rb new file mode 100644 index 0000000..f09d62b --- /dev/null +++ b/spec/decanter/parser/json_parser_spec.rb @@ -0,0 +1,26 @@ +require 'spec_helper' + +describe 'JsonParser' do + + let(:name) { :foo } + + let(:parser) { Decanter::Parser::JsonParser } + + describe '#parse' do + it 'parses string value and returns a parsed JSON' do + expect(parser.parse(name, '{"key": "value"}')).to match({name => "{\"key\": \"value\"}"}) + end + + context 'with empty string' do + it 'returns nil' do + expect(parser.parse(name, '')).to match({name => nil}) + end + end + + context 'with nil' do + it 'returns nil' do + expect(parser.parse(name, nil)).to match({name => nil}) + end + end + end +end From b111d4fa3fd52340f59fbb7959f32d17f9b6ae2b Mon Sep 17 00:00:00 2001 From: Daniel Schadd Date: Tue, 25 Apr 2023 11:58:12 -0500 Subject: [PATCH 3/8] add spec and force json parse on everything --- lib/decanter/parser/json_parser.rb | 4 +--- spec/decanter/parser/json_parser_spec.rb | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/decanter/parser/json_parser.rb b/lib/decanter/parser/json_parser.rb index 4e6e6c3..51746ea 100644 --- a/lib/decanter/parser/json_parser.rb +++ b/lib/decanter/parser/json_parser.rb @@ -2,10 +2,8 @@ module Decanter module Parser class JsonParser < ValueParser - allow String - parser do |val, options| - raise Decanter::ParseError.new 'Expects a single value' if val.is_a? Array + raise Decanter::ParseError.new 'Expects a JSON string' if val.is_a?(Array) || val.is_a?(Hash) next if (val.nil? || val === '') JSON.parse(val) end diff --git a/spec/decanter/parser/json_parser_spec.rb b/spec/decanter/parser/json_parser_spec.rb index f09d62b..7857bcc 100644 --- a/spec/decanter/parser/json_parser_spec.rb +++ b/spec/decanter/parser/json_parser_spec.rb @@ -8,7 +8,7 @@ describe '#parse' do it 'parses string value and returns a parsed JSON' do - expect(parser.parse(name, '{"key": "value"}')).to match({name => "{\"key\": \"value\"}"}) + expect(parser.parse(name, '{"key": "value"}')).to match({name => {"key" => "value"}}) end context 'with empty string' do From 16006326f0d63a82d9ea23d481e65c7de0cb0f88 Mon Sep 17 00:00:00 2001 From: Daniel Schadd Date: Tue, 25 Apr 2023 12:03:09 -0500 Subject: [PATCH 4/8] change version --- lib/decanter/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/decanter/version.rb b/lib/decanter/version.rb index 98f9e75..764056d 100644 --- a/lib/decanter/version.rb +++ b/lib/decanter/version.rb @@ -1,3 +1,3 @@ module Decanter - VERSION = '4.0.1'.freeze + VERSION = '4.0.2'.freeze end From 2be0b967486f8d17477b018896d620e7cecadc00 Mon Sep 17 00:00:00 2001 From: Daniel Schadd Date: Tue, 25 Apr 2023 12:10:02 -0500 Subject: [PATCH 5/8] update version in gemfile --- Gemfile.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile.lock b/Gemfile.lock index 117da4e..3d29efb 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - decanter (4.0.1) + decanter (4.0.2) actionpack (>= 4.2.10) activesupport rails-html-sanitizer (>= 1.0.4) From e0e564fee43f06fa51d2e8585e9a04b0a971ed71 Mon Sep 17 00:00:00 2001 From: Daniel Schadd Date: Tue, 25 Apr 2023 12:11:55 -0500 Subject: [PATCH 6/8] fix version --- lib/decanter/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/decanter/version.rb b/lib/decanter/version.rb index 764056d..98f9e75 100644 --- a/lib/decanter/version.rb +++ b/lib/decanter/version.rb @@ -1,3 +1,3 @@ module Decanter - VERSION = '4.0.2'.freeze + VERSION = '4.0.1'.freeze end From 21d8eb4617259872374157ab533595b8be9e0fa2 Mon Sep 17 00:00:00 2001 From: Daniel Schadd Date: Tue, 25 Apr 2023 12:14:39 -0500 Subject: [PATCH 7/8] actually fix version --- Gemfile.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile.lock b/Gemfile.lock index 3d29efb..117da4e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - decanter (4.0.2) + decanter (4.0.1) actionpack (>= 4.2.10) activesupport rails-html-sanitizer (>= 1.0.4) From 11b9209b87ac431dec560621a4abd48ca9addc05 Mon Sep 17 00:00:00 2001 From: Daniel Schadd Date: Tue, 25 Apr 2023 12:20:24 -0500 Subject: [PATCH 8/8] update readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 6b3a042..950da00 100644 --- a/README.md +++ b/README.md @@ -128,6 +128,7 @@ Decanter comes with the following parsers out of the box: - `:phone` - `:string` - `:array` +- `:json` Note: these parsers are designed to operate on a single value, except for `:array`. This parser expects an array, and will use the `parse_each` option to call a given parser on each of its elements: