elster 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- elster (0.0.6)
4
+ elster (0.0.7)
5
5
  multi_json
6
6
 
7
7
  GEM
@@ -1,6 +1,6 @@
1
1
  # Elster
2
2
 
3
- A simple streaming JSON encoder.
3
+ Elster is a streaming JSON encoder written in pure ruby.
4
4
 
5
5
  ## Installation
6
6
 
@@ -0,0 +1,4 @@
1
+ module Elster
2
+ class JsonContainerTypeError < StandardError
3
+ end
4
+ end
@@ -44,6 +44,10 @@ module Elster
44
44
  #
45
45
  # { "mistake" : [ "HUGE!" ] }
46
46
  def key(key, value=nil, &block)
47
+ if @current_type == :array
48
+ raise JsonContainerTypeError, "Attempted to write an object `key` value inside a JSON array."
49
+ end
50
+
47
51
  if @item_count > 0
48
52
  write ","
49
53
  else
@@ -82,6 +86,10 @@ module Elster
82
86
  #
83
87
  # [ 1, { "name" : "Wiggens" } ]
84
88
  def add(value=nil, &block)
89
+ if @current_type == :object
90
+ raise JsonContainerTypeError, "Attempted to add an array value inside a JSON object."
91
+ end
92
+
85
93
  if @item_count > 0
86
94
  write ","
87
95
  else
@@ -119,7 +127,7 @@ module Elster
119
127
  when nil
120
128
  encode_nil(value)
121
129
  else
122
- MultiJson.dump(value)
130
+ encode_generic(value)
123
131
  end
124
132
  end
125
133
 
@@ -127,7 +135,7 @@ module Elster
127
135
  MultiJson.dump(value)
128
136
  end
129
137
 
130
- SAFE_RE = Regexp.new("\\A[#{Regexp.escape((32..126).select{|c| c != 34}.map{|c| c.chr}.join)}]+\\z")
138
+ SAFE_RE = Regexp.new("\\A[#{Regexp.escape((32..126).select{|c| ![34, 92].include?(c)}.map{|c| c.chr}.join)}]+\\z")
131
139
  def encode_string(value)
132
140
  if value =~ SAFE_RE
133
141
  "\"#{value}\""
@@ -1,3 +1,3 @@
1
1
  module Elster
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -1,2 +1,3 @@
1
1
  require "elster/version"
2
+ require "elster/exceptions"
2
3
  require "elster/streamer"
@@ -110,11 +110,43 @@ class StreamerTest < MiniTest::Unit::TestCase
110
110
  assert_equal 44, parsed[1]["age"]
111
111
  end
112
112
 
113
+ def test_array_nested_array
114
+ json.add("99")
115
+ json.add do
116
+ json.add(1)
117
+ json.add(2)
118
+ end
119
+ json.add(99)
120
+ json.close
121
+
122
+ assert_equal "99", parsed[0]
123
+ assert_equal [1, 2], parsed[1]
124
+ assert_equal 99, parsed[2]
125
+ end
126
+
127
+ def test_array_of_nils
128
+ json.add
129
+ json.add
130
+ json.add(nil)
131
+ json.close
132
+
133
+ assert_equal [nil, nil, nil], parsed
134
+ end
135
+
136
+ def test_object_of_nils
137
+ json.key(:name)
138
+ json.key(:age)
139
+ json.close
140
+
141
+ assert_equal({"name" => nil, "age" => nil}, parsed)
142
+ end
143
+
113
144
  def test_ruby_hash
114
- json.key("name", {:first => "Bobert"})
145
+ json.key("name", {:first => "Bobert", :age => 99})
115
146
  json.close
116
147
 
117
148
  assert_equal "Bobert", parsed["name"]["first"]
149
+ assert_equal 99, parsed["name"]["age"]
118
150
  end
119
151
 
120
152
  def test_ruby_array
@@ -124,18 +156,18 @@ class StreamerTest < MiniTest::Unit::TestCase
124
156
  assert_equal [1, 2], parsed["an_array"]
125
157
  end
126
158
 
127
- def test_string_with_cr_at_end
128
- json.key("description", "I am \n")
159
+ def test_string_with_cr
160
+ json.key("description", "I am \nmulti\nlined")
129
161
  json.close
130
162
 
131
- assert_equal "I am \n", parsed["description"]
163
+ assert_equal "I am \nmulti\nlined", parsed["description"]
132
164
  end
133
165
 
134
- def test_string_with_cr
135
- json.key("description", "I am \nmulti\nlined")
166
+ def test_string_with_cr_at_end
167
+ json.key("description", "I am \n")
136
168
  json.close
137
169
 
138
- assert_equal "I am \nmulti\nlined", parsed["description"]
170
+ assert_equal "I am \n", parsed["description"]
139
171
  end
140
172
 
141
173
  def test_boolean_true
@@ -151,4 +183,26 @@ class StreamerTest < MiniTest::Unit::TestCase
151
183
 
152
184
  assert_equal false, parsed["no_works"]
153
185
  end
186
+
187
+ def test_encode_basic_chars
188
+ expected = " !\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~"
189
+ json.key("chars", expected)
190
+ json.close
191
+
192
+ assert_equal expected, parsed["chars"]
193
+ end
194
+
195
+ def test_no_key_in_arrays
196
+ json.add(1)
197
+ assert_raises(Elster::JsonContainerTypeError) do
198
+ json.key("DoesntMatter", "Nothing Written")
199
+ end
200
+ end
201
+
202
+ def test_no_add_in_objects
203
+ json.key("DoesntMatter", "Nothing will be written")
204
+ assert_raises(Elster::JsonContainerTypeError) do
205
+ json.add(1)
206
+ end
207
+ end
154
208
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elster
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -101,6 +101,7 @@ files:
101
101
  - /Users/jwebb/src/bigjason/elster/elster.gemspec
102
102
  - /Users/jwebb/src/bigjason/elster/Gemfile
103
103
  - /Users/jwebb/src/bigjason/elster/Gemfile.lock
104
+ - /Users/jwebb/src/bigjason/elster/lib/elster/exceptions.rb
104
105
  - /Users/jwebb/src/bigjason/elster/lib/elster/streamer.rb
105
106
  - /Users/jwebb/src/bigjason/elster/lib/elster/version.rb
106
107
  - /Users/jwebb/src/bigjason/elster/lib/elster.rb
@@ -108,6 +109,7 @@ files:
108
109
  - /Users/jwebb/src/bigjason/elster/pkg/elster-0.0.2.gem
109
110
  - /Users/jwebb/src/bigjason/elster/pkg/elster-0.0.3.gem
110
111
  - /Users/jwebb/src/bigjason/elster/pkg/elster-0.0.5.gem
112
+ - /Users/jwebb/src/bigjason/elster/pkg/elster-0.0.6.gem
111
113
  - /Users/jwebb/src/bigjason/elster/Rakefile
112
114
  - /Users/jwebb/src/bigjason/elster/README.md
113
115
  - /Users/jwebb/src/bigjason/elster/test/streamer_test.rb
@@ -125,7 +127,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
125
127
  version: '0'
126
128
  segments:
127
129
  - 0
128
- hash: 1630700077536672737
130
+ hash: -398928207981350799
129
131
  required_rubygems_version: !ruby/object:Gem::Requirement
130
132
  none: false
131
133
  requirements:
@@ -134,7 +136,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
134
136
  version: '0'
135
137
  segments:
136
138
  - 0
137
- hash: 1630700077536672737
139
+ hash: -398928207981350799
138
140
  requirements: []
139
141
  rubyforge_project:
140
142
  rubygems_version: 1.8.24