4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/tolerate_json.rb', line 4
def pretty_print_json(json, indentation_character = ' ')
return json if json.to_s.size < 1
json.gsub!("\r\n", '')
json.gsub!("\n", '')
json.gsub!("\r", '')
if json.match(/[^\n]\}$/)
json.gsub!(/\}$/, "\n}")
json.gsub!(/\}\}/, "}\n}")
end
if json.match(/[^\n]\]$/)
json.gsub!(/\]$/, "\n]")
end
json = json.to_s.gsub(/\{[\s+]\"/, "{\"").gsub(/\{(\s+)\"/, "{\"").gsub(/\"(\s+)\}/, "\"}").gsub(/true(\s+)\}/, "true}").gsub(/false(\s+)\}/, "false}").gsub(/\",(\s+)\"/, '","')
json = json.gsub("},", "},\n").gsub("],", "],\n").gsub("{[", "{\n[").gsub("}]", "}\n]").gsub("[{", "[\n{").gsub("]}", "]\n}").gsub("{\"", "{\n\"").gsub("\"}", "\"\n}").gsub("\",\"", "\",\n\"").gsub(":true,\"", ":true,\n\"").gsub(":false,\"", ":false,\n\"").gsub(":true}", ":true\n}").gsub(":false}", ":false\n}").gsub(/\:(\d+),\"/) { ":#{$1},\n\"" }.gsub(/\:(\d+),\"/,":\1,\n\"")
output = []
indent_level = 0
json.split("\n").each do |s|
indent_level -= 1 if ["]", "}"].include?(s.split('').first) && indent_level > 0
output << (indentation_character*indent_level) + s
if ["{", "["].include?(s.split('').last)
indent_level += 1
next
end
if ["{", "["].include?(s.split('').first)
indent_level += 1
next
end
end
output.join("\n")+"\n"
end
|