Class: Jansson::FFI::Entity Private

Inherits:
FFI::Struct
  • Object
show all
Defined in:
lib/jansson/ffi.rb,
lib/jansson/ffi/ext/entity.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/jansson/ffi/ext/entity.rb', line 58

def self.from(value)
  case value
  when NilClass
    FFI.json_null
  when TrueClass
    FFI.json_true
  when FalseClass
    FFI.json_false
  when Integer
    FFI.json_integer(value)
  when Float
    FFI.json_real(value)
  when String
    value = value.force_encoding(Encoding::UTF_8)
    FFI.json_stringn_nocheck(value, value.bytesize)
  when Symbol
    value = value.to_s.force_encoding(Encoding::UTF_8)
    FFI.json_stringn_nocheck(value, value.bytesize)
  when Array
    array = FFI.json_array
    value.each do |item|
      FFI.json_array_append_new(array, from(item))
    end
    array
  when Hash
    object = FFI.json_object
    value.each do |key, value|
      FFI.json_object_set_new(object, key.to_s, from(value))
    end
    object
  end
end

.from_s(string, flags = LOAD_DECODE_ANY | LOAD_ALLOW_NUL) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



17
18
19
20
21
# File 'lib/jansson/ffi/ext/entity.rb', line 17

def self.from_s(string, flags = LOAD_DECODE_ANY | LOAD_ALLOW_NUL)
  error  = FFI::Error.new
  entity = FFI.json_loads(string, flags, error)
  entity.pointer.null? ? error : entity
end

Instance Method Details

#free!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



6
7
8
# File 'lib/jansson/ffi/ext/entity.rb', line 6

def free!
  FFI.json_delete(self) if 0 >= (self[:refcount] -= 1)
end

#to_rubyObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/jansson/ffi/ext/entity.rb', line 27

def to_ruby
  case type
  when :null
    nil
  when :true
    true
  when :false
    false
  when :integer
    FFI.json_integer_value(self)
  when :real
    FFI.json_real_value(self)
  when :string
    FFI.json_string_value(self).read_string(FFI.json_string_length(self))
  when :array
    FFI.json_array_size(self).times.map do |index|
      FFI.json_array_get(self, index).to_ruby
    end
  when :object
    object = {}
    iter = FFI.json_object_iter(self)
    while (key = FFI.json_object_iter_key(iter)) && \
        (value = FFI.json_object_iter_value(iter))
      object[key] = value.to_ruby
      iter = FFI.json_object_iter_next(self, iter)
    end
    object
  else raise NotImplementedError
  end
end

#to_s(flags = DUMP_ENCODE_ANY | DUMP_PRESERVE_ORDER) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



10
11
12
13
14
15
# File 'lib/jansson/ffi/ext/entity.rb', line 10

def to_s(flags = DUMP_ENCODE_ANY | DUMP_PRESERVE_ORDER)
  ptr = FFI.json_dumps(self, flags)
  str = ptr.read_string
  FFI.free(ptr)
  str
end

#typeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



23
24
25
# File 'lib/jansson/ffi/ext/entity.rb', line 23

def type
  self[:type]
end