Sha256: f022e060a97156e47116540586d25dbc36f36b793ad37b62253059f76d0cfb2c

Contents?: true

Size: 839 Bytes

Versions: 56

Compression:

Stored size: 839 Bytes

Contents

# frozen_string_literal: true

module Appsignal
  module Utils
    class JSON
      class << self
        def generate(body)
          ::JSON.generate(jsonify(body))
        end

        private

        def jsonify(value)
          case value
          when String
            encode_utf8(value)
          when Numeric, NilClass, TrueClass, FalseClass
            value
          when Hash
            value.each_with_object({}) do |(k, v), hash|
              hash[jsonify(k)] = jsonify(v)
            end
          when Array
            value.map { |v| jsonify(v) }
          else
            jsonify(value.to_s)
          end
        end

        def encode_utf8(value)
          value.encode(
            "utf-8",
            :invalid => :replace,
            :undef   => :replace
          )
        end
      end
    end
  end
end

Version data entries

56 entries across 56 versions & 1 rubygems

Version Path
appsignal-4.0.4-java lib/appsignal/utils/json.rb
appsignal-4.0.4 lib/appsignal/utils/json.rb
appsignal-4.0.3-java lib/appsignal/utils/json.rb
appsignal-4.0.3 lib/appsignal/utils/json.rb
appsignal-4.0.2-java lib/appsignal/utils/json.rb
appsignal-4.0.2 lib/appsignal/utils/json.rb
appsignal-4.0.1-java lib/appsignal/utils/json.rb
appsignal-4.0.1 lib/appsignal/utils/json.rb
appsignal-4.0.0-java lib/appsignal/utils/json.rb
appsignal-4.0.0 lib/appsignal/utils/json.rb
appsignal-3.13.1-java lib/appsignal/utils/json.rb
appsignal-3.13.1 lib/appsignal/utils/json.rb
appsignal-3.13.1.alpha.1-java lib/appsignal/utils/json.rb
appsignal-3.13.1.alpha.1 lib/appsignal/utils/json.rb
appsignal-4.0.0.beta.2-java lib/appsignal/utils/json.rb
appsignal-4.0.0.beta.2 lib/appsignal/utils/json.rb
appsignal-4.0.0.beta.1-java lib/appsignal/utils/json.rb
appsignal-4.0.0.beta.1 lib/appsignal/utils/json.rb
appsignal-3.13.0-java lib/appsignal/utils/json.rb
appsignal-3.13.0 lib/appsignal/utils/json.rb