Sha256: 4176e0f5ec7df3cd08628b00e9fbfb19a550170e66d80f8cd446cbdb9b58f0b4
Contents?: true
Size: 1.48 KB
Versions: 60
Compression:
Stored size: 1.48 KB
Contents
module ForestLiana module JsonPrinter def pretty_print json, indentation = "" result = "" if json.kind_of? Array result << "[" is_small = json.length < 3 is_primary_value = false json.each_index do |index| item = json[index] is_primary_value = !item.kind_of?(Hash) && !item.kind_of?(Array) if index == 0 && is_primary_value && !is_small result << "\n#{indentation} " elsif index > 0 && is_primary_value && !is_small result << ",\n#{indentation} " elsif index > 0 result << ", " end result << pretty_print(item, is_primary_value ? "#{indentation} " : indentation); end result << "\n#{indentation}" if is_primary_value && !is_small result << "]" elsif json.kind_of? Hash result << "{\n" is_first = true json = json.stringify_keys json.each do |key, value| result << ",\n" unless is_first is_first = false result << "#{indentation} \"#{key}\": " result << pretty_print(value, "#{indentation} ") end result << "\n#{indentation}}" elsif json.nil? result << "null" elsif !!json == json result << (json ? "true" : "false") elsif json.is_a?(String) || json.is_a?(Symbol) result << "\"#{json.gsub(/"/, '\"')}\"" else result << json.to_s end result end end end
Version data entries
60 entries across 60 versions & 1 rubygems