Class: Takagi::Serialization::CborSerializer
- Defined in:
- lib/takagi/serialization/cbor_serializer.rb
Overview
CBOR serializer (RFC 8949)
Handles application/cbor content-format (code 60). Encodes Ruby objects to CBOR binary format and decodes CBOR to Ruby objects.
Uses Takagi’s built-in CBOR implementation for zero external dependencies.
Instance Method Summary collapse
-
#binary? ⇒ Boolean
CBOR is a binary format.
-
#content_format_code ⇒ Integer
CoAP content-format code for CBOR.
-
#content_type ⇒ String
MIME type for CBOR.
-
#decode(bytes) ⇒ Object
Decode CBOR bytes to Ruby object.
-
#encode(data) ⇒ String
Encode Ruby object to CBOR bytes.
Methods inherited from Base
Instance Method Details
#binary? ⇒ Boolean
CBOR is a binary format
87 88 89 |
# File 'lib/takagi/serialization/cbor_serializer.rb', line 87 def binary? true end |
#content_format_code ⇒ Integer
CoAP content-format code for CBOR
80 81 82 |
# File 'lib/takagi/serialization/cbor_serializer.rb', line 80 def content_format_code CoAP::Registries::ContentFormat::CBOR end |
#content_type ⇒ String
MIME type for CBOR
73 74 75 |
# File 'lib/takagi/serialization/cbor_serializer.rb', line 73 def content_type 'application/cbor' end |
#decode(bytes) ⇒ Object
Decode CBOR bytes to Ruby object
60 61 62 63 64 65 66 67 68 |
# File 'lib/takagi/serialization/cbor_serializer.rb', line 60 def decode(bytes) return nil if bytes.nil? || bytes.empty? CBOR::Decoder.decode(bytes) rescue CBOR::DecodeError => e raise DecodeError, "CBOR decoding failed: #{e.}" rescue StandardError => e raise DecodeError, "CBOR decoding error: #{e.}" end |
#encode(data) ⇒ String
Encode Ruby object to CBOR bytes
39 40 41 42 43 44 45 46 47 |
# File 'lib/takagi/serialization/cbor_serializer.rb', line 39 def encode(data) return ''.b if data.nil? || data == '' CBOR::Encoder.encode(data) rescue CBOR::EncodeError => e raise EncodeError, "CBOR encoding failed: #{e.}" rescue StandardError => e raise EncodeError, "CBOR encoding error: #{e.}" end |