module JSON::TruffleRuby::Generator::GeneratorMethods::Hash
Public Instance Methods
Source
# File lib/json/truffle_ruby/generator.rb, line 504 def to_json(state = nil, *) state = State.from_state(state) depth = state.depth state.check_max_nesting json_transform(state) ensure state.depth = depth end
Private Instance Methods
Source
# File lib/json/truffle_ruby/generator.rb, line 515 def json_transform(state) depth = state.depth += 1 if empty? state.depth -= 1 return '{}' end delim = ",#{state.object_nl}" result = +"{#{state.object_nl}" first = true key_type = nil indent = !state.object_nl.empty? each { |key, value| if first key_type = key.class else if key_type && !state.allow_duplicate_key? && key_type != key.class key_type = nil # stop checking JSON.send(:on_mixed_keys_hash, self, state.allow_duplicate_key? == false) end result << delim end result << state.indent * depth if indent if state.strict? if state.as_json && (!Generator.native_key?(key) || !Generator.valid_encoding?(key)) key = state.as_json.call(key, true) end unless Generator.native_key?(key) raise GeneratorError.new("#{key.class} not allowed as object key in JSON", key) end unless Generator.valid_encoding?(key) raise GeneratorError.new("source sequence is illegal/malformed utf-8", key) end end key_str = key.to_s if key_str.is_a?(String) key_json = key_str.to_json(state) else raise TypeError, "#{key.class}#to_s returns an instance of #{key_str.class}, expected a String" end result = +"#{result}#{key_json}#{state.space_before}:#{state.space}" if state.strict? && !Generator.native_type?(value) if state.as_json value = state.as_json.call(value, false) unless Generator.native_type?(value) raise GeneratorError.new("#{value.class} returned by #{state.as_json} not allowed in JSON", value) end result << value.to_json(state) state.depth = depth else raise GeneratorError.new("#{value.class} not allowed in JSON", value) end elsif value.respond_to?(:to_json) result << value.to_json(state) state.depth = depth else result << %{"#{String(value)}"} end first = false } depth -= 1 unless first result << state.object_nl result << state.indent * depth if indent end result << '}' result end