Class: TJSON::DataType::Binary64

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

Overview

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



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/tjson/datatype/binary.rb', line 50

def decode(str)
  raise TJSON::TypeError, "expected String, got #{str.class}: #{str.inspect}" unless str.is_a?(::String)
  raise TJSON::ParseError, "base64url only: #{str.inspect}" if str =~ %r{\+|\/}
  raise TJSON::ParseError, "padding disallowed: #{str.inspect}" if str.include?("=")
  raise TJSON::ParseError, "invalid base64url: #{str.inspect}" unless str =~ /\A[A-Za-z0-9\-_]*\z/

  # Add padding, as older Rubies (< 2.3) require it
  str = str.ljust((str.length + 3) & ~3, "=") if (str.length % 4).nonzero?

  ::Base64.urlsafe_decode64(str)
end

#encode(binary) ⇒ Object



62
63
64
# File 'lib/tjson/datatype/binary.rb', line 62

def encode(binary)
  Base64.urlsafe_encode64(binary).delete("=")
end

#tagObject



46
47
48
# File 'lib/tjson/datatype/binary.rb', line 46

def tag
  "d64"
end