Sha256: f9464c3a1b60037a3390becebd765aa4e2061d11ee9cb7fc990508b02329842c

Contents?: true

Size: 757 Bytes

Versions: 1

Compression:

Stored size: 757 Bytes

Contents

# frozen_string_literal: true

module TJSON
  class DataType
    # TJSON objects
    class Object < NonScalar
      def tag
        "O"
      end

      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

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

        Hash[members]
      end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tjson-0.4.0 lib/tjson/datatype/object.rb