tolerate_json 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.rvmrc +2 -0
- data/.travis.yml +11 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +53 -0
- data/Rakefile +11 -0
- data/lib/tolerate_json/version.rb +3 -0
- data/lib/tolerate_json.rb +41 -0
- data/spec/lib/tolerate_json_spec.rb +69 -0
- data/spec/spec_helper.rb +6 -0
- data/tolerate_json.gemspec +24 -0
- metadata +115 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rvmrc
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Matthew Nielsen
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# TolerateJson
|
2
|
+
|
3
|
+
A JSON formatter written in pure Ruby that works "good enough" and has no gem dependencies. 60% of the time, it works every time.
|
4
|
+
|
5
|
+
## Description
|
6
|
+
|
7
|
+
Tolerate_json is a pure-ruby Json formatter that has no outside gem dependencies. It is meant to make JSON nice to look at without requiring a full-blown json parsing library.
|
8
|
+
|
9
|
+
It is a "90%" solution: it works in 90% of use cases, but will almost certainly fall down on complicated examples. When you start getting to that point, it's time to use a real json parsing engine.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'tolerate_json'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install tolerate_json
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
require 'rubygems' # optional on 1.9.3 and up
|
29
|
+
|
30
|
+
require 'tolerate_json'
|
31
|
+
|
32
|
+
include TolderateJson
|
33
|
+
|
34
|
+
puts pretty_print_json('{"foo":{"bar":"baz"}}')
|
35
|
+
|
36
|
+
# Prints:
|
37
|
+
#
|
38
|
+
# {
|
39
|
+
# "foo":{
|
40
|
+
# "bar":"baz"
|
41
|
+
# }
|
42
|
+
# }
|
43
|
+
#
|
44
|
+
```
|
45
|
+
|
46
|
+
|
47
|
+
## Contributing
|
48
|
+
|
49
|
+
1. Fork it
|
50
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
51
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
52
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
53
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require "tolerate_json/version"
|
2
|
+
|
3
|
+
module TolerateJson
|
4
|
+
def pretty_print_json(json, indentation_character = ' ')
|
5
|
+
return json if json.to_s.size < 1
|
6
|
+
|
7
|
+
json.gsub!("\r\n", '')
|
8
|
+
json.gsub!("\n", '')
|
9
|
+
json.gsub!("\r", '')
|
10
|
+
|
11
|
+
if json.match(/[^\n]\}$/)
|
12
|
+
json.gsub!(/\}$/, "\n}")
|
13
|
+
end
|
14
|
+
|
15
|
+
if json.match(/[^\n]\]$/)
|
16
|
+
json.gsub!(/\]$/, "\n]")
|
17
|
+
end
|
18
|
+
|
19
|
+
json = json.to_s.gsub(/\{[\s+]\"/, "{\"").gsub(/\{(\s+)\"/, "{\"").gsub(/\"(\s+)\}/, "\"}").gsub(/true(\s+)\}/, "true}").gsub(/false(\s+)\}/, "false}").gsub(/\",(\s+)\"/, '","')
|
20
|
+
|
21
|
+
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\"")
|
22
|
+
|
23
|
+
output = []
|
24
|
+
|
25
|
+
indent_level = 0
|
26
|
+
json.split("\n").each do |s|
|
27
|
+
indent_level -= 1 if ["]", "}"].include?(s.split('').first) && indent_level > 0
|
28
|
+
output << (indentation_character*indent_level) + s
|
29
|
+
if ["{", "["].include?(s.split('').last)
|
30
|
+
indent_level += 1
|
31
|
+
next
|
32
|
+
end
|
33
|
+
|
34
|
+
if ["{", "["].include?(s.split('').first)
|
35
|
+
indent_level += 1
|
36
|
+
next
|
37
|
+
end
|
38
|
+
end
|
39
|
+
output.join("\n")+"\n"
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'tolerate_json'
|
3
|
+
|
4
|
+
class T
|
5
|
+
include TolerateJson
|
6
|
+
end
|
7
|
+
|
8
|
+
describe TolerateJson do
|
9
|
+
let(:t) { T.new }
|
10
|
+
|
11
|
+
describe "pretty_print_json" do
|
12
|
+
|
13
|
+
it "should format simple json" do
|
14
|
+
unformatted = '{"foo":"bar"}'
|
15
|
+
formatted= "{\n \"foo\":\"bar\"\n}\n"
|
16
|
+
|
17
|
+
expect(t.pretty_print_json(unformatted)).to eq(formatted)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should format complex json" do
|
21
|
+
unformatted = '{"foo":"bar","boolean":true,"Numbers":12345,"baz":"quz"}'
|
22
|
+
formatted= "{\n \"foo\":\"bar\",\n \"boolean\":true,\n \"Numbers\":12345,\n \"baz\":\"quz\"\n}\n"
|
23
|
+
|
24
|
+
expect(t.pretty_print_json(unformatted)).to eq(formatted)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should put the last curly brace on its own line" do
|
28
|
+
unformatted = '{}'
|
29
|
+
formatted= "{\n}\n"
|
30
|
+
|
31
|
+
expect(t.pretty_print_json(unformatted)).to eq(formatted)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should put the last suqre bracket on its own line" do
|
35
|
+
unformatted = '[]'
|
36
|
+
formatted= "[\n]\n"
|
37
|
+
|
38
|
+
expect(t.pretty_print_json(unformatted)).to eq(formatted)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should indent nested structures" do
|
42
|
+
unformatted = '{"foo":{"bar":"baz"}}'
|
43
|
+
formatted= "{\n \"foo\":{\n \"bar\":\"baz\"\n }\n}\n"
|
44
|
+
|
45
|
+
expect(t.pretty_print_json(unformatted)).to eq(formatted)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should include only one cr at the end of the result" do
|
49
|
+
unformatted = "{}\n\n\n"
|
50
|
+
formatted= "{\n}\n"
|
51
|
+
|
52
|
+
expect(t.pretty_print_json(unformatted)).to eq(formatted)
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should remove any newlines in the input" do
|
56
|
+
unformatted = "{\n\"foo\"\n:{\"bar\":\"baz\"}\n\n\n\n\n}"
|
57
|
+
formatted= "{\n \"foo\":{\n \"bar\":\"baz\"\n }\n}\n"
|
58
|
+
|
59
|
+
expect(t.pretty_print_json(unformatted)).to eq(formatted)
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should tolerate and format already-formatted json" do
|
63
|
+
unformatted = "{\n\t\"foo\":{\n\t\t\"bar\":\"baz\",\n\t\t\"qux\":true\n\t}\n}"
|
64
|
+
formatted= "{\n \"foo\":{\n \"bar\":\"baz\",\n \"qux\":true\n }\n}\n"
|
65
|
+
|
66
|
+
expect(t.pretty_print_json(unformatted)).to eq(formatted)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'tolerate_json/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "tolerate_json"
|
8
|
+
spec.version = TolerateJson::VERSION
|
9
|
+
spec.authors = ["Matthew Nielsen"]
|
10
|
+
spec.email = ["xunker@pyxidis.org"]
|
11
|
+
spec.description = %q{A JSON formatter written in pure Ruby that works "good enough" and has no gem dependencies. 60% of the time, it works every time.}
|
12
|
+
spec.summary = %q{Tolerate_json is a pure-ruby Json formatter that has no outside gem dependencies. It almost works right and is good enough so you can tolerate reading JSON from your screen (hence the name). It is designed to be a simple formatter for when you need a lightweight component, but it is not suitable for cases where proper formatting is mission-critical. Compatible with MRI 1.8.7, 1.9.3 and 2.0.0 as well as JRuby and Rubinius in those modes.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec", ">= 2.10"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tolerate_json
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Matthew Nielsen
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-08-31 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '2.10'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.10'
|
62
|
+
description: A JSON formatter written in pure Ruby that works "good enough" and has
|
63
|
+
no gem dependencies. 60% of the time, it works every time.
|
64
|
+
email:
|
65
|
+
- xunker@pyxidis.org
|
66
|
+
executables: []
|
67
|
+
extensions: []
|
68
|
+
extra_rdoc_files: []
|
69
|
+
files:
|
70
|
+
- .gitignore
|
71
|
+
- .rspec
|
72
|
+
- .rvmrc
|
73
|
+
- .travis.yml
|
74
|
+
- Gemfile
|
75
|
+
- LICENSE.txt
|
76
|
+
- README.md
|
77
|
+
- Rakefile
|
78
|
+
- lib/tolerate_json.rb
|
79
|
+
- lib/tolerate_json/version.rb
|
80
|
+
- spec/lib/tolerate_json_spec.rb
|
81
|
+
- spec/spec_helper.rb
|
82
|
+
- tolerate_json.gemspec
|
83
|
+
homepage: ''
|
84
|
+
licenses:
|
85
|
+
- MIT
|
86
|
+
post_install_message:
|
87
|
+
rdoc_options: []
|
88
|
+
require_paths:
|
89
|
+
- lib
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
none: false
|
92
|
+
requirements:
|
93
|
+
- - ! '>='
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
requirements: []
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 1.8.25
|
105
|
+
signing_key:
|
106
|
+
specification_version: 3
|
107
|
+
summary: Tolerate_json is a pure-ruby Json formatter that has no outside gem dependencies.
|
108
|
+
It almost works right and is good enough so you can tolerate reading JSON from your
|
109
|
+
screen (hence the name). It is designed to be a simple formatter for when you need
|
110
|
+
a lightweight component, but it is not suitable for cases where proper formatting
|
111
|
+
is mission-critical. Compatible with MRI 1.8.7, 1.9.3 and 2.0.0 as well as JRuby
|
112
|
+
and Rubinius in those modes.
|
113
|
+
test_files:
|
114
|
+
- spec/lib/tolerate_json_spec.rb
|
115
|
+
- spec/spec_helper.rb
|