# Copyright (c) 2015 Lamont Granquist# Copyright (c) 2015 Chef Software, Inc.## Permission is hereby granted, free of charge, to any person obtaining# a copy of this software and associated documentation files (the# "Software"), to deal in the Software without restriction, including# without limitation the rights to use, copy, modify, merge, publish,# distribute, sublicense, and/or sell copies of the Software, and to# permit persons to whom the Software is furnished to do so, subject to# the following conditions:## The above copyright notice and this permission notice shall be# included in all copies or substantial portions of the Software.## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.moduleFFI_YajlclassEncodeError<StandardError;endclassEncoderattr_accessor:optsdefencode(obj)# initialization that we can do in pure rubyyajl_gen_opts={}yajl_gen_opts[:yajl_gen_validate_utf8]=@opts[:validate_utf8]==false?false:trueyajl_gen_opts[:yajl_gen_beautify]=falseyajl_gen_opts[:yajl_gen_indent_string]=" "ifopts[:pretty]yajl_gen_opts[:yajl_gen_beautify]=trueyajl_gen_opts[:yajl_gen_indent_string]=opts[:indent]?opts[:indent]:" "end# call either the ext or ffi hookstr=do_yajl_encode(obj,yajl_gen_opts,opts)# we can skip cleaning the whole string for utf-8 issues if we have yajl validate as we gostr.force_encoding("UTF-8")unlessyajl_gen_opts[:yajl_gen_validate_utf8]ifstr.respond_to?(:scrub)str.scrub!elsestr.encode!("UTF-16le",undef: :replace,invalid: :replace).encode!("UTF-8")endendstrenddefself.encode(obj,*args)new(*args).encode(obj)enddefinitialize(opts={})@opts=opts@opts||={}enddefself.raise_error_for_status(status,token=nil)# scrub token to valid utf-8 since we may be issuing an exception on an invalid utf-8 tokentoken=token.to_s.force_encoding("UTF-8")iftoken.respond_to?(:scrub)token.scrub!elsetoken.encode!("UTF-16le",undef: :replace,invalid: :replace).encode!("UTF-8")endcasestatuswhen1# yajl_gen_keys_must_be_stringsraiseFFI_Yajl::EncodeError,"YAJL internal error: attempted use of non-string object as key"when2# yajl_max_depth_exceededraiseFFI_Yajl::EncodeError,"Max nesting depth exceeded"when3# yajl_gen_in_error_stateraiseFFI_Yajl::EncodeError,"YAJL internal error: a generator function (yajl_gen_XXX) was called while in an error state"when4# yajl_gen_generation_completeraiseFFI_Yajl::EncodeError,"YAJL internal error: attempted to encode to an already-complete document"when5# yajl_gen_invalid_numberraiseFFI_Yajl::EncodeError,"Invalid number: cannot encode Infinity, -Infinity, or NaN"when6# yajl_gen_no_bufraiseFFI_Yajl::EncodeError,"YAJL internal error: yajl_gen_get_buf was called, but a print callback was specified, so no internal buffer is available"when7# yajl_gen_invalid_stringraiseFFI_Yajl::EncodeError,"Invalid UTF-8 string '#{token}': cannot encode to UTF-8"elseraiseFFI_Yajl::EncodeError,"Unknown YAJL Error (#{status}), please report this as a bug"endendendend