-
Notifications
You must be signed in to change notification settings - Fork 4
add json parser #140
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
add json parser #140
Changes from all commits
981b15d
b950040
b111d4f
1600632
2be0b96
e0e564f
21d8eb4
11b9209
22f51b0
4442067
7efe829
9961218
5e45710
ccc6873
95aa485
c6c23ab
678d787
f96d633
2178759
2a7e4f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| module Decanter | ||
| module Parser | ||
| class JsonParser < ValueParser | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this should inherit from See https://github.com/LaunchPadLab/decanter#custom-parser-base-classes
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, that doesn't account for the fact that it could be an array. NVM!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea it turns out that JSON is anything lol |
||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since |
||
| parser do |val, options| | ||
| next if val.nil? || val === '' | ||
| raise Decanter::ParseError.new 'Expects a JSON string' unless val.is_a?(String) | ||
| parse_json(val) | ||
| end | ||
|
|
||
| def self.parse_json(val) | ||
| begin | ||
| JSON.parse(val) | ||
| rescue | ||
| raise Decanter::ParseError.new 'Invalid JSON string' | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| module Decanter | ||
| VERSION = '5.0.0'.freeze | ||
| VERSION = '5.1.0'.freeze | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| require 'spec_helper' | ||
|
|
||
| describe 'JsonParser' do | ||
|
|
||
| let(:name) { :foo } | ||
|
|
||
| let(:parser) { Decanter::Parser::JsonParser } | ||
|
|
||
| describe '#parse' do | ||
|
|
||
| context 'with a valid JSON string' do | ||
| it 'parses the string value and returns a parsed JSON' do | ||
| expect(parser.parse(name, '{"key": "value"}')).to match({name => {"key" => "value"}}) | ||
| expect(parser.parse(name, '["hello", "goodbye"]')).to match({name => ["hello", "goodbye"]}) | ||
| end | ||
| 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 | ||
|
|
||
| context 'with a non-string value' do | ||
| it 'raises a Decanter::ParseError' do | ||
| expect { parser.parse(name, 1) }.to raise_error(Decanter::ParseError, 'Expects a JSON string') | ||
| expect { parser.parse(name, true) }.to raise_error(Decanter::ParseError, 'Expects a JSON string') | ||
| expect { parser.parse(name, {}) }.to raise_error(Decanter::ParseError, 'Expects a JSON string') | ||
| end | ||
| end | ||
|
|
||
| context 'with a string that is invalid JSON' do | ||
| json_parser_error = 'Invalid JSON string' | ||
| it 'raises a Decanter::ParseError' do | ||
| expect { parser.parse(name, 'invalid') }.to raise_error(Decanter::ParseError, json_parser_error) | ||
| expect { parser.parse(name, '{ name: "John Smith", age: 30 }') }.to raise_error(Decanter::ParseError, json_parser_error) | ||
| expect { parser.parse(name, '{\"bio\": \"Line1 \n Line2\"}') }.to raise_error(Decanter::ParseError, json_parser_error) | ||
| expect { parser.parse(name, '{ "name": "John Smith", "age": 30, }') }.to raise_error(Decanter::ParseError, json_parser_error) | ||
| end | ||
| end | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just formatting