Module: CBOR
- Defined in:
- lib/libcbor.rb,
lib/libcbor/tag.rb,
lib/libcbor/item.rb,
lib/libcbor/cache.rb,
lib/libcbor/helpers.rb,
lib/libcbor/version.rb,
lib/libcbor/streaming.rb,
lib/libcbor/byte_string.rb,
lib/libcbor/simple_value.rb,
lib/libcbor/inner/lib_cbor.rb,
lib/libcbor/streaming/encoder.rb,
lib/libcbor/streaming/buffered_decoder.rb,
lib/libcbor/streaming/callback_simplifier.rb
Overview
Provides encoding, decoding, and streaming interaction with CBOR data. Please refer to README for an overview and a short tutorial.
Defined Under Namespace
Modules: ArrayHelper, ByteStringHelper, FalseClassHelper, FixnumHelper, FloatHelper, HashHelper, LibC, LibCBOR, NilClassHelper, Streaming, StringHelper, TagHelper, TrueClassHelper Classes: ByteString, Cache, DecodingError, Item, SimpleValue, Tag
Constant Summary collapse
- VERSION =
The current version. It is completely independent of libcbor’s version
'0.1.0'
Class Method Summary collapse
-
.decode(data) ⇒ Fixnum, ...
Deserialize CBOR.
-
.encode(object) ⇒ String
Encodes the given object to CBOR representation Custom semantics are supported - the object has to to provide the
to_cbor
method In case CBOR.method_name is notto_cbor
, provide the custom method instead. -
.load!(name = nil) ⇒ Array
Load the extensions for the core classes.
-
.method_name ⇒ Symbol
The name of the method that is used for encoding objects.
Class Method Details
.decode(data) ⇒ Fixnum, ...
Deserialize CBOR
61 62 63 64 65 66 67 |
# File 'lib/libcbor.rb', line 61 def self.decode(data) res = FFI::MemoryPointer.new LibCBOR::CborLoadResult Item.new( LibCBOR.cbor_load(FFI::MemoryPointer.from_string(data), data.bytes.count, res). tap { |ptr| raise DecodingError if ptr.null? } ).value end |
.encode(object) ⇒ String
Encodes the given object to CBOR representation Custom semantics are supported - the object has to to provide the to_cbor
method In case method_name is not to_cbor
, provide the custom method instead.
32 33 34 |
# File 'lib/libcbor.rb', line 32 def self.encode(object) object.to_cbor end |
.load!(name = nil) ⇒ Array
Load the extensions for the core classes
47 48 49 50 51 52 53 54 |
# File 'lib/libcbor.rb', line 47 def self.load!(name = nil) @@method_name = name || :to_cbor %w{Fixnum Float Array Hash String TrueClass FalseClass NilClass Tag ByteString}.each do |klass| kklass = const_get(klass) kklass.send(:include, const_get('::CBOR::' + klass + 'Helper')) kklass.send(:alias_method, method_name, :__libcbor_to_cbor) end end |
.method_name ⇒ Symbol
The name of the method that is used for encoding objects. Defaults to :to_cbor
39 40 41 |
# File 'lib/libcbor.rb', line 39 def self.method_name @@method_name end |