summaryrefslogtreecommitdiff
path: root/lib/ffi_yajl/parser.rb
blob: f1b9625f5c3e8280f8937f7258a25429fecbe7a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

module FFI_Yajl
  class ParseError < StandardError; end
  class Parser
    attr_accessor :stack, :key_stack, :key, :finished

    attr_accessor :opts

    #
    # stack used to build up our complex object
    #
    def stack
      @stack ||= Array.new
    end

    #
    # stack to keep track of keys as we create nested hashes
    #
    def key_stack
      @key_stack ||= Array.new
    end

    def self.parse(obj, *args)
      new(*args).parse(obj)
    end

    def initialize(opts = {})
      @opts = opts ? opts.dup : {}
      # JSON gem uses 'symbolize_names' and ruby-yajl supports this as well
      @opts[:symbolize_keys] = true if @opts[:symbolize_names]
    end

    def parse(str)
      # initialization that we can do in pure ruby
      yajl_opts = {}

      yajl_opts[:yajl_allow_comments] = @opts[:allow_comments]

      # XXX: bug-compat with ruby-yajl
      return nil if str == ""

      # call either the ext or ffi hook
      do_yajl_parse(str, yajl_opts)
    end
  end
end