Module: JSON::TruffleRuby::Generator::GeneratorMethods::String
- Defined in:
- lib/json/truffle_ruby/generator.rb
Instance Method Summary collapse
-
#to_json(state = nil, *args) ⇒ Object
This string should be encoded with UTF-8 A call to this method returns a JSON string encoded with UTF16 big endian characters as \u????.
Instance Method Details
#to_json(state = nil, *args) ⇒ Object
This string should be encoded with UTF-8 A call to this method returns a JSON string encoded with UTF16 big endian characters as \u????.
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 |
# File 'lib/json/truffle_ruby/generator.rb', line 776 def to_json(state = nil, *args) state = State.from_state(state) string = self if state.strict? && state.as_json unless Generator.valid_encoding?(string) coerced = state.as_json.call(string, false) unless coerced.equal?(string) # as_json is only called once, so a new string that is still # invalid raises instead of calling it again. coerced = coerced.name if coerced.is_a?(::Symbol) unless coerced.is_a?(::String) return coerced.to_json(state, *args) end unless Generator.valid_encoding?(coerced) raise GeneratorError.new("source sequence is illegal/malformed utf-8", self) end string = coerced end end end if string.encoding == ::Encoding::UTF_8 unless string.valid_encoding? raise GeneratorError.new("source sequence is illegal/malformed utf-8", self) end else string = string.encode(::Encoding::UTF_8) end if state.ascii_only? %("#{JSON::TruffleRuby::Generator.utf8_to_json_ascii(string, state.script_safe)}") else %("#{JSON::TruffleRuby::Generator.utf8_to_json(string, state.script_safe)}") end rescue Encoding::UndefinedConversionError => error raise ::JSON::GeneratorError.new(error., self) end |