Sha256: 7d4b55da48fa8fec889589ee3eadb03609551094247c0eae8522b42e6fb40ee8

Contents?: true

Size: 752 Bytes

Versions: 1

Compression:

Stored size: 752 Bytes

Contents

# frozen_string_literal: true

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

      def decode(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 encode(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.encode(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.5.0 lib/tjson/datatype/object.rb