ffi-yajl 1.3.1 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 010f67d48fdc7e489a8ca807819f76d4f678c7d7
4
- data.tar.gz: dc3398cacd981441353db287b91fccdbf24266f3
3
+ metadata.gz: 7af5eee3b4627e919e66d1aea4326bdf251c4256
4
+ data.tar.gz: 581d8c97420c1313de961eb974b74ded07c44f2b
5
5
  SHA512:
6
- metadata.gz: 06c1b64a2568cb90acada91dc9c3194b50d0fc8a671db94d1101943d77cd3fe1eed0918a02297455958b0708d456374b9affb84f4dd8c7b49a48ab25186a95f0
7
- data.tar.gz: 5528506311181bc998ce071935157f74c027313fb43af831fae27253016f271c5ea8b66315959f885132dc7b1d45c5b7a1c4080b9ac9e729977df90b0b7deebe
6
+ metadata.gz: feb427a319145b354c0fc8f506a8b535e56b79d14abe803b9728ef3e6cd633547ed8857245bc58d15c7eeb14332f1bee32007d457afbe8ee46458ae81cfd7ad4
7
+ data.tar.gz: d02c0c2c853b0d6d9fad1078d32a77e1984383592623121f748249e3cf7e605802b487b258442808341ad8fc559b07d650955d24e25c63f1727e758891df4b7d
data/README.md CHANGED
@@ -1,14 +1,40 @@
1
1
 
2
2
  [![Build Status](https://travis-ci.org/opscode/ffi-yajl.png)](https://travis-ci.org/opscode/ffi-yajl) [![Code Climate](https://codeclimate.com/github/opscode/ffi-yajl.png)](https://codeclimate.com/github/opscode/ffi-yajl)
3
3
 
4
- ## TODO
5
-
6
-
7
- ## BUILD NOTES
8
-
9
-
10
- ## KNOWN BUGS
11
-
4
+ # FFI YAJL
5
+
6
+ ffi-yajl is a Ruby adapter for the [yajl](http://lloyd.github.io/yajl/)
7
+ JSON parser/generator library. ffi-yajl supports multiple Ruby C
8
+ extension mechanisms, including both MRI native extensions and FFI in
9
+ order to be compatible with as many Ruby implementations as possible
10
+ while providing good performance where possible.
11
+
12
+ ## Basic Usage
13
+
14
+ ```ruby
15
+ require 'ffi-yajl'
16
+ json_out = FFI_Yajl::Encoder.encode( { "foo" => [ "bar", "baz" ] } )
17
+ # => "{\"foo\":[\"bar\",\"baz\"]}"
18
+ data_in = FFI_Yajl::Parser.parse( json_out )
19
+ # => {"foo"=>["bar", "baz"]}
20
+ ```
21
+
22
+ ## Why This Instead of X?
23
+
24
+ yajl is the only JSON library we've found that has error messages that
25
+ meet our requirements. The stdlib json gem and oj (at the time we
26
+ started this project) have error messages like "invalid token at byte
27
+ 1234," which are probably fine for server use, but in
28
+ [chef](https://github.com/chef/chef) we frequently deal with
29
+ user-written JSON documents, which means we need a good user experience
30
+ when encountering malformed JSON.
31
+
32
+ We previously used brianmario's
33
+ [yajl-ruby](https://github.com/brianmario/yajl-ruby) project, but we
34
+ wanted to be able to fallback to using FFI bindings to the C code (so we
35
+ could support non-MRI rubies) and we also needed some bug fixes in
36
+ yajl2, but the maintainer wasn't able to devote enough time to the
37
+ project to make these updates in a timeframe that worked for us.
12
38
 
13
39
  ## Thanks
14
40
 
@@ -17,3 +43,9 @@ pulled in from brianmario's existing yajl-ruby gem, particularly all the c exten
17
43
  process of writing this would have been much more difficult without being able to draw heavily from already solved problems in
18
44
  yajl-ruby.
19
45
 
46
+ ## License
47
+
48
+ Given that this draws heavily from the yajl-ruby sources, and could be considered a derivative work, the MIT License from that
49
+ project has been preserved and this source code has deliberately not been dual licensed under Chef's typical Apache License.
50
+ See the [LICENSE](https://github.com/chef/ffi-yajl/blob/master/LICENSE) file in this project.
51
+
@@ -8,7 +8,7 @@ module FFI_Yajl
8
8
  # initialization that we can do in pure ruby
9
9
  yajl_gen_opts = {}
10
10
 
11
- yajl_gen_opts[:yajl_gen_validate_utf8] = true
11
+ yajl_gen_opts[:yajl_gen_validate_utf8] = @opts[:validate_utf8] == false ? false : true
12
12
  yajl_gen_opts[:yajl_gen_beautify] = false
13
13
  yajl_gen_opts[:yajl_gen_indent_string] = " "
14
14
 
@@ -46,8 +46,10 @@ module FFI_Yajl
46
46
  raise FFI_Yajl::EncodeError, "Invalid number: cannot encode Infinity, -Infinity, or NaN"
47
47
  when 6 # yajl_gen_no_buf
48
48
  raise FFI_Yajl::EncodeError, "YAJL internal error: yajl_gen_get_buf was called, but a print callback was specified, so no internal buffer is available"
49
+ when 7 # yajl_gen_invalid_string
50
+ raise FFI_Yajl::EncodeError, "Invalid UTF-8 string: cannot encode to UTF-8"
49
51
  else
50
- raise FFI_Yajl::EncodeError, "Unknown YAJL Error, please report this as a bug"
52
+ raise FFI_Yajl::EncodeError, "Unknown YAJL Error (#{status}), please report this as a bug"
51
53
  end
52
54
  end
53
55
  end
data/lib/ffi_yajl/ext.rb CHANGED
@@ -4,14 +4,17 @@ require 'ffi_yajl/encoder'
4
4
  require 'ffi_yajl/parser'
5
5
  require 'ffi'
6
6
  require 'libyajl2'
7
+ require 'ffi_yajl/platform'
7
8
 
8
9
  module FFI_Yajl
10
+ extend FFI_Yajl::Platform
11
+
9
12
  # FIXME: DRY with ffi_yajl/ffi.rb
10
13
  # FIXME: extract map_library_name from FFI and stop requiring it at the top level
11
14
  # so that the C-library can be installed without FFI
12
15
  libname = ::FFI.map_library_name("yajl")
13
16
  # awful windows patch, but there is an open issue to entirely replace FFI.map_library_name already
14
- libname = "libyajl.so" if libname == "yajl.dll"
17
+ libname = "libyajl.so" if windows?
15
18
  libpath = File.expand_path(File.join(Libyajl2.opt_path, libname))
16
19
  libpath.gsub!(/dylib/, 'bundle')
17
20
  libpath = ::FFI.map_library_name("yajl") unless File.exist?(libpath)
@@ -0,0 +1,7 @@
1
+ module FFI_Yajl
2
+ module Platform
3
+ def windows?
4
+ !!(RUBY_PLATFORM =~ /mswin|mingw|cygwin|windows/)
5
+ end
6
+ end
7
+ end
@@ -1,3 +1,3 @@
1
1
  module FFI_Yajl
2
- VERSION = "1.3.1"
2
+ VERSION = "1.4.0"
3
3
  end
@@ -5,7 +5,9 @@ require 'date'
5
5
 
6
6
  describe "FFI_Yajl::Encoder" do
7
7
 
8
- let(:encoder) { FFI_Yajl::Encoder.new }
8
+ let(:options) { {} }
9
+
10
+ let(:encoder) { FFI_Yajl::Encoder.new(options) }
9
11
 
10
12
  it "encodes hashes in keys as strings", :ruby_gte_193 => true do
11
13
  ruby = { {'a' => 'b'} => 2 }
@@ -146,4 +148,28 @@ describe "FFI_Yajl::Encoder" do
146
148
  end
147
149
  end
148
150
 
151
+ context "when encoding invalid utf-8" do
152
+ ruby = {
153
+ "automatic"=>{
154
+ "etc"=>{
155
+ "passwd"=>{
156
+ "root"=>{"dir"=>"/root", "gid"=>0, "uid"=>0, "shell"=>"/bin/sh", "gecos"=>"Elan Ruusam\xc3\xa4e"},
157
+ "glen"=>{"dir"=>"/home/glen", "gid"=>500, "uid"=>500, "shell"=>"/bin/bash", "gecos"=>"Elan Ruusam\xE4e"},
158
+ }
159
+ },
160
+ },
161
+ }
162
+
163
+ it "raises an error on invalid json" do
164
+ expect{ encoder.encode(ruby) }.to raise_error(FFI_Yajl::EncodeError, "Invalid UTF-8 string: cannot encode to UTF-8")
165
+ end
166
+
167
+ context "when validate_utf8 is off" do
168
+ let(:options) { { :validate_utf8 => false } }
169
+
170
+ it "does not raise an error" do
171
+ expect{ encoder.encode(ruby) }.not_to raise_error
172
+ end
173
+ end
174
+ end
149
175
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ffi-yajl
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lamont Granquist
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-25 00:00:00.000000000 Z
11
+ date: 2015-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -156,6 +156,7 @@ files:
156
156
  - lib/ffi_yajl/ffi/parser.rb
157
157
  - lib/ffi_yajl/json_gem.rb
158
158
  - lib/ffi_yajl/parser.rb
159
+ - lib/ffi_yajl/platform.rb
159
160
  - lib/ffi_yajl/version.rb
160
161
  - spec/ffi_yajl/encoder_spec.rb
161
162
  - spec/ffi_yajl/json_gem_spec.rb
@@ -181,7 +182,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
181
182
  version: '0'
182
183
  requirements: []
183
184
  rubyforge_project:
184
- rubygems_version: 2.2.2
185
+ rubygems_version: 2.4.3
185
186
  signing_key:
186
187
  specification_version: 4
187
188
  summary: Ruby FFI wrapper around YAJL 2.x