123456789_123456789_123456789_123456789_123456789_

Module: JSON::Ext

Overview

This module holds all the modules/classes that implement JSON's functionality as C extensions.

Class Method Summary

Class Method Details

.coerce_key(key, as_json)

This method is for internal use only.
[ GitHub ]

  
# File 'ext/json/lib/json/ext.rb', line 46

def coerce_key(key, as_json) # :nodoc:
  as_json_called = false

  unless String === key || Symbol === key
    key = as_json.call(key, true)
    as_json_called = true
  end

  if Symbol === key
    key = key.to_s
  end

  unless String === key
    raise GeneratorError.new("#{key.class} not allowed as object key in JSON", key)
  end

  unless valid_encoding?(key)
    if as_json_called
      raise GeneratorError.new("source sequence is illegal/malformed utf-8", key)
    end

    key = as_json.call(key, true)
    unless String === key
      raise GeneratorError.new("#{key.class} not allowed as object key in JSON", key)
    end

    unless Ext.valid_encoding?(key)
      raise GeneratorError.new("source sequence is illegal/malformed utf-8", key)
    end
  end

  key
end

.on_duplicate_key(hash, as_json)

[ GitHub ]

  
# File 'ext/json/lib/json/ext.rb', line 80

def on_duplicate_key(hash, as_json)
  set = {}
  hash.each_key do |key|
    key = coerce_key(key, as_json)
    if set[key]
      raise GeneratorError.new("detected duplicate key #{key.inspect}", key)
    else
      set[key] = true
    end
  end
end

.shareable_lambda(block)

This method is for internal use only.

See additional method definition at line 33.

[ GitHub ]

  
# File 'ext/json/lib/json/ext.rb', line 37

def shareable_lambda(block) # :nodoc:
  Ractor.shareable_lambda(&block)
end

.valid_encoding?(str) ⇒ Boolean

This method is for internal use only.
[ GitHub ]

  
# File 'ext/json/lib/json/ext.rb', line 42

def valid_encoding?(str) # :nodoc:
  str.ascii_only? || (str.encoding == Encoding::UTF_8 && str.valid_encoding?)
end