tolerate_json 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZDEyYzEyMzYxYTA4YjQzYmQ5NzcwOGIzYmE1M2E2MDg2ZjZkY2JkYQ==
5
+ data.tar.gz: !binary |-
6
+ MGQwYzBiNTg3MzZkZWQ2YTM4ODliYzE1NzFkOTg2OWU5OWViYjY2Mg==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ NjczNzdkNjYwNTBlY2UxMTZmOGI0M2FkNzk1Yjg0Y2ZiNTYwNTIxMzMwNzQz
10
+ Nzg0MzU1M2RhMmY5NjczZDFiNDYzYTM3NDk4Yzc5ZTg3YzE0OWVkMDA1OWRk
11
+ Yjc2YjcxZDQyODM3NThmMDYzMTJhNGEyNDU4NTVmOWNkOGU0ZjA=
12
+ data.tar.gz: !binary |-
13
+ Y2NjZDJmOWI4NGJiZDFhOTE4ZGMzYjhkZWZlMWNlOWU2MDViNjVkNjljMTA4
14
+ ODMxNjFlMjE1N2MzNDI4MzJjNzNiZWNmZTE0YjU5OTFkNzgyNTYxZmEzZmYz
15
+ NTg1ZWYxNWVmMGE1NGNjM2EyODMzMWZhM2JiNjMwNjAwYjUxOWE=
data/.travis.yml CHANGED
@@ -1,8 +1,8 @@
1
1
  language: ruby
2
2
  rvm:
3
+ - 2.1.0
3
4
  - 2.0.0
4
5
  - 1.9.3
5
- - 1.9.2
6
6
  - rbx-18mode
7
7
  - rbx-19mode
8
8
  - 1.8.7
data/README.md CHANGED
@@ -12,28 +12,39 @@ It is a "90%" solution: it works in 90% of use cases, but will almost certainly
12
12
 
13
13
  ## Installation
14
14
 
15
- Add this line to your application's Gemfile:
15
+ Either add `gem 'tolerate_json'` to your application's Gemfile and do `bundle install`, or install it manually with `gem install tolerate_json`
16
16
 
17
- gem 'tolerate_json'
17
+ ## Usage
18
18
 
19
- And then execute:
19
+ ### Include the module
20
20
 
21
- $ bundle
21
+ ```ruby
22
+ require 'rubygems' # optional on 1.9.3 and up
22
23
 
23
- Or install it yourself as:
24
+ require 'tolerate_json'
24
25
 
25
- $ gem install tolerate_json
26
+ include TolderateJson
26
27
 
27
- ## Usage
28
+ puts pretty_print_json('{"foo":{"bar":"baz"}}')
29
+
30
+ # Prints:
31
+ #
32
+ # {
33
+ # "foo":{
34
+ # "bar":"baz"
35
+ # }
36
+ # }
37
+ #
38
+ ```
39
+
40
+ ### Use it directly from the class
28
41
 
29
42
  ```ruby
30
43
  require 'rubygems' # optional on 1.9.3 and up
31
44
 
32
45
  require 'tolerate_json'
33
46
 
34
- include TolderateJson
35
-
36
- puts pretty_print_json('{"foo":{"bar":"baz"}}')
47
+ puts TolderateJson.pretty_print_json('{"foo":{"bar":"baz"}}')
37
48
 
38
49
  # Prints:
39
50
  #
data/lib/tolerate_json.rb CHANGED
@@ -1,42 +1,56 @@
1
1
  require "tolerate_json/version"
2
2
 
3
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", '')
4
+ module Formatter
5
+ def self.format_json_string(json, indentation_character=nil)
6
+ indentation_character ||= ' '
7
+ return json if json.to_s.size < 1
8
+
9
+ json.gsub!("\r\n", '')
10
+ json.gsub!("\n", '')
11
+ json.gsub!("\r", '')
12
+
13
+ if json.match(/[^\n]\}$/)
14
+ json.gsub!(/\}$/, "\n}")
15
+ json.gsub!(/\}\}/, "}\n}")
16
+ end
10
17
 
11
- if json.match(/[^\n]\}$/)
12
- json.gsub!(/\}$/, "\n}")
13
- json.gsub!(/\}\}/, "}\n}")
14
- end
18
+ if json.match(/[^\n]\]$/)
19
+ json.gsub!(/\]$/, "\n]")
20
+ end
15
21
 
16
- if json.match(/[^\n]\]$/)
17
- json.gsub!(/\]$/, "\n]")
18
- end
22
+ json = json.to_s.gsub(/\{[\s+]\"/, "{\"").gsub(/\{(\s+)\"/, "{\"").gsub(/\"(\s+)\}/, "\"}").gsub(/true(\s+)\}/, "true}").gsub(/false(\s+)\}/, "false}").gsub(/\",(\s+)\"/, '","')
19
23
 
20
- json = json.to_s.gsub(/\{[\s+]\"/, "{\"").gsub(/\{(\s+)\"/, "{\"").gsub(/\"(\s+)\}/, "\"}").gsub(/true(\s+)\}/, "true}").gsub(/false(\s+)\}/, "false}").gsub(/\",(\s+)\"/, '","')
24
+ 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\"" }.gsub(/\:(\d+),\"/,":\1,\n\"")
21
25
 
22
- 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\"" }.gsub(/\:(\d+),\"/,":\1,\n\"")
26
+ # don't put a CR in empty arrays
27
+ json = json.gsub(/\[\n\s*\]/, '[]')
23
28
 
24
- output = []
29
+ output = []
25
30
 
26
- indent_level = 0
27
- json.split("\n").each do |s|
28
- indent_level -= 1 if ["]", "}"].include?(s.split('').first) && indent_level > 0
29
- output << (indentation_character*indent_level) + s
30
- if ["{", "["].include?(s.split('').last)
31
- indent_level += 1
32
- next
33
- end
31
+ indent_level = 0
32
+ json.split("\n").each do |s|
33
+ indent_level -= 1 if ["]", "}"].include?(s.split('').first) && indent_level > 0
34
+ output << (indentation_character*indent_level) + s
35
+ if ["{", "["].include?(s.split('').last)
36
+ indent_level += 1
37
+ next
38
+ end
34
39
 
35
- if ["{", "["].include?(s.split('').first)
36
- indent_level += 1
37
- next
40
+ if ["{", "["].include?(s.split('').first)
41
+ indent_level += 1
42
+ next
43
+ end
38
44
  end
45
+ output.join("\n")+"\n"
39
46
  end
40
- output.join("\n")+"\n"
47
+ end
48
+
49
+ def pretty_print_json(json, indentation_character=nil)
50
+ TolerateJson::Formatter.format_json_string(json, indentation_character)
51
+ end
52
+
53
+ def self.pretty_print_json(json, indentation_character=nil)
54
+ TolerateJson::Formatter.format_json_string(json, indentation_character)
41
55
  end
42
56
  end
@@ -1,3 +1,3 @@
1
1
  module TolerateJson
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -0,0 +1,104 @@
1
+ require 'spec_helper'
2
+ require 'tolerate_json'
3
+
4
+ describe TolerateJson::Formatter do
5
+
6
+ describe "pretty_print_json" do
7
+
8
+ it "should format simple json" do
9
+ unformatted = '{"foo":"bar"}'
10
+ formatted= "{\n \"foo\":\"bar\"\n}\n"
11
+
12
+ expect(subject.format_json_string(unformatted)).to eq(formatted)
13
+ end
14
+
15
+ it "should format complex json" do
16
+ unformatted = '{"foo":"bar","boolean":true,"Numbers":12345,"baz":"quz"}'
17
+ formatted= "{\n \"foo\":\"bar\",\n \"boolean\":true,\n \"Numbers\":12345,\n \"baz\":\"quz\"\n}\n"
18
+
19
+ expect(subject.format_json_string(unformatted)).to eq(formatted)
20
+ end
21
+
22
+ describe 'arrays' do
23
+ context 'array is empty' do
24
+ it "should not add a cr between the square braces of an empty array" do
25
+ unformatted = "{\"foo\":[]}"
26
+ formatted= "{\n \"foo\":[]\n}\n"
27
+
28
+ expect(subject.format_json_string(unformatted)).to eq(formatted)
29
+ end
30
+ end
31
+
32
+ context 'array is not empty' do
33
+ it "should put the last square bracket on its own line" do
34
+ unformatted = '["foo", "bar", "baz"]'
35
+ formatted= "[\"foo\",\n \"bar\",\n \"baz\"\n]\n"
36
+
37
+ expect(subject.format_json_string(unformatted)).to eq(formatted)
38
+ end
39
+ end
40
+ end
41
+
42
+ describe 'hashes' do
43
+ context 'hash is empty' do
44
+ it "should not add a cr between the curly braces of an empty hash" do
45
+ unformatted = "{\"foo\":{}}"
46
+ formatted= "{\n \"foo\":{}\n}\n"
47
+
48
+ expect(subject.format_json_string(unformatted)).to eq(formatted)
49
+ end
50
+ end
51
+
52
+ context 'hash is not empty' do
53
+ it "should put the last curly brace on its own line" do
54
+ unformatted = '{"foo":"bar"}'
55
+ formatted= "{\n \"foo\":\"bar\"\n}\n"
56
+
57
+ expect(subject.format_json_string(unformatted)).to eq(formatted)
58
+ end
59
+ end
60
+ end
61
+
62
+ it "should indent nested structures" do
63
+ unformatted = '{"foo":{"bar":"baz"}}'
64
+ formatted= "{\n \"foo\":{\n \"bar\":\"baz\"\n }\n}\n"
65
+
66
+ expect(subject.format_json_string(unformatted)).to eq(formatted)
67
+ end
68
+
69
+ it "should separate concurrant curly braces with a cr" do
70
+ unformatted = '{"qux":{"foo":{"bar":"baz"}}, "quux":"quuux"}'
71
+ formatted= "{\n \"qux\":{\n \"foo\":{\n \"bar\":\"baz\"\n }\n },\n \"quux\":\"quuux\"\n}\n"
72
+
73
+ expect(subject.format_json_string(unformatted)).to eq(formatted)
74
+ end
75
+
76
+ it "should separate numbers and closing curly braces with cr" do
77
+ unformatted = '{"qux":{"foo":1}, "quux":"quuux"}'
78
+ formatted= "{\n \"qux\":{\n \"foo\":1\n },\n \"quux\":\"quuux\"\n}\n"
79
+
80
+ expect(subject.format_json_string(unformatted)).to eq(formatted)
81
+ end
82
+
83
+ it "should include only one cr at the end of the result" do
84
+ unformatted = "{}\n\n\n"
85
+ formatted= "{\n}\n"
86
+
87
+ expect(subject.format_json_string(unformatted)).to eq(formatted)
88
+ end
89
+
90
+ it "should remove any newlines in the input" do
91
+ unformatted = "{\n\"foo\"\n:{\"bar\":\"baz\"}\n\n\n\n\n}"
92
+ formatted= "{\n \"foo\":{\n \"bar\":\"baz\"\n }\n}\n"
93
+
94
+ expect(subject.format_json_string(unformatted)).to eq(formatted)
95
+ end
96
+
97
+ it "should tolerate and format already-formatted json" do
98
+ unformatted = "{\n\t\"foo\":{\n\t\t\"bar\":\"baz\",\n\t\t\"qux\":true\n\t}\n}"
99
+ formatted= "{\n \"foo\":{\n \"bar\":\"baz\",\n \"qux\":true\n }\n}\n"
100
+
101
+ expect(subject.format_json_string(unformatted)).to eq(formatted)
102
+ end
103
+ end
104
+ end
@@ -1,83 +1,28 @@
1
1
  require 'spec_helper'
2
2
  require 'tolerate_json'
3
3
 
4
- class T
5
- include TolerateJson
6
- end
7
-
8
4
  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
5
 
41
- it "should indent nested structures" do
42
- unformatted = '{"foo":{"bar":"baz"}}'
43
- formatted= "{\n \"foo\":{\n \"bar\":\"baz\"\n }\n}\n"
6
+ let(:json) { double(:json_string) }
7
+ let(:indentation_string) { "\t" }
44
8
 
45
- expect(t.pretty_print_json(unformatted)).to eq(formatted)
46
- end
47
-
48
- it "should separate concurrant curly braces with a cr" do
49
- unformatted = '{"qux":{"foo":{"bar":"baz"}}, "quux":"quuux"}'
50
- formatted= "{\n \"qux\":{\n \"foo\":{\n \"bar\":\"baz\"\n }\n },\n \"quux\":\"quuux\"\n}\n"
51
-
52
- expect(t.pretty_print_json(unformatted)).to eq(formatted)
53
- end
54
-
55
- it "should separate numbers and closing curly braces with cr" do
56
- unformatted = '{"qux":{"foo":1}, "quux":"quuux"}'
57
- formatted= "{\n \"qux\":{\n \"foo\":1\n },\n \"quux\":\"quuux\"\n}\n"
58
-
59
- expect(t.pretty_print_json(unformatted)).to eq(formatted)
60
- end
61
-
62
- it "should include only one cr at the end of the result" do
63
- unformatted = "{}\n\n\n"
64
- formatted= "{\n}\n"
65
-
66
- expect(t.pretty_print_json(unformatted)).to eq(formatted)
67
- end
68
-
69
- it "should remove any newlines in the input" do
70
- unformatted = "{\n\"foo\"\n:{\"bar\":\"baz\"}\n\n\n\n\n}"
71
- formatted= "{\n \"foo\":{\n \"bar\":\"baz\"\n }\n}\n"
72
-
73
- expect(t.pretty_print_json(unformatted)).to eq(formatted)
9
+ describe '.pretty_print_json' do
10
+ it 'calls TolerateJson::Formatter' do
11
+ expect(TolerateJson::Formatter).to receive(:format_json_string).with(json, indentation_string)
12
+ TolerateJson.pretty_print_json(json, indentation_string)
74
13
  end
14
+ end
75
15
 
76
- it "should tolerate and format already-formatted json" do
77
- unformatted = "{\n\t\"foo\":{\n\t\t\"bar\":\"baz\",\n\t\t\"qux\":true\n\t}\n}"
78
- formatted= "{\n \"foo\":{\n \"bar\":\"baz\",\n \"qux\":true\n }\n}\n"
16
+
17
+ describe '#pretty_print_json' do
18
+ it 'calls TolerateJson::Formatter' do
19
+ class T
20
+ include TolerateJson
21
+ end
22
+ t = T.new
79
23
 
80
- expect(t.pretty_print_json(unformatted)).to eq(formatted)
24
+ expect(TolerateJson::Formatter).to receive(:format_json_string).with(json, indentation_string)
25
+ t.pretty_print_json(json, indentation_string)
81
26
  end
82
27
  end
83
28
  end
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tolerate_json
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
5
- prerelease:
4
+ version: 0.0.4
6
5
  platform: ruby
7
6
  authors:
8
7
  - Matthew Nielsen
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-10-30 00:00:00.000000000 Z
11
+ date: 2015-01-05 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: bundler
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ~>
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ~>
28
25
  - !ruby/object:Gem::Version
@@ -30,7 +27,6 @@ dependencies:
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rake
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
31
  - - ! '>='
36
32
  - !ruby/object:Gem::Version
@@ -38,7 +34,6 @@ dependencies:
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
38
  - - ! '>='
44
39
  - !ruby/object:Gem::Version
@@ -46,7 +41,6 @@ dependencies:
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: rspec
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
45
  - - ! '>='
52
46
  - !ruby/object:Gem::Version
@@ -54,7 +48,6 @@ dependencies:
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
52
  - - ! '>='
60
53
  - !ruby/object:Gem::Version
@@ -77,33 +70,33 @@ files:
77
70
  - Rakefile
78
71
  - lib/tolerate_json.rb
79
72
  - lib/tolerate_json/version.rb
73
+ - spec/lib/tolerate_json/formatter_spec.rb
80
74
  - spec/lib/tolerate_json_spec.rb
81
75
  - spec/spec_helper.rb
82
76
  - tolerate_json.gemspec
83
77
  homepage: https://github.com/xunker/tolerate_json
84
78
  licenses:
85
79
  - MIT
80
+ metadata: {}
86
81
  post_install_message:
87
82
  rdoc_options: []
88
83
  require_paths:
89
84
  - lib
90
85
  required_ruby_version: !ruby/object:Gem::Requirement
91
- none: false
92
86
  requirements:
93
87
  - - ! '>='
94
88
  - !ruby/object:Gem::Version
95
89
  version: '0'
96
90
  required_rubygems_version: !ruby/object:Gem::Requirement
97
- none: false
98
91
  requirements:
99
92
  - - ! '>='
100
93
  - !ruby/object:Gem::Version
101
94
  version: '0'
102
95
  requirements: []
103
96
  rubyforge_project:
104
- rubygems_version: 1.8.25
97
+ rubygems_version: 2.2.2
105
98
  signing_key:
106
- specification_version: 3
99
+ specification_version: 4
107
100
  summary: Tolerate_json is a pure-ruby Json formatter that has no outside gem dependencies.
108
101
  It almost works right and is good enough so you can tolerate reading JSON from your
109
102
  screen (hence the name). It is designed to be a simple formatter for when you need
@@ -111,5 +104,6 @@ summary: Tolerate_json is a pure-ruby Json formatter that has no outside gem dep
111
104
  is mission-critical. Compatible with MRI 1.8.7, 1.9.3 and 2.0.0 as well as JRuby
112
105
  and Rubinius in those modes.
113
106
  test_files:
107
+ - spec/lib/tolerate_json/formatter_spec.rb
114
108
  - spec/lib/tolerate_json_spec.rb
115
109
  - spec/spec_helper.rb