Skip to content

Commit

Permalink
Use multi_json instead of json gem
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Mar 1, 2013
1 parent 70c847d commit 66e13da
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 38 deletions.
4 changes: 2 additions & 2 deletions examples/parse_and_output_json.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'gherkin/parser/parser'
require 'gherkin/formatter/json_formatter'
require 'stringio'
require 'json'
require 'multi_json'

# This example reads a couple of features and outputs them as JSON.

Expand All @@ -16,4 +16,4 @@
end

formatter.done
puts JSON.pretty_generate(JSON.parse(io.string))
puts MultiJson.dump(MultiJson.load(io.string), :pretty => true)
8 changes: 4 additions & 4 deletions features/.cucumber/stepdefs.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,23 +136,23 @@
]
},
{
"file_colon_line": "features/step_definitions/json_parser_steps.rb:5",
"file_colon_line": "features/step_definitions/json_parser_steps.rb:6",
"flags": "",
"source": "^a PrettyFormatter$",
"steps": [

]
},
{
"file_colon_line": "features/step_definitions/json_parser_steps.rb:10",
"file_colon_line": "features/step_definitions/json_parser_steps.rb:11",
"flags": "",
"source": "^a JSON lexer$",
"steps": [

]
},
{
"file_colon_line": "features/step_definitions/json_parser_steps.rb:14",
"file_colon_line": "features/step_definitions/json_parser_steps.rb:15",
"flags": "",
"source": "^the following JSON is parsed:$",
"steps": [
Expand All @@ -165,7 +165,7 @@
]
},
{
"file_colon_line": "features/step_definitions/json_parser_steps.rb:18",
"file_colon_line": "features/step_definitions/json_parser_steps.rb:19",
"flags": "",
"source": "^the outputted text should be:$",
"steps": [
Expand Down
8 changes: 4 additions & 4 deletions features/step_definitions/json_formatter_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ def keys
end

Then /^the outputted JSON should be:$/ do |expected_json|
require 'json'
require 'multi_json'
@formatter.done
actual_json = @out.string
puts actual_json
puts JSON.pretty_generate(JSON.parse(actual_json))
expected = JSON.parse(expected_json)
actual = JSON.parse(actual_json)
puts MultiJson.dump(MultiJson.load(actual_json), :pretty => true)
expected = MultiJson.load(expected_json)
actual = MultiJson.load(actual_json)
actual.should == expected
end

Expand Down
3 changes: 2 additions & 1 deletion features/step_definitions/json_parser_steps.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'stringio'
require 'gherkin/formatter/pretty_formatter'
require 'gherkin/json_parser'
require 'multi_json'

Given /^a PrettyFormatter$/ do
@io = StringIO.new
Expand All @@ -12,7 +13,7 @@
end

Given /^the following JSON is parsed:$/ do |text|
@json_parser.parse(JSON.pretty_generate(JSON.parse(text)))
@json_parser.parse(MultiJson.dump(MultiJson.load(text), :pretty => true))
end

Then /^the outputted text should be:$/ do |expected_text|
Expand Down
8 changes: 4 additions & 4 deletions gherkin.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Gem::Specification.new do |s|
#
# Repeat these step for cucumber (swap `cucumber` with `gherkin`).
#
# When both are building OK, do a `bundle exec rake install` in both cucumber and gherkin projects, revert the changes in the first 2 steps
# When both are building OK, do a `bundle exec rake install` in both cucumber and gherkin projects, revert the changes in the first 2 steps
# and release both projects. Do this for both ruby 1.8.7, ruby 1.9.3 and jruby.
#
s.version = "2.11.6"
Expand All @@ -36,11 +36,11 @@ Gem::Specification.new do |s|
s.files -= Dir['lib/**/*.dll']
s.files -= Dir['lib/**/*.bundle']
s.files -= Dir['lib/**/*.so']

if ENV['GEM_PLATFORM']
puts "GEM_PLATFORM:#{ENV['GEM_PLATFORM']}"
end
s.platform = ENV['GEM_PLATFORM'] if ENV['GEM_PLATFORM']
s.platform = ENV['GEM_PLATFORM'] if ENV['GEM_PLATFORM']
case s.platform.to_s
when /java/
s.files += Dir['lib/*.jar']
Expand All @@ -56,7 +56,7 @@ Gem::Specification.new do |s|
end
s.files -= Dir['**/.gitignore']

s.add_runtime_dependency('json', '>= 1.7.6')
s.add_runtime_dependency('multi_json', '~> 1.3')

s.add_development_dependency('cucumber', '>= 1.2.1')
s.add_development_dependency('rake', '>= 10.0.3')
Expand Down
12 changes: 6 additions & 6 deletions lib/gherkin/i18n.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'json'
require 'gherkin/rubify'
require 'gherkin/native'
require 'multi_json'

module Gherkin
class I18n
Expand All @@ -11,7 +11,7 @@ class I18n
FEATURE_ELEMENT_KEYS = %w{feature background scenario scenario_outline examples}
STEP_KEYWORD_KEYS = %w{given when then and but}
KEYWORD_KEYS = FEATURE_ELEMENT_KEYS + STEP_KEYWORD_KEYS
LANGUAGES = JSON.parse(File.open(File.dirname(__FILE__) + '/i18n.json', 'r:utf-8').read)
LANGUAGES = MultiJson.load(File.open(File.dirname(__FILE__) + '/i18n.json', 'r:utf-8').read)

class << self
include Rubify
Expand All @@ -30,7 +30,7 @@ def get(iso_code)
# there is typically a code generation tool to generate regular expressions for recognising the
# various I18n translations of Gherkin's keywords.
#
# The +keywords+ arguments can be one of <tt>:feature</tt>, <tt>:background</tt>, <tt>:scenario</tt>,
# The +keywords+ arguments can be one of <tt>:feature</tt>, <tt>:background</tt>, <tt>:scenario</tt>,
# <tt>:scenario_outline</tt>, <tt>:examples</tt>, <tt>:step</tt>.
def keyword_regexp(*keywords)
unique_keywords = all.map do |i18n|
Expand All @@ -42,7 +42,7 @@ def keyword_regexp(*keywords)
end
end
end

unique_keywords.flatten.compact.map{|kw| kw.to_s}.sort.reverse.uniq.join('|').gsub(/\*/, '\*')
end

Expand Down Expand Up @@ -150,14 +150,14 @@ def keyword_table
gherkin_keyword_table = KEYWORD_KEYS.map do |key|
Formatter::Model::Row.new([], [key, keywords(key).map{|keyword| %{"#{keyword}"}}.join(', ')], nil)
end

code_keyword_table = STEP_KEYWORD_KEYS.map do |key|
code_keywords = keywords(key).reject{|keyword| keyword == '* '}.map do |keyword|
%{"#{self.class.code_keyword_for(keyword)}"}
end.join(', ')
Formatter::Model::Row.new([], ["#{key} (code)", code_keywords], nil)
end

pf.table(gherkin_keyword_table + code_keyword_table)
io.string
end
Expand Down
8 changes: 4 additions & 4 deletions lib/gherkin/json_parser.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require 'json'
require 'gherkin/formatter/model'
require 'base64'
require 'gherkin/formatter/argument'
require 'gherkin/formatter/model'
require 'gherkin/native'
require 'base64'
require 'multi_json'

module Gherkin
class JSONParser
Expand All @@ -17,7 +17,7 @@ def initialize(reporter, formatter)
# Parse a gherkin object +o+, which can either be a JSON String,
# or a Hash (from a parsed JSON String).
def parse(o)
o = JSON.parse(o) if String === o
o = MultiJson.load(o) if String === o

o.each do |f|
@formatter.uri(f['uri'])
Expand Down
7 changes: 4 additions & 3 deletions spec/gherkin/formatter/json_formatter_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require 'spec_helper'
require 'stringio'
require 'gherkin/formatter/json_formatter'
require 'gherkin/formatter/model'
require 'multi_json'
require 'stringio'

module Gherkin
module Formatter
Expand All @@ -25,7 +26,7 @@ module Formatter

f.eof
f.done

expected = %{
[
{
Expand Down Expand Up @@ -85,7 +86,7 @@ module Formatter
}
]
}
JSON.parse(expected).should == JSON.parse(io.string)
MultiJson.load(expected).should == MultiJson.load(io.string)
end
end
end
Expand Down
21 changes: 11 additions & 10 deletions spec/gherkin/json_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
require 'spec_helper'
require 'gherkin/json_parser'
require 'gherkin/formatter/json_formatter'
require 'multi_json'

module Gherkin
describe JSONParser do
describe JSONParser do

def check_json(json)
io = StringIO.new
f = Formatter::JSONFormatter.new(io)
p = JSONParser.new(f, f)
p.parse(json)
f.done
expected = JSON.parse(json)
actual = JSON.parse(io.string)
expected = MultiJson.load(json)
actual = MultiJson.load(io.string)

begin
actual.should == expected
Expand All @@ -34,10 +35,10 @@ def check_json(json)
{
"id": "one",
"uri": "test.feature",
"keyword": "Feature",
"name": "One",
"description": "",
"line" : 3
"keyword": "Feature",
"name": "One",
"description": "",
"line" : 3
}
]})
end
Expand All @@ -53,9 +54,9 @@ def check_json(json)
"line": 22
}
],
"keyword": "Feature",
"name": "One",
"description": "",
"keyword": "Feature",
"name": "One",
"description": "",
"line": 3,
"elements": [
{
Expand Down

0 comments on commit 66e13da

Please sign in to comment.