module Qpid::Proton::Codec::STRING

Public Class Methods

put(data, value) click to toggle source
# File lib/codec/mapping.rb, line 128
def put(data, value)
  # if we have a symbol then convert it to a string
  value = value.to_s if value.is_a?(Symbol)

  isutf = false

  if value.is_a?(Types::UTFString)
    isutf = true
  else
    # For Ruby 1.8 we will just treat all strings as binary.
    # For Ruby 1.9+ we can check the encoding first to see what it is
    if RUBY_VERSION >= "1.9"
      # If the string is ASCII-8BIT then treat is as binary. Otherwise,
      # try to convert it to UTF-8 and, if successful, send as that.
      if value.encoding != Encoding::ASCII_8BIT &&
          value.encode(Encoding::UTF_8).valid_encoding?
        isutf = true
      end
    end
  end

  data.string = value if isutf
  data.binary = value if !isutf
end