wankel 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f6571c0e2472dab431e07ca8ee912e4a88b5319d
4
- data.tar.gz: 50aeba78c777dd142181126b1fefba3480a200e1
3
+ metadata.gz: a2a9555df2d54b524e29a89321699384d49e0613
4
+ data.tar.gz: 1e924383e839c3bda6f31b8a316ad2d55b67bd17
5
5
  SHA512:
6
- metadata.gz: 7d49e4dc10b8a3cccdfec412531226b5e4ccbba35a30c9b148edda091c42ef7c54aa7b368aef8930f46bd6b1fbf4214f1e29414e459d8078ae17ae7756b00433
7
- data.tar.gz: 8e0a6915521e4d9c7d5d27f580324a60074fe17d3e38078b2c9225e33b188c7abe4b88668fa8021d7827e4474f4afd67ef920847d65c34c44e101daab40fdcd4
6
+ metadata.gz: 24847eb3f44ffd7c2eb6eebd205f96ee6e155e68b22aea2e64d826aaf3fe4ec9d5d7df485653f86034205bbf0cd5a07c19e8fe7055e2eccc9e9b7d4551abe62f
7
+ data.tar.gz: 9efcc10ef6c1169cbadce7c63c27b3db150794666e4aeb0b69556699afe5800ca092fb4908e9868bdd3577a750ac5a089aa4b342476504767390b2723e60f71c
data/.gitignore CHANGED
@@ -10,3 +10,4 @@ coverage
10
10
  Makefile
11
11
  tmp
12
12
  .DS_Store
13
+ doc
data/.travis.yml CHANGED
@@ -2,7 +2,7 @@ language: ruby
2
2
  rvm:
3
3
  - 2.0.0
4
4
  - 2.1.2
5
- - 2.2.0-rc1
5
+ - 2.2.0
6
6
  cache: bundler
7
7
  install:
8
8
  - wget 'https://github.com/lloyd/yajl/archive/2.1.0.tar.gz'
data/CHANGELOG.md CHANGED
@@ -8,6 +8,12 @@ Minor Changes:
8
8
 
9
9
  Bugfixes:
10
10
 
11
+ ## 0.6.0
12
+
13
+ New Features:
14
+
15
+ - Added `:mode` option for the `Wankel::StreamEncoder`
16
+
11
17
  ## 0.5.0 (December 24th, 2014)
12
18
 
13
19
  - Renaming of `Wankel::SaxEncoder` and `Wankel::SaxParser` to
@@ -29,4 +35,4 @@ Major Changes:
29
35
 
30
36
  Major Changes:
31
37
 
32
- - `Wankel::SaxEncoder#bool` changed to `Wankel::SaxEncoder#boolean`
38
+ - `Wankel::SaxEncoder#bool` changed to `Wankel::SaxEncoder#boolean`
data/README.md CHANGED
@@ -1,5 +1,4 @@
1
- <a href="http://wanklerb.com">![Wankel](/logo.png)</a>
2
-
1
+ [![Wankel](logo.png)](http://wanklerb.org)
3
2
 
4
3
  Wankel [![Build Status](https://travis-ci.org/malomalo/wankel.svg?branch=master)](https://travis-ci.org/malomalo/wankel)
5
4
  --------
@@ -238,4 +237,82 @@ The test description (ie. Small: 100,000 parses of a 255 byte JSON):
238
237
 
239
238
  #### Streaming Encoder
240
239
 
240
+ class Wankel::SaxEncoder
241
+
242
+ def number
243
+ end
244
+
245
+ def string
246
+ end
247
+
248
+ def null
249
+ end
250
+
251
+ def bool
252
+ end
253
+
254
+ def map_open
255
+ end
256
+
257
+ def map_close
258
+ end
259
+
260
+ def array_open
261
+ end
262
+
263
+ def array_close
264
+ end
265
+
266
+ def value
267
+ end
268
+
269
+ def complete
270
+ end
271
+
272
+ end
273
+
241
274
  #### Streaming Decoder
275
+
276
+ class Wankel::SaxParser
277
+
278
+ def on_map_start
279
+ puts 'start map'
280
+ end
281
+
282
+ def on_map_end
283
+ puts 'end map'
284
+ end
285
+
286
+ def on_map_key(key)
287
+ puts 'key'
288
+ end
289
+
290
+ def on_null
291
+ puts "null"
292
+ end
293
+
294
+ def on_boolean(value)
295
+ puts value
296
+ end
297
+
298
+ def on_integer(i)
299
+ puts "integer"
300
+ end
301
+
302
+ def on_double(d)
303
+ puts "double"
304
+ end
305
+
306
+ def on_string(s)
307
+ puts "string"
308
+ end
309
+
310
+ def on_array_start
311
+ puts "start array"
312
+ end
313
+
314
+ def on_array_end
315
+ puts "end array"
316
+ end
317
+
318
+ end
data/Rakefile CHANGED
@@ -5,7 +5,7 @@ Bundler.require(:development)
5
5
  require "rake/extensiontask"
6
6
  require "rake/testtask"
7
7
  require "rubygems/package_task"
8
- require "rdoc/task"
8
+ require "sdoc"
9
9
  require 'fileutils'
10
10
 
11
11
  if ENV['COVERALLS_REPO_TOKEN']
@@ -65,3 +65,20 @@ task :c_coverage do
65
65
  SimpleCov::ResultMerger.store_result(data)
66
66
  end
67
67
  end
68
+
69
+ RDoc::Task.new do |rdoc|
70
+ rdoc.main = 'README.md'
71
+ rdoc.title = 'Wankel API'
72
+ rdoc.rdoc_dir = 'doc'
73
+
74
+ rdoc.rdoc_files.include('README.md')
75
+ rdoc.rdoc_files.include('logo.png')
76
+ rdoc.rdoc_files.include('lib/**/*.rb')
77
+ rdoc.rdoc_files.include('ext/**/*.{h,c}')
78
+
79
+ rdoc.options << '-f' << 'sdoc'
80
+ rdoc.options << '-T' << '42floors'
81
+ rdoc.options << '--charset' << 'utf-8'
82
+ rdoc.options << '--line-numbers'
83
+ rdoc.options << '--github'
84
+ end
data/ext/wankel/wankel.c CHANGED
@@ -4,7 +4,34 @@ static ID intern_new, intern_parse, intern_encode;
4
4
 
5
5
  static VALUE c_wankel, c_wankelParser, c_wankelEncoder, e_parseError, e_encodeError;
6
6
 
7
- // Class Methods =============================================================
7
+ /*
8
+ * Document-method: dump
9
+ * call-seq:
10
+ * dump(obj, io=nil, opts=nil)
11
+ */
12
+
13
+ /*
14
+ * Document-method: load
15
+ * call-seq:
16
+ * load(io, opts=nil, &block)
17
+ */
18
+
19
+ /*
20
+ * Document-method: parse
21
+ * call-seq:
22
+ * parse(io, opts=nil, &block=nil)
23
+ *
24
+ * [io] The +IO+ object parse JSON from.
25
+ * [opts] An optional hash of options to override the default parsing options.
26
+ * See DEFAULTS.
27
+ * [&block] An optional callback used with the +multiple_values+ option. For
28
+ * example:
29
+ * results = []
30
+ * p = Wankel::Parser.new(:multiple_values => true)
31
+ * p.parse('[{"abc": 123},{"def": 456}][{"abc": 123},{"def": 456}]') do |data|
32
+ * result << data
33
+ * end
34
+ */
8
35
  static VALUE wankel_parse(int argc, VALUE * argv, VALUE klass) {
9
36
  VALUE parser, input, options, callback;
10
37
  rb_scan_args(argc, argv, "11&", &input, &options, &callback);
@@ -13,6 +40,18 @@ static VALUE wankel_parse(int argc, VALUE * argv, VALUE klass) {
13
40
  return rb_funcall(parser, intern_parse, 2, input, callback);
14
41
  }
15
42
 
43
+ /* call-seq:
44
+ * encode(obj, io=nil, opts=nil)
45
+ *
46
+ * Returns the obj encoded as a JSON string.
47
+ *
48
+ * [obj] The object to be encoded as JSON
49
+ * [io] Either an +IO+ object to which the encoded JSON is written or the
50
+ * options (opts) hash.
51
+ * [ops] An optional hash to override the encoding options. See DEFAULTS. If
52
+ * there is no +IO+ object this is the second argument. If there is an
53
+ * +IO+ object it is the third argument.
54
+ */
16
55
  static VALUE wankel_encode(int argc, VALUE * argv, VALUE klass) {
17
56
  VALUE encoder, input, output, options;
18
57
  rb_scan_args(argc, argv, "12", &input, &output, &options);
@@ -39,9 +78,10 @@ void Init_wankel() {
39
78
  rb_define_singleton_method(c_wankel, "parse", wankel_parse, -1);
40
79
  rb_define_singleton_method(c_wankel, "encode", wankel_encode, -1);
41
80
 
42
- rb_define_singleton_method(c_wankel, "load", wankel_parse, -1);
43
- rb_define_singleton_method(c_wankel, "dump", wankel_encode, -1);
44
-
81
+ VALUE c_wankel_singleton = rb_singleton_class(c_wankel);
82
+ rb_define_alias(c_wankel_singleton, "load", "parse");
83
+ rb_define_alias(c_wankel_singleton, "dump", "encode");
84
+
45
85
  c_wankelParser = Init_wankel_parser();
46
86
  c_wankelEncoder = Init_wankel_encoder();
47
87
  Init_wankel_stream_parser();
@@ -5,8 +5,9 @@
5
5
  #include <yajl/yajl_common.h>
6
6
  #include <yajl/yajl_parse.h>
7
7
  #include <yajl/yajl_gen.h>
8
-
8
+
9
9
  // Yajl Helpers ==============================================================
10
+ void Init_yajl_helpers();
10
11
  void yajl_helper_check_status(yajl_handle handle, yajl_status status, int verbose, const unsigned char * jsonText, size_t jsonTextLength);
11
12
  void yajl_helper_check_gen_status(yajl_gen_status status);
12
13
 
@@ -0,0 +1,35 @@
1
+ class Wankel
2
+ class StreamEncoder
3
+
4
+ def value(val)
5
+ case val
6
+ when NilClass
7
+ null
8
+ when TrueClass, FalseClass
9
+ boolean(val)
10
+ when Numeric
11
+ number(val)
12
+ when String
13
+ string(val)
14
+ when Array
15
+ array_open
16
+ val.each {|v| value(v) }
17
+ array_close
18
+ when Hash
19
+ map_open
20
+ val.each {|k, v| string(k.to_s); value(v) }
21
+ map_close
22
+ else
23
+ case @options[:mode]
24
+ when :strict
25
+ raise Wankel::EncodeError, "Unkown JSON type #{val.class}"
26
+ when :nil
27
+ null
28
+ else
29
+ value(val.send(@options[:mode]))
30
+ end
31
+ end
32
+ end
33
+
34
+ end
35
+ end
data/lib/wankel.rb CHANGED
@@ -1,44 +1,59 @@
1
1
  require 'wankel/wankel'
2
2
 
3
3
  class Wankel
4
- DEFAULTS = {
5
- :read_buffer_size => 8092,
6
- :symbolize_keys => false,
7
- :allow_comments => false,
8
- :validate_utf8 => false,
9
- :allow_trailing_garbage => false,
10
- :multiple_values => false,
11
- :allow_partial_values => false,
4
+
5
+ # [read_buffer_size] The size of the read buffer in bytes. Deafults to
6
+ # +8092+.
7
+ # [symbolize_keys] Set the keys of hashes to symbols instead of
8
+ # strings. Defaults to +false+.
9
+ # [allow_comments] Ignore JavaScript style comments present in JSON
10
+ # input. Defaults to +false+.
11
+ # [validate_utf8] Verify that all strings in the JSON input is valid
12
+ # UTF-8. Defaults to +false+.
13
+ # [allow_trailing_garbage] Don't raise an error there is content after the end
14
+ # of the JSON document. Defaults to +false+.
15
+ # [multiple_values] Allow multiple JSON documents to be parsed. Every
16
+ # document must be valid and seperated by whitespace
17
+ # of any kind. The parser will continue to operate
18
+ # on input rather going into a completed state.
19
+ # Useful for parsing multiple values from an input
20
+ # stream. Defaults to +false+.
21
+ # [allow_partial_values] The parser will check the top level value was
22
+ # completely consumed by default, setting this allows
23
+ # partial JSON values. Defaults to +false+.
24
+ # [mode] The mode used for generate JSON. Setting mode to
25
+ # +:strict+ mode only allows the 7 basic JSON types.
26
+ # Setting the mode to +:nil+ replaces an object that is
27
+ # not a basic JSON type with null. If any other value
28
+ # is passed it will call the object method with the
29
+ # same name to serialize it to JSON. Defaults to
30
+ # +:strict+
31
+ # [write_buffer_size] The size of the write buffer in bytes. Defaults to
32
+ # +8092+.
33
+ # [beautify] Generate indented (beautiful) output. Defaults to
34
+ # +false+.
35
+ # [indent_string] The indent string used when beautify is set.
36
+ # Defaults to +' '+ (4 spaces).
37
+ # [validate_utf8] Verify that all strings in JSON input are valid
38
+ # UTF-8. Defaults to +false+.
39
+ # [escape_solidus] Alway escape the forward solidus ('/'), even though
40
+ # it is not required in JSON strings. Defaults to +false+.
41
+ DEFAULTS = {
42
+ :read_buffer_size => 8092,
43
+ :symbolize_keys => false,
44
+ :allow_comments => false,
45
+ :validate_utf8 => false,
46
+ :allow_trailing_garbage => false,
47
+ :multiple_values => false,
48
+ :allow_partial_values => false,
12
49
 
13
- :write_buffer_size => 8092,
14
- :beautify => false,
15
- :indent_string => ' ',
16
- :validate_utf8 => false,
17
- :escape_solidus => false
18
- }
50
+ :mode => :strict,
51
+ :write_buffer_size => 8092,
52
+ :beautify => false,
53
+ :indent_string => ' ',
54
+ :validate_utf8 => false,
55
+ :escape_solidus => false
56
+ }
19
57
  end
20
58
 
21
- class Wankel::StreamEncoder
22
-
23
- def value(val)
24
- case val
25
- when NilClass
26
- null
27
- when TrueClass, FalseClass
28
- boolean(val)
29
- when Numeric
30
- number(val)
31
- when String
32
- string(val)
33
- when Array
34
- array_open
35
- val.each {|v| value(v) }
36
- array_close
37
- when Hash
38
- map_open
39
- val.each {|k, v| string(k.to_s); value(v) }
40
- map_close
41
- end
42
- end
43
-
44
- end
59
+ require 'wankel/stream_encoder'
@@ -142,6 +142,28 @@ class Wankel::StreamEncoderTest < Minitest::Test
142
142
  assert_output('{"one":"one","two":2,"three":true,"four":null,"5":[45.21]}')
143
143
  end
144
144
 
145
+ test "value(Object) :mode => :strict" do
146
+ assert_raises Wankel::EncodeError do
147
+ @encoder.value(StringIO.new)
148
+ end
149
+ end
150
+
151
+ test "value(Object) :mode => :nil" do
152
+ @encoder = Wankel::StreamEncoder.new(@output, :mode => :nil)
153
+ @encoder.value(StringIO.new)
154
+ @encoder.flush
155
+
156
+ assert_output("null")
157
+ end
158
+
159
+ test "value(Object) :mode => :string" do
160
+ @encoder = Wankel::StreamEncoder.new(@output, :mode => :string)
161
+ @encoder.value(StringIO.new("string method"))
162
+ @encoder.flush
163
+
164
+ assert_output('"string method"')
165
+ end
166
+
145
167
  test 'flush' do
146
168
  output = StringIO.new
147
169
 
data/wankel.gemspec CHANGED
@@ -1,22 +1,27 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'wankel'
3
- s.version = '0.5.0'
3
+ s.version = '0.6.0'
4
4
  s.licenses = ['MIT']
5
5
  s.authors = ['Jon Bracy']
6
6
  s.email = ['jonbracy@gmail.com']
7
7
  s.homepage = 'http://wankelrb.com'
8
8
  s.summary = 'Streaming JSON parser and encoder'
9
9
  s.description = 'A JSON parser that enables streaming parsing and encoding of JSON'
10
-
10
+
11
+ s.extra_rdoc_files = %w(README.md)
12
+ s.rdoc_options.concat ['--main', 'README.md']
13
+
11
14
  s.files = `git ls-files`.split("\n")
12
15
  s.extensions = ['ext/wankel/extconf.rb']
13
16
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
17
  s.require_paths = ['lib']
15
18
 
16
19
  s.required_ruby_version = '>= 2.0.0'
17
-
20
+
18
21
  s.add_development_dependency 'rake-compiler', '~> 0.9'
19
22
  s.add_development_dependency 'minitest', '~> 5.3'
20
23
  s.add_development_dependency 'minitest-reporters', '~> 1.0'
21
24
  s.add_development_dependency 'mocha', '~> 1.1'
22
- end
25
+ s.add_development_dependency 'sdoc', '~> 0.4'
26
+ s.add_development_dependency 'sdoc-templates-42floors', '~> 0.3'
27
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wankel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Bracy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-25 00:00:00.000000000 Z
11
+ date: 2015-02-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake-compiler
@@ -66,13 +66,42 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '1.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sdoc
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.4'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.4'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sdoc-templates-42floors
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.3'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.3'
69
97
  description: A JSON parser that enables streaming parsing and encoding of JSON
70
98
  email:
71
99
  - jonbracy@gmail.com
72
100
  executables: []
73
101
  extensions:
74
102
  - ext/wankel/extconf.rb
75
- extra_rdoc_files: []
103
+ extra_rdoc_files:
104
+ - README.md
76
105
  files:
77
106
  - ".gitignore"
78
107
  - ".tm_properties"
@@ -100,6 +129,7 @@ files:
100
129
  - ext/wankel/yajl_helpers.c
101
130
  - ext/wankel/yajl_helpers.h
102
131
  - lib/wankel.rb
132
+ - lib/wankel/stream_encoder.rb
103
133
  - logo.png
104
134
  - test/encoding/encoding_test.rb
105
135
  - test/encoding/sax_encoder_test.rb
@@ -174,7 +204,9 @@ licenses:
174
204
  - MIT
175
205
  metadata: {}
176
206
  post_install_message:
177
- rdoc_options: []
207
+ rdoc_options:
208
+ - "--main"
209
+ - README.md
178
210
  require_paths:
179
211
  - lib
180
212
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -189,7 +221,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
189
221
  version: '0'
190
222
  requirements: []
191
223
  rubyforge_project:
192
- rubygems_version: 2.2.2
224
+ rubygems_version: 2.4.5
193
225
  signing_key:
194
226
  specification_version: 4
195
227
  summary: Streaming JSON parser and encoder