json_builder 2.0.6 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +122 -0
- data/README.md +221 -59
- data/Rakefile +26 -0
- data/lib/json_builder.rb +1 -1
- data/lib/json_builder/compiler.rb +63 -0
- data/lib/json_builder/elements.rb +17 -0
- data/lib/json_builder/member.rb +24 -0
- data/lib/json_builder/template.rb +40 -14
- data/lib/json_builder/value.rb +30 -0
- data/lib/json_builder/version.rb +1 -1
- data/spec/benchmarks/builder.rb +60 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/controllers/application_controller.rb +3 -0
- data/spec/dummy/app/controllers/users_controller.rb +5 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/helpers/users_helper.rb +2 -0
- data/spec/dummy/app/models/user.rb +9 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/app/views/users/index.json_builder +3 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +45 -0
- data/spec/dummy/config/boot.rb +10 -0
- data/spec/dummy/config/database.yml +22 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +26 -0
- data/spec/dummy/config/environments/production.rb +49 -0
- data/spec/dummy/config/environments/test.rb +35 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/inflections.rb +10 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +7 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/routes.rb +5 -0
- data/spec/dummy/db/development.sqlite3 +0 -0
- data/spec/dummy/db/migrate/20111127061428_create_users.rb +14 -0
- data/spec/dummy/db/schema.rb +23 -0
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/public/404.html +26 -0
- data/spec/dummy/public/422.html +26 -0
- data/spec/dummy/public/500.html +26 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/public/javascripts/application.js +2 -0
- data/spec/dummy/public/javascripts/controls.js +965 -0
- data/spec/dummy/public/javascripts/dragdrop.js +974 -0
- data/spec/dummy/public/javascripts/effects.js +1123 -0
- data/spec/dummy/public/javascripts/prototype.js +6001 -0
- data/spec/dummy/public/javascripts/rails.js +191 -0
- data/spec/dummy/public/stylesheets/.gitkeep +0 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/dummy/spec/controllers/users_controller_spec.rb +14 -0
- data/spec/integration/navigation_spec.rb +9 -0
- data/spec/json_builder_spec.rb +7 -0
- data/spec/spec_helper.rb +34 -0
- data/spec/support/json_builder.rb +7 -0
- data/spec/unit/json_builder_spec.rb +76 -0
- metadata +158 -93
- data/lib/json_builder/generator.rb +0 -127
- data/test/benchmarks/speed.rb +0 -85
- data/test/fixtures/simple.rb +0 -27
- data/test/test_helper.rb +0 -4
@@ -1,127 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'blankslate' unless defined? BlankSlate
|
3
|
-
require 'json'
|
4
|
-
|
5
|
-
module JSONBuilder
|
6
|
-
class Generator < BlankSlate
|
7
|
-
def initialize(options=nil)
|
8
|
-
@pretty_print ||= options.delete(:pretty) if !options.nil? && options[:pretty] # don't want nil
|
9
|
-
@compiled = []
|
10
|
-
@indent = 0
|
11
|
-
@passed_items = 0
|
12
|
-
@indent_next = false
|
13
|
-
@is_array = false
|
14
|
-
end
|
15
|
-
|
16
|
-
def inspect
|
17
|
-
compile!
|
18
|
-
end
|
19
|
-
|
20
|
-
#
|
21
|
-
# Using +tag!+ is neccessary when dynamic keys are needed
|
22
|
-
#
|
23
|
-
# json.tag!
|
24
|
-
#
|
25
|
-
def tag!(sym, *args, &block)
|
26
|
-
method_missing(sym.to_sym, *args, &block)
|
27
|
-
end
|
28
|
-
|
29
|
-
def array!(set, &block)
|
30
|
-
@array_length = set.length if set.respond_to?(:length)
|
31
|
-
@is_array = true
|
32
|
-
method_missing(nil, nil, &block)
|
33
|
-
end
|
34
|
-
|
35
|
-
def array_item!(&block)
|
36
|
-
method_missing(nil, nil, &block)
|
37
|
-
|
38
|
-
@passed_items += 1
|
39
|
-
@compiled << '},{' if @passed_items < @array_length
|
40
|
-
end
|
41
|
-
|
42
|
-
def method_missing(sym, *args, &block)
|
43
|
-
text = type_cast(args.first)
|
44
|
-
|
45
|
-
if block
|
46
|
-
start_block(sym) unless sym.nil?
|
47
|
-
block.call(self)
|
48
|
-
end_block unless sym.nil?
|
49
|
-
else
|
50
|
-
if @indent_next
|
51
|
-
@compiled[@compiled.length-1] = @compiled.last + "\"#{sym}\":#{text}"
|
52
|
-
@indent_next = false
|
53
|
-
else
|
54
|
-
@compiled << "\"#{sym}\": #{text}"
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
def compile!
|
60
|
-
# If there is no JSON, no need for an array
|
61
|
-
if @is_array
|
62
|
-
if @compiled.length > 0
|
63
|
-
compiled = ('[{' + @compiled.join(',') + '}]').gsub(',},{,', '},{')
|
64
|
-
else
|
65
|
-
# No need to make this pretty
|
66
|
-
@pretty_print = false
|
67
|
-
compiled = '[]'
|
68
|
-
end
|
69
|
-
else
|
70
|
-
compiled = '{' + @compiled.join(', ') + '}'
|
71
|
-
end
|
72
|
-
|
73
|
-
if @pretty_print
|
74
|
-
JSON.pretty_generate(JSON[compiled])
|
75
|
-
else
|
76
|
-
compiled
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
# Since most methods here are public facing,
|
81
|
-
private
|
82
|
-
def type_cast(text)
|
83
|
-
case text
|
84
|
-
when Array then '['+ text.map! { |j| type_cast(j) }.join(', ') +']'
|
85
|
-
when Hash then loop_hash(text)
|
86
|
-
when String then "\"#{text.gsub('"', '\"')}\""
|
87
|
-
when TrueClass then 'true'
|
88
|
-
when FalseClass then 'false'
|
89
|
-
when NilClass then 'null'
|
90
|
-
when DateTime, Time, Date then "\"#{text.strftime('%Y-%m-%dT%H:%M:%S%z')}\""
|
91
|
-
when Fixnum, Bignum then text
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
def loop_hash(hash)
|
96
|
-
compiled_hash = []
|
97
|
-
|
98
|
-
hash.each do |key, value|
|
99
|
-
compiled_hash << "\"#{key.to_s}\": #{type_cast(value)}"
|
100
|
-
end
|
101
|
-
|
102
|
-
'{' + compiled_hash.join(', ') + '}'
|
103
|
-
end
|
104
|
-
|
105
|
-
def start_indent
|
106
|
-
@indent += 1
|
107
|
-
end
|
108
|
-
|
109
|
-
def end_indent
|
110
|
-
@indent -= 1
|
111
|
-
end
|
112
|
-
|
113
|
-
def start_block(sym)
|
114
|
-
start_indent
|
115
|
-
@indent_next = true
|
116
|
-
@compiled << "\"#{sym}\":{"
|
117
|
-
end
|
118
|
-
|
119
|
-
def end_block
|
120
|
-
if @indent > 0
|
121
|
-
@compiled[@compiled.length-1] = @compiled.last + '}'
|
122
|
-
else
|
123
|
-
@compiled << '}'
|
124
|
-
end
|
125
|
-
end
|
126
|
-
end
|
127
|
-
end
|
data/test/benchmarks/speed.rb
DELETED
@@ -1,85 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'benchmark'
|
3
|
-
require 'builder'
|
4
|
-
require 'json_builder'
|
5
|
-
|
6
|
-
Benchmark.bm do |b|
|
7
|
-
b.report('JSON') do
|
8
|
-
15000.times {
|
9
|
-
j = JSONBuilder::Generator.new
|
10
|
-
j.name "Garrett Bjerkhoel"
|
11
|
-
j.birthday Time.local(1991, 9, 14)
|
12
|
-
j.street do
|
13
|
-
j.address "1143 1st Ave"
|
14
|
-
j.address2 "Apt 200"
|
15
|
-
j.city "New York"
|
16
|
-
j.state "New York"
|
17
|
-
j.zip 10065
|
18
|
-
end
|
19
|
-
j.skills do
|
20
|
-
j.ruby true
|
21
|
-
j.asp false
|
22
|
-
j.php true
|
23
|
-
j.mysql true
|
24
|
-
j.mongodb true
|
25
|
-
j.haproxy true
|
26
|
-
j.marathon false
|
27
|
-
end
|
28
|
-
j.single_skills ['ruby', 'php', 'mysql', 'mongodb', 'haproxy']
|
29
|
-
j.booleans [true, true, false, nil]
|
30
|
-
j.compile!
|
31
|
-
}
|
32
|
-
end
|
33
|
-
b.report('JSON Pretty') do
|
34
|
-
15000.times {
|
35
|
-
j = JSONBuilder::Generator.new(:pretty => true)
|
36
|
-
j.name "Garrett Bjerkhoel"
|
37
|
-
j.birthday Time.local(1991, 9, 14)
|
38
|
-
j.street do
|
39
|
-
j.address "1143 1st Ave"
|
40
|
-
j.address2 "Apt 200"
|
41
|
-
j.city "New York"
|
42
|
-
j.state "New York"
|
43
|
-
j.zip 10065
|
44
|
-
end
|
45
|
-
j.skills do
|
46
|
-
j.ruby true
|
47
|
-
j.asp false
|
48
|
-
j.php true
|
49
|
-
j.mysql true
|
50
|
-
j.mongodb true
|
51
|
-
j.haproxy true
|
52
|
-
j.marathon false
|
53
|
-
end
|
54
|
-
j.single_skills ['ruby', 'php', 'mysql', 'mongodb', 'haproxy']
|
55
|
-
j.booleans [true, true, false, nil]
|
56
|
-
j.compile!
|
57
|
-
}
|
58
|
-
end
|
59
|
-
b.report('Builder') do
|
60
|
-
15000.times {
|
61
|
-
xml = Builder::XmlMarkup.new(:indent => 2)
|
62
|
-
xml.name "Garrett Bjerkhoel"
|
63
|
-
xml.birthday Time.local(1991, 9, 14)
|
64
|
-
xml.street do
|
65
|
-
xml.address "1143 1st Ave"
|
66
|
-
xml.address2 "Apt 200"
|
67
|
-
xml.city "New York"
|
68
|
-
xml.state "New York"
|
69
|
-
xml.zip 10065
|
70
|
-
end
|
71
|
-
xml.skills do
|
72
|
-
xml.ruby true
|
73
|
-
xml.asp false
|
74
|
-
xml.php true
|
75
|
-
xml.mysql true
|
76
|
-
xml.mongodb true
|
77
|
-
xml.haproxy true
|
78
|
-
xml.marathon false
|
79
|
-
end
|
80
|
-
xml.single_skills ['ruby', 'php', 'mysql', 'mongodb', 'haproxy']
|
81
|
-
xml.booleans [true, true, false, nil]
|
82
|
-
xml.target!
|
83
|
-
}
|
84
|
-
end
|
85
|
-
end
|
data/test/fixtures/simple.rb
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
$:.unshift(File.expand_path('../../lib', File.dirname(__FILE__)))
|
2
|
-
require 'json_builder'
|
3
|
-
j = JSONBuilder::Generator.new(:pretty => true)
|
4
|
-
|
5
|
-
j.name "Garrett Bjerkhoel"
|
6
|
-
j.birthday Time.local(1991, 9, 14)
|
7
|
-
j.street do
|
8
|
-
j.address "1143 1st Ave"
|
9
|
-
j.address2 "Apt 200"
|
10
|
-
j.city "New York"
|
11
|
-
j.state "New York"
|
12
|
-
j.zip 10065
|
13
|
-
end
|
14
|
-
j.hashed({ :my_name => "Garrett Bjerkhoel".split('') })
|
15
|
-
j.skills do
|
16
|
-
j.ruby true
|
17
|
-
j.asp false
|
18
|
-
j.php true
|
19
|
-
j.mysql true
|
20
|
-
j.mongodb true
|
21
|
-
j.haproxy true
|
22
|
-
j.marathon false
|
23
|
-
end
|
24
|
-
j.single_skills ['ruby', 'php', 'mysql', 'mongodb', 'haproxy']
|
25
|
-
j.booleans [true, true, false, nil]
|
26
|
-
|
27
|
-
puts j.compile!
|
data/test/test_helper.rb
DELETED