class TJSON::DataType::Array

TJSON arrays

Public Class Methods

identify_type(array) click to toggle source

Determine the type of a Ruby array (for serialization)

# File lib/tjson/datatype/array.rb, line 8
def self.identify_type(array)
  inner_type = nil

  array.each do |elem|
    t = TJSON::DataType.identify_type(elem)
    inner_type ||= t
    raise TJSON::TypeError, "array contains heterogenous types: #{array.inspect}" unless inner_type == t
  end

  new(inner_type)
end

Public Instance Methods

decode(array) click to toggle source
# File lib/tjson/datatype/array.rb, line 24
def decode(array)
  raise TJSON::TypeError, "expected Array, got #{array.class}" unless array.is_a?(::Array)

  return array.map! { |o| @inner_type.decode(o) } if @inner_type
  return array if array.empty?
  raise TJSON::ParseError, "no inner type specified for non-empty array: #{array.inspect}"
end
encode(array) click to toggle source
# File lib/tjson/datatype/array.rb, line 32
def encode(array)
  array.map { |o| TJSON::DataType.encode(o) }
end
tag() click to toggle source
# File lib/tjson/datatype/array.rb, line 20
def tag
  "A<#{@inner_type.tag}>"
end