Class: TJSON::DataType::Object

Inherits:
NonScalar show all
Defined in:
lib/tjson/datatype/object.rb

Overview

TJSON objects

Constant Summary

Constants inherited from TJSON::DataType

TAGS

Instance Attribute Summary

Attributes inherited from NonScalar

#inner_type

Instance Method Summary collapse

Methods inherited from NonScalar

#==, #initialize, #scalar?

Methods inherited from TJSON::DataType

[], generate, identify_type, parse

Constructor Details

This class inherits a constructor from TJSON::DataType::NonScalar

Instance Method Details

#convert(obj) ⇒ Object

Raises:



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

def convert(obj)
  raise TJSON::TypeError, "expected TJSON::Object, got #{obj.class}" unless obj.is_a?(TJSON::Object)

  # Objects handle their own member conversions
  obj
end

#generate(obj) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/tjson/datatype/object.rb', line 18

def generate(obj)
  members = obj.map do |k, v|
    raise TypeError, "expected String for key, got #{k.class}" unless k.is_a?(::String)
    type = TJSON::DataType.identify_type(v)
    ["#{k}:#{type.tag}", TJSON::DataType.generate(v)]
  end

  Hash[members]
end

#inspectObject



28
29
30
# File 'lib/tjson/datatype/object.rb', line 28

def inspect
  "#<#{self.class}>"
end

#tagObject



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

def tag
  "O"
end