Sha256: f54549d9f875ab6f5e3e1cb7ea3cfe33c551738585cd4a97274c784ca2e356b2

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

module TJSON
  class DataType
    # TJSON sets
    class Set < NonScalar
      # Determine the type of a Ruby array (for serialization)
      def self.identify_type(array)
        inner_type = nil

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

        new(inner_type)
      end

      def tag
        "S<#{@inner_type.tag}>"
      end

      def decode(array)
        raise TJSON::TypeError, "expected Array, got #{array.class}" unless array.is_a?(::Array)

        if @inner_type
          result = ::Set.new(array.map { |o| @inner_type.decode(o) })
          raise TJSON::ParseError, "set contains duplicate items" if result.size < array.size
          return result
        end

        return ::Set.new if array.empty?
        raise TJSON::ParseError, "no inner type specified for non-empty set: #{array.inspect}"
      end

      def encode(set)
        set.map { |o| TJSON::DataType.generate(o) }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tjson-0.5.0 lib/tjson/datatype/set.rb