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

Class Method Details

.decode(data) ⇒ Fixnum, ...

Deserialize CBOR

Parameters:

  • data (String)

Returns:

  • (Fixnum, String, Float, Array, Map, Tag, TrueClass, FalseClass, NilClass)

    resulting object

Raises:



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.

Parameters:

  • object (Fixnum, String, Float, Array, Map, Tag, TrueClass, FalseClass, NilClass, #to_cbor)

    the object to encode

Returns:

  • (String)

    the CBOR representation



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

Parameters:

  • name (Symbol) (defaults to: nil)

    the name to use for the encoding method. If not provided, to_cbor will be used

Returns:

  • (Array)

    list of the patched 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_nameSymbol

The name of the method that is used for encoding objects. Defaults to :to_cbor

Returns:

  • (Symbol)

    the method name



39
40
41
# File 'lib/libcbor.rb', line 39

def self.method_name
	@@method_name
end