Class: TJSON::DataType::Binary16

Inherits:
Scalar show all
Defined in:
lib/tjson/datatype/binary.rb

Overview

Base16-serialized binary data

Constant Summary

Constants inherited from TJSON::DataType

TAGS

Instance Method Summary collapse

Methods inherited from Scalar

#inspect

Methods inherited from TJSON::DataType

[], encode, identify_type, parse

Instance Method Details

#decode(str) ⇒ Object

Raises:



11
12
13
14
15
16
17
# File 'lib/tjson/datatype/binary.rb', line 11

def decode(str)
  raise TJSON::TypeError, "expected String, got #{str.class}: #{str.inspect}" unless str.is_a?(::String)
  raise TJSON::ParseError, "base16 must be lower case: #{str.inspect}" if str =~ /[A-F]/
  raise TJSON::ParseError, "invalid base16: #{str.inspect}" unless str =~ /\A[a-f0-9]*\z/

  [str].pack("H*")
end

#encode(binary) ⇒ Object



19
20
21
# File 'lib/tjson/datatype/binary.rb', line 19

def encode(binary)
  binary.unpack("H*").first
end

#tagObject



7
8
9
# File 'lib/tjson/datatype/binary.rb', line 7

def tag
  "d16"
end