jsonify 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
data/jsonify.gemspec CHANGED
@@ -14,11 +14,12 @@ Gem::Specification.new do |s|
14
14
  s.rubyforge_project = s.name
15
15
 
16
16
  s.files = `git ls-files`.split("\n")
17
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,performance}/*`.split("\n")
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
20
 
21
- s.add_dependency 'json_pure'
21
+ # 1.9 JSON library built-in
22
+ s.add_development_dependency 'json' unless RUBY_VERSION =~ /^1.9/
22
23
 
23
24
  s.add_development_dependency 'bundler'
24
25
  s.add_development_dependency 'rake'
data/lib/jsonify.rb CHANGED
@@ -1,7 +1,10 @@
1
- require 'json'
1
+ begin
2
+ require 'json'
3
+ rescue LoadError
4
+ raise "No JSON implementation found. Please install a JSON library of your choosing."
5
+ end
2
6
  require 'jsonify/blank_slate'
3
7
  require 'jsonify/version'
4
8
  require 'jsonify/json_value'
5
9
  require 'jsonify/generate'
6
- require 'jsonify/builder'
7
- require 'benchmark'
10
+ require 'jsonify/builder'
@@ -35,7 +35,7 @@ module Jsonify
35
35
  # @raise [TypeError] only if +:verify+ is set to true
36
36
  # @raise [JSON::ParseError] only if +:verify+ is set to true
37
37
  def compile!
38
- result = (@stack[0] ? @stack[0].evaluate : {}.to_json)
38
+ result = (@stack[0] || {}).to_json
39
39
  JSON.parse(result) if @verify
40
40
  @pretty ? JSON.pretty_generate(JSON.parse(result)) : result
41
41
  end
@@ -4,31 +4,30 @@ module Jsonify
4
4
  module Generate
5
5
 
6
6
  class << self
7
-
8
- # Coerces the given value into a JsonValue.
7
+
8
+ # Coerces the given value into a JsonValue (or subclass), String, or Number.
9
9
  #
10
10
  # The coercion rules are based on the type (class) of the value as follows:
11
11
  # - +JsonValue+ => no coercion
12
- # - +String+ => JsonString ( \"foo\" )
13
- # - +Numeric+ => JsonNumber ( 1 )
12
+ # - +String+ => no coercion
13
+ # - +Numeric+ => no coercion
14
14
  # - +TrueClass+ => JsonTrue ( true )
15
15
  # - +FalseClass+=> JsonFalse ( false )
16
16
  # - +NilClass+ => JsonNull ( null )
17
17
  # - +Array+ => JsonArray ( [1,2,3] )
18
18
  # - +Hash+ => JsonObject ( <code>{"\a":1,\"b\":2}</code> )
19
+ # - +else+ => #to_s
19
20
  #
20
21
  # @param val value to coerce into a JsonValue.
21
22
  def value(val)
22
23
  case val
23
- when JsonValue; val
24
- when String; string_value val
25
- when Numeric; number_value val
26
- when TrueClass; true_value
27
- when FalseClass; false_value
28
- when NilClass; null_value
24
+ when JsonValue, String, Numeric; val
25
+ when TrueClass; @json_true ||= JsonTrue.new
26
+ when FalseClass; @json_false ||= JsonFalse.new
27
+ when NilClass; @json_null ||= JsonNull.new
29
28
  when Array; array_value val
30
- when Hash; object_value val
31
- else string_value val
29
+ when Hash; object_value val
30
+ else val.to_s
32
31
  end
33
32
  end
34
33
 
@@ -36,10 +35,6 @@ module Jsonify
36
35
  JsonPair.new(key,value(val))
37
36
  end
38
37
 
39
- def string_value(val)
40
- JsonString.new(val)
41
- end
42
-
43
38
  def object_value(hash)
44
39
  json_object = JsonObject.new
45
40
  hash.each { |key,val| json_object.add( pair_value(key, val) ) }
@@ -50,22 +45,6 @@ module Jsonify
50
45
  JsonArray.new(Array(vals).map{ |v| value v })
51
46
  end
52
47
 
53
- def number_value(val)
54
- JsonNumber.new(val)
55
- end
56
-
57
- def true_value
58
- @json_true ||= JsonTrue.new # memoize
59
- end
60
-
61
- def false_value
62
- @json_false ||= JsonFalse.new # memoize
63
- end
64
-
65
- def null_value
66
- @json_null ||= JsonNull.new # memoize
67
- end
68
-
69
48
  end
70
49
 
71
50
  end
@@ -6,8 +6,8 @@ module Jsonify
6
6
  @values = values || []
7
7
  end
8
8
 
9
- def evaluate
10
- wrap values.map {|v| v.evaluate}.join(',')
9
+ def to_json
10
+ wrap values.map {|v| v.to_json}.join(',')
11
11
  end
12
12
 
13
13
  def add(jsonValue)
@@ -41,7 +41,6 @@ module Jsonify
41
41
  end
42
42
 
43
43
  alias_method :<<, :add
44
- alias_method :add!, :add # for consistency with the Builder api
45
44
 
46
45
  end
47
46
 
@@ -61,7 +60,6 @@ module Jsonify
61
60
  end
62
61
 
63
62
  alias_method :<<, :add
64
- alias_method :add!, :add # for consistency with the Builder api
65
63
 
66
64
  end
67
65
 
@@ -71,45 +69,25 @@ module Jsonify
71
69
  @key = key.to_s
72
70
  @value = Generate.value(value)
73
71
  end
74
- def evaluate
75
- %Q{#{key.to_json}:#{value.evaluate}}
76
- end
77
- end
78
-
79
- class JsonString < JsonValue
80
- attr_accessor :value
81
- def initialize(value)
82
- @value = value.to_s
83
- end
84
- def evaluate
85
- value.to_json
86
- end
87
- end
88
-
89
- class JsonNumber < JsonValue
90
- attr_accessor :value
91
- def initialize(value)
92
- @value = value
93
- end
94
- def evaluate
95
- value
72
+ def to_json
73
+ %Q{#{key.to_json}:#{value.to_json}}
96
74
  end
97
75
  end
98
76
 
99
77
  class JsonTrue < JsonValue
100
- def evaluate
78
+ def to_json
101
79
  'true'
102
80
  end
103
81
  end
104
82
 
105
83
  class JsonFalse < JsonValue
106
- def evaluate
84
+ def to_json
107
85
  'false'
108
86
  end
109
87
  end
110
88
 
111
89
  class JsonNull < JsonValue
112
- def evaluate
90
+ def to_json
113
91
  'null'
114
92
  end
115
93
  end
@@ -1,3 +1,3 @@
1
1
  module Jsonify
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
File without changes
File without changes
data/spec/builder_spec.rb CHANGED
@@ -23,7 +23,7 @@ describe Jsonify::Builder do
23
23
  attr_accessor :stack
24
24
  end
25
25
  class FooBar
26
- def evaluate
26
+ def to_json
27
27
  "foobar"
28
28
  end
29
29
  end
@@ -13,7 +13,7 @@ describe Jsonify::Generate do
13
13
  json = Jsonify::Generate
14
14
  result = json.value links
15
15
  expected = '{"links":[{"rel":"foo","href":"goo"},{"rel":"bar","href":"baz"}]}'
16
- JSON.parse(result.evaluate).should == JSON.parse(expected)
16
+ JSON.parse(result.to_json).should == JSON.parse(expected)
17
17
  end
18
18
 
19
19
  describe 'complex example' do
@@ -29,7 +29,7 @@ describe Jsonify::Generate do
29
29
  }
30
30
  )
31
31
  expected = "{\"links\":[{\"rel\":\"foo\",\"href\":\"goo\"},{\"rel\":\"bar\",\"href\":\"baz\"}]}"
32
- JSON.parse(json.evaluate).should == JSON.parse(expected)
32
+ JSON.parse(json.to_json).should == JSON.parse(expected)
33
33
  end
34
34
  end
35
35
  end
@@ -3,49 +3,40 @@ require 'spec_helper'
3
3
  describe Jsonify::JsonValue do
4
4
 
5
5
  describe Jsonify::JsonPair do
6
- let(:pair) { Jsonify::JsonPair.new('key',Jsonify::JsonString.new('value')) }
6
+ let(:pair) { Jsonify::JsonPair.new('key','value') }
7
7
  it 'should be constructed of a key and value' do
8
8
  pair.key.should == 'key'
9
9
  end
10
10
  it 'should evaluate to key:value' do
11
- pair.evaluate.should == "\"key\":\"value\""
11
+ pair.to_json.should == "\"key\":\"value\""
12
12
  end
13
13
  end
14
14
 
15
15
  describe Jsonify::JsonTrue do
16
16
  it 'should have a value of true' do
17
- Jsonify::JsonTrue.new.evaluate.should == 'true'
17
+ Jsonify::JsonTrue.new.to_json.should == 'true'
18
18
  end
19
19
  end
20
20
 
21
21
  describe Jsonify::JsonFalse do
22
22
  it 'should have a value of false' do
23
- Jsonify::JsonFalse.new.evaluate.should == 'false'
23
+ Jsonify::JsonFalse.new.to_json.should == 'false'
24
24
  end
25
25
  end
26
26
 
27
27
  describe Jsonify::JsonNull do
28
28
  it 'should have a value of true' do
29
- Jsonify::JsonNull.new.evaluate.should == 'null'
29
+ Jsonify::JsonNull.new.to_json.should == 'null'
30
30
  end
31
31
  end
32
32
 
33
- describe Jsonify::JsonNumber do
34
- it 'should accept an integer' do
35
- Jsonify::JsonNumber.new(1).evaluate.should == 1
36
- end
37
- it 'should accept a float' do
38
- Jsonify::JsonNumber.new(1.23).evaluate.should == 1.23
39
- end
40
- end
41
-
42
- describe Jsonify::JsonString do
33
+ describe 'strings' do
43
34
  it 'should quote the value' do
44
- Jsonify::JsonString.new('foo').evaluate.should == "\"foo\""
35
+ 'foo'.to_json.should == "\"foo\""
45
36
  end
46
37
  it 'should encode unicode' do
47
38
  unicode = 'goober'.concat(16)
48
- Jsonify::JsonString.new(unicode).evaluate.should == "\"goober\\u0010\""
39
+ unicode.to_json.should == "\"goober\\u0010\""
49
40
  end
50
41
  end
51
42
 
data/spec/spec_helper.rb CHANGED
@@ -2,7 +2,6 @@ require 'benchmark'
2
2
  require 'bundler'
3
3
  require 'bundler/setup'
4
4
  require 'jsonify'
5
- require 'benchmark_helper'
6
5
 
7
6
  RSpec.configure do |config|
8
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,22 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-03 00:00:00.000000000Z
12
+ date: 2011-08-05 00:00:00.000000000Z
13
13
  dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: json_pure
16
- requirement: &70097037247660 !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: *70097037247660
25
14
  - !ruby/object:Gem::Dependency
26
15
  name: bundler
27
- requirement: &70097037247160 !ruby/object:Gem::Requirement
16
+ requirement: &70213882267320 !ruby/object:Gem::Requirement
28
17
  none: false
29
18
  requirements:
30
19
  - - ! '>='
@@ -32,10 +21,10 @@ dependencies:
32
21
  version: '0'
33
22
  type: :development
34
23
  prerelease: false
35
- version_requirements: *70097037247160
24
+ version_requirements: *70213882267320
36
25
  - !ruby/object:Gem::Dependency
37
26
  name: rake
38
- requirement: &70097037246620 !ruby/object:Gem::Requirement
27
+ requirement: &70213882266100 !ruby/object:Gem::Requirement
39
28
  none: false
40
29
  requirements:
41
30
  - - ! '>='
@@ -43,10 +32,10 @@ dependencies:
43
32
  version: '0'
44
33
  type: :development
45
34
  prerelease: false
46
- version_requirements: *70097037246620
35
+ version_requirements: *70213882266100
47
36
  - !ruby/object:Gem::Dependency
48
37
  name: rspec
49
- requirement: &70097037245940 !ruby/object:Gem::Requirement
38
+ requirement: &70213882264440 !ruby/object:Gem::Requirement
50
39
  none: false
51
40
  requirements:
52
41
  - - ! '>='
@@ -54,10 +43,10 @@ dependencies:
54
43
  version: '0'
55
44
  type: :development
56
45
  prerelease: false
57
- version_requirements: *70097037245940
46
+ version_requirements: *70213882264440
58
47
  - !ruby/object:Gem::Dependency
59
48
  name: autotest
60
- requirement: &70097037245420 !ruby/object:Gem::Requirement
49
+ requirement: &70213882261500 !ruby/object:Gem::Requirement
61
50
  none: false
62
51
  requirements:
63
52
  - - ! '>='
@@ -65,10 +54,10 @@ dependencies:
65
54
  version: '0'
66
55
  type: :development
67
56
  prerelease: false
68
- version_requirements: *70097037245420
57
+ version_requirements: *70213882261500
69
58
  - !ruby/object:Gem::Dependency
70
59
  name: yard
71
- requirement: &70097037244760 !ruby/object:Gem::Requirement
60
+ requirement: &70213882260300 !ruby/object:Gem::Requirement
72
61
  none: false
73
62
  requirements:
74
63
  - - ! '>='
@@ -76,10 +65,10 @@ dependencies:
76
65
  version: '0'
77
66
  type: :development
78
67
  prerelease: false
79
- version_requirements: *70097037244760
68
+ version_requirements: *70213882260300
80
69
  - !ruby/object:Gem::Dependency
81
70
  name: rdiscount
82
- requirement: &70097037244220 !ruby/object:Gem::Requirement
71
+ requirement: &70213882258020 !ruby/object:Gem::Requirement
83
72
  none: false
84
73
  requirements:
85
74
  - - ! '>='
@@ -87,10 +76,10 @@ dependencies:
87
76
  version: '0'
88
77
  type: :development
89
78
  prerelease: false
90
- version_requirements: *70097037244220
79
+ version_requirements: *70213882258020
91
80
  - !ruby/object:Gem::Dependency
92
81
  name: ruby-prof
93
- requirement: &70097037243620 !ruby/object:Gem::Requirement
82
+ requirement: &70213882254960 !ruby/object:Gem::Requirement
94
83
  none: false
95
84
  requirements:
96
85
  - - ! '>='
@@ -98,7 +87,7 @@ dependencies:
98
87
  version: '0'
99
88
  type: :development
100
89
  prerelease: false
101
- version_requirements: *70097037243620
90
+ version_requirements: *70213882254960
102
91
  description: Turn Ruby objects into JSON -- correctly!
103
92
  email:
104
93
  - bsiggelkow@me.com
@@ -119,15 +108,13 @@ files:
119
108
  - lib/jsonify/generate.rb
120
109
  - lib/jsonify/json_value.rb
121
110
  - lib/jsonify/version.rb
122
- - spec/benchmark_helper.rb
123
- - spec/benchmark_spek.rb
111
+ - performance/profile.rb
112
+ - performance/speed.rb
124
113
  - spec/builder_spec.rb
125
114
  - spec/generate_spec.rb
126
115
  - spec/json_value_spec.rb
127
116
  - spec/jsonify_spec.rb
128
- - spec/profile.rb
129
117
  - spec/spec_helper.rb
130
- - spec/speed.rb
131
118
  homepage: http://github.com/bsiggelkow/jsonify
132
119
  licenses: []
133
120
  post_install_message:
@@ -142,7 +129,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
142
129
  version: '0'
143
130
  segments:
144
131
  - 0
145
- hash: 2291352512493608136
132
+ hash: -1667055293303286952
146
133
  required_rubygems_version: !ruby/object:Gem::Requirement
147
134
  none: false
148
135
  requirements:
@@ -151,7 +138,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
151
138
  version: '0'
152
139
  segments:
153
140
  - 0
154
- hash: 2291352512493608136
141
+ hash: -1667055293303286952
155
142
  requirements: []
156
143
  rubyforge_project: jsonify
157
144
  rubygems_version: 1.8.6
@@ -159,12 +146,10 @@ signing_key:
159
146
  specification_version: 3
160
147
  summary: Turn Ruby objects into JSON
161
148
  test_files:
162
- - spec/benchmark_helper.rb
163
- - spec/benchmark_spek.rb
149
+ - performance/profile.rb
150
+ - performance/speed.rb
164
151
  - spec/builder_spec.rb
165
152
  - spec/generate_spec.rb
166
153
  - spec/json_value_spec.rb
167
154
  - spec/jsonify_spec.rb
168
- - spec/profile.rb
169
155
  - spec/spec_helper.rb
170
- - spec/speed.rb
@@ -1,133 +0,0 @@
1
- require "benchmark"
2
- require "rspec"
3
-
4
- module RSpec
5
- module Benchmark
6
- class Result
7
- attr_accessor :slowest, :fastest, :average, :elapsed
8
-
9
- def initialize(elapsed)
10
- @elapsed = elapsed
11
- @slowest = elapsed.max
12
- @fastest = elapsed.min
13
- @average = elapsed.inject(0) {|b, t| b + t} / elapsed.size
14
- end
15
-
16
- def to_s
17
- "[average: #{average}, slowest: #{slowest}, fastest: #{fastest}]"
18
- end
19
- end
20
-
21
- # Run a given block and calculate the average execution time.
22
- # The block will be executed 1000 times by default.
23
- #
24
- # benchmark { do something }
25
- # benchmark(100) { do something }
26
- #
27
- def benchmark(times = 1_000, &block)
28
- elapsed = (1..times).collect do
29
- GC.start
30
- ::Benchmark.realtime(&block) * 1000
31
- end
32
-
33
- Result.new(elapsed)
34
- end
35
- end
36
- end
37
-
38
- # Check if the slowest execution is less than expected.
39
- #
40
- # it "should do something fast" do
41
- # benchmark { do something }.should be_faster_than(1.3)
42
- # end
43
- #
44
- RSpec::Matchers.define :be_faster_than do |expected|
45
- match do |result|
46
- result.slowest < expected
47
- end
48
- end
49
-
50
- # Check if the slowest execution is greater than expected.
51
- #
52
- # it "should do something slow" do
53
- # benchmark { do something }.should_not be_slower_than(1.3)
54
- # end
55
- #
56
- RSpec::Matchers.define :be_slower_than do |expected|
57
- match do |result|
58
- result.slowest > expected
59
- end
60
- end
61
-
62
- # Check if the execution average is close to expected.
63
- #
64
- # it "should do something average time" do
65
- # benchmark { do something }.should be_on_average(1.3, 0.01)
66
- # end
67
- #
68
- RSpec::Matchers.define :be_on_average do |expected, delta|
69
- match do |result|
70
- (result.average - expected).abs < delta
71
- end
72
- end
73
-
74
- # Include matchers and <tt>benchmark</tt> method into RSpec context.
75
- RSpec.configure do |config|
76
- config.include(RSpec::Benchmark)
77
- end
78
-
79
- if ARGV[0] == __FILE__
80
- describe RSpec::Benchmark do
81
- before do
82
- GC.stub(:start)
83
- end
84
-
85
- it "should be faster than expected" do
86
- stub(:slowest => 0.01).should be_faster_than(0.5)
87
- end
88
-
89
- it "should not be faster than expected" do
90
- stub(:slowest => 2).should_not be_faster_than(0.5)
91
- end
92
-
93
- it "should be slower than expected" do
94
- stub(:slowest => 2).should be_slower_than(0.5)
95
- end
96
-
97
- it "should not be slower than expected" do
98
- stub(:slowest => 0.5).should_not be_slower_than(2)
99
- end
100
-
101
- it "should be on average" do
102
- stub(:average => 0.51).should be_on_average(0.5, 0.019)
103
- stub(:average => 0.49).should be_on_average(0.5, 0.019)
104
- end
105
-
106
- it "should call garbage collector" do
107
- GC.should_receive(:start).exactly(5).times
108
- benchmark(5) { true }
109
- end
110
-
111
- it "should return result with collected data" do
112
- result = benchmark(5) { true }
113
-
114
- result.average.should be_kind_of(Float)
115
- result.slowest.should be_kind_of(Float)
116
- result.fastest.should be_kind_of(Float)
117
- end
118
-
119
- it "should run block" do
120
- object = mock
121
- object.should_receive(:run).exactly(1000).times
122
-
123
- benchmark { object.run }
124
- end
125
-
126
- it "should run block with custom range" do
127
- object = mock
128
- object.should_receive(:run).exactly(3).times
129
-
130
- benchmark(3) { object.run }
131
- end
132
- end
133
- end
@@ -1,46 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Jsonify::Builder do
4
-
5
- let(:j) { Jsonify::Builder.new }
6
-
7
- describe 'hello world' do
8
- it "runs under 0.3 milliseconds" do
9
- benchmark do
10
- j.hello 'world'
11
- j.compile!
12
- j.reset!
13
- end.should be_faster_than( 0.3 ) #milliseconds
14
- end
15
- end
16
-
17
-
18
- describe 'json_builder example' do
19
- it 'should be better than builder (0.5 milliseconds)' do
20
- benchmark do
21
- j.name "Garrett Bjerkhoel"
22
- j.birthday Time.local(1991, 9, 14)
23
- j.street do
24
- j.address "1143 1st Ave"
25
- j.address2 "Apt 200"
26
- j.city "New York"
27
- j.state "New York"
28
- j.zip 10065
29
- end
30
- j.skills do
31
- j.ruby true
32
- j.asp false
33
- j.php true
34
- j.mysql true
35
- j.mongodb true
36
- j.haproxy true
37
- j.marathon false
38
- end
39
- j.single_skills ['ruby', 'php', 'mysql', 'mongodb', 'haproxy']
40
- j.booleans [true, true, false, nil]
41
- j.reset!
42
- end.should be_faster_than(0.5)
43
- end
44
- end
45
-
46
- end