GithubHelp home page GithubHelp logo

isabella232 / ffi-yajl Goto Github PK

View Code? Open in Web Editor NEW

This project forked from chef/ffi-yajl

0.0 0.0 0.0 594 KB

Ruby FFI gem wrapper around yajl2 library

License: MIT License

Ruby 81.03% C 17.66% PowerShell 0.90% Shell 0.40%

ffi-yajl's Introduction

FFI YAJL

Build status Gem Version

Umbrella Project: Chef Foundation

Project State: Active

Issues Response Time Maximum: 14 days

Pull Request Response Time Maximum: 14 days

ffi-yajl is a Ruby adapter for the yajl JSON parser/generator library. ffi-yajl supports multiple Ruby C extension mechanisms, including both MRI native extensions and FFI in order to be compatible with as many Ruby implementations as possible while providing good performance where possible.

How to Install

Install from the command-line:

gem install ffi-yajl

Or use a Gemfile:

gem 'ffi-yajl'

Supported Ruby VMs:

  • Ruby 2.2+ and compatible rbx or jruby

Supported Distros:

  • Ubuntu 10.04 through 14.10
  • Debian 7.x
  • RHEL/CentOS/Oracle 5.x/6.x/7.x
  • Solaris 9/10/11 (gcc, sun compiler untested)
  • AIX 6.x/7.x (gcc or xlc)
  • Windows 2008r2/2012 (and Win2k/2k3 and consumer versions should work)

Basic Usage

Start by requiring it:

require 'ffi_yajl'

You can encode and parse with class objects:

options_hash = {}
json = FFI_Yajl::Encoder.encode( {"foo"=>["bar","baz"]}, options_hash )
hash = FFI_Yajl::Parser.parse( json, options_hash )

Or you can be more object oriented:

options_hash = {}
encoder = FFI_Yajl::Encoder.new( options_hash )
json = encoder.encode( {"foo"=>["bar","baz"]} )
parser = FFI_Yajl::Parser.new( options_hash )
hash = parser.parse( json )

Parser Options

  • :check_utf8
  • :dont_validate_strings
  • :symbolize_keys (default = false): JSON keys are parsed into symbols instead of strings.
  • :symbolize_names (default = false): Alias for :symbolize_keys.
  • :allow_trailing_garbage
  • :allow_multiple_values
  • :allow_partial_values
  • :unique_key_checking (default = false): Will raise an exception if keys are repeated in hashes in the input JSON. Without this, repeated keys will silently replace the previous key.

Encoder Options

  • :pretty (default = false): Produces more human readable 'pretty' output.
  • :validate_utf8 (default = true): Will raise an exception when trying to encode strings that are invalid UTF-8. When set to false this still will produce valid UTF-8 JSON but will replace invalid characters.

Forcing FFI or C Extension

You can set the environment variable FORCE_FFI_YAJL to ext or ffi to control if the C extension or FFI bindings are used.

Yajl Library Packaging

This library prefers to use the embedded yajl 2.x C library packaged in the libyajl2 gem. In order to use the operating system yajl library (which must be yajl 2.x) the environment variable USE_SYSTEM_LIBYAJL2 can be set before installing or bundling libyajl2. This will force the libyajl2 gem to skip compiling its embedded library and the ffi-yajl gem will fallback to using the system yajl library.

No JSON Gem Compatiblity layer

This library does not offer a feature to patch #to_json methods into all the ruby classes similarly to the JSON gem or yajl-ruby's yajl/json_gem compatibility API. The differences in encoding between the JSON gem and the Yajl C library can produce different output on different systems and makes testing brittle. Such a feature will not be included. It was removed in this pull request and could be easily extracted to its own gem (we have no interest in maintaining that gem):

https://github.com/chef/ffi-yajl/pull/47/files

Why This Instead of X?

yajl is the only JSON library we've found that has error messages that meet our requirements. The stdlib json gem and oj (at the time we started this project) have error messages like "invalid token at byte 1234," which are probably fine for server use, but in chef we frequently deal with user-written JSON documents, which means we need a good user experience when encountering malformed JSON.

We previously used brianmario's yajl-ruby project, but we wanted to be able to fallback to using FFI bindings to the C code (so we could support non-MRI rubies) and we also needed some bug fixes in yajl2, but the maintainer wasn't able to devote enough time to the project to make these updates in a timeframe that worked for us.

Thanks

This was initially going to be a clean rewrite of an ffi ruby wrapper around yajl2, but as it progressed more and more code was pulled in from brianmario's existing yajl-ruby gem, particularly all the c extension code, lots of specs and the benchmarks. And the process of writing this would have been much more difficult without being able to draw heavily from already solved problems in yajl-ruby.

License

Given that this draws heavily from the yajl-ruby sources, and could be considered a derivative work, the MIT License from that project has been preserved and this source code has deliberately not been dual licensed under Chef's typical Apache License. See the LICENSE file in this project.

ffi-yajl's People

Contributors

aaronlasseigne avatar analogj avatar chef-ci avatar chef-expeditor[bot] avatar danielsdeleo avatar electrawn avatar glensc avatar kou avatar lamont-granquist avatar lebrinkma avatar lrdcasimir avatar olleolleolle avatar skeshari12 avatar tas50 avatar tduffield avatar thommay avatar tyler-ball avatar voxik avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.