Class: TJSON::DataType::Binary32

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

Overview

Base32-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:



30
31
32
33
34
35
36
37
# File 'lib/tjson/datatype/binary.rb', line 30

def decode(str)
  raise TJSON::TypeError, "expected String, got #{str.class}: #{str.inspect}" unless str.is_a?(::String)
  raise TJSON::ParseError, "base32 must be lower case: #{str.inspect}" if str =~ /[A-Z]/
  raise TJSON::ParseError, "padding disallowed: #{str.inspect}" if str.include?("=")
  raise TJSON::ParseError, "invalid base32: #{str.inspect}" unless str =~ /\A[a-z2-7]*\z/

  ::Base32.decode(str.upcase).force_encoding(Encoding::BINARY)
end

#encode(binary) ⇒ Object



39
40
41
# File 'lib/tjson/datatype/binary.rb', line 39

def encode(binary)
  Base32.encode(binary).downcase.delete("=")
end

#tagObject



26
27
28
# File 'lib/tjson/datatype/binary.rb', line 26

def tag
  "d32"
end