class Faraday::Request::Json

Doesn’t try to encode bodies that already are in string form.
to JSON MIME-type.
If a request doesn’t have a type but has a body, it sets the Content-type
Processes only requests with matching Content-type or those without a type.
Request middleware that encodes the body as JSON.

def body?(env)

def body?(env)
  body = env[:body]
  case body
  when true, false
    true
  when nil
    # NOTE: nil can be converted to `"null"`, but this middleware doesn't process `nil` for the compatibility.
    false
  else
    !(body.respond_to?(:to_str) && body.empty?)
  end
end

def encode(data)

def encode(data)
  if options[:encoder].is_a?(Array) && options[:encoder].size >= 2
    options[:encoder][0].public_send(options[:encoder][1], data)
  elsif options[:encoder].respond_to?(:dump)
    options[:encoder].dump(data)
  else
    ::JSON.generate(data)
  end
end

def match_content_type(env)

def match_content_type(env)
  return unless process_request?(env)
  env[:request_headers][CONTENT_TYPE] ||= MIME_TYPE
  yield env[:body] unless env[:body].respond_to?(:to_str)
end

def on_request(env)

def on_request(env)
  match_content_type(env) do |data|
    env[:body] = encode(data)
  end
end

def process_request?(env)

def process_request?(env)
  type = request_type(env)
  body?(env) && (type.empty? || type.match?(MIME_TYPE_REGEX))
end

def request_type(env)

def request_type(env)
  type = env[:request_headers][CONTENT_TYPE].to_s
  type = type.split(';', 2).first if type.index(';')
  type
end