Module: JSON::Ext

Defined in:
lib/json/ext.rb,
lib/json/ext/generator/state.rb,
ext/json/ext/parser/parser.c,
ext/json/ext/generator/generator.c

Overview

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

Defined Under Namespace

Modules: Generator Classes: Parser, ParserConfig

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.coerce_key(key, as_json) ⇒ Object

:nodoc:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File '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) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File '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

.valid_encoding?(str) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


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

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

Instance Method Details

#shareable_lambda(block) ⇒ Object

:nodoc:



33
34
35
# File 'lib/json/ext.rb', line 33

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