jsonlint 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in jsonlint.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Doug Barth
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.
@@ -0,0 +1,29 @@
1
+ # JsonLint
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'jsonlint'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install jsonlint
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/jsonlint/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
8
+
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $: << File.expand_path('../../lib', __FILE__)
4
+
5
+ require 'jsonlint'
6
+ require 'jsonlint/cli'
7
+
8
+ JsonLint::CLI.new(ARGV).execute!
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'jsonlint/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "jsonlint"
8
+ spec.version = JsonLint::VERSION
9
+ spec.authors = ["Doug Barth"]
10
+ spec.email = ["dougbarth@gmail.com"]
11
+ spec.summary = %q{JSON lint checker}
12
+ spec.description = %q{Checks JSON files for correct syntax and no silly mistakes}
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency 'oj', '~> 2'
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.6"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec"
25
+ end
@@ -0,0 +1,13 @@
1
+ require 'logger'
2
+
3
+ require 'jsonlint/version'
4
+ require 'jsonlint/linter'
5
+
6
+ module JsonLint
7
+ # Your code goes here...
8
+ def self.logger
9
+ @logger ||= Logger.new(STDOUT).tap do |l|
10
+ l.level = Logger::INFO
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,19 @@
1
+ module JsonLint
2
+ class CLI
3
+ def initialize(argv, stdin = STDIN, stdout = STDOUT, stderr = STDERR, kernel = Kernel)
4
+ @argv, @stdin, @stdout, @stderr, @kernel = argv, stdin, stdout, stderr, kernel
5
+ end
6
+
7
+ def execute!
8
+ files_to_check = @argv
9
+
10
+ linter = JsonLint::Linter.new
11
+ linter.check_all(files_to_check)
12
+
13
+ if linter.has_errors?
14
+ linter.display_errors
15
+ @kernel.exit(1)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module JsonLint
2
+ class FileNotFoundError < StandardError; end
3
+ end
@@ -0,0 +1,147 @@
1
+ require 'oj'
2
+ require 'set'
3
+
4
+ require 'jsonlint/errors'
5
+
6
+ module JsonLint
7
+ class Linter
8
+ attr_reader :errors
9
+
10
+ def initialize
11
+ @errors = Hash.new {|h,k| h[k] = [] }
12
+ end
13
+
14
+ def check_all(*files_to_check)
15
+ files_to_check.flatten.each {|f| check(f) }
16
+ end
17
+
18
+ def check(path)
19
+ raise FileNotFoundError, "#{path} does not exist" unless File.exist?(path)
20
+
21
+ check_syntax_valid(path) && check_overlapping_keys(path)
22
+ end
23
+
24
+ def has_errors?
25
+ ! errors.empty?
26
+ end
27
+
28
+ def display_errors
29
+ errors.each do |path, errors|
30
+ puts path
31
+ errors.each do |err|
32
+ puts " #{err}"
33
+ end
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def check_syntax_valid(path)
40
+ Oj.load_file(path, nilnil: false)
41
+ true
42
+ rescue Oj::ParseError => e
43
+ errors[path] << e.message
44
+ false
45
+ end
46
+
47
+ class KeyOverlapDetector < Oj::Saj
48
+ attr_reader :overlapping_keys
49
+
50
+ def initialize
51
+ @seen_keys = Set.new
52
+ @key_components = []
53
+ @overlapping_keys = Set.new
54
+
55
+ @complex_type = []
56
+ @array_positions = []
57
+ end
58
+
59
+ def hash_start(key)
60
+ JsonLint.logger.debug { "hash_start: #{key.inspect}" }
61
+
62
+ case @complex_type.last
63
+ when :hash
64
+ @key_components.push(key)
65
+ when :array
66
+ @key_components.push(@array_positions.last)
67
+ @array_positions[-1] += 1
68
+ end
69
+
70
+ @complex_type.push(:hash)
71
+ check_for_overlap!
72
+ end
73
+
74
+ def hash_end(key)
75
+ JsonLint.logger.debug { "hash_end: #{key.inspect}" }
76
+ @key_components.pop
77
+ @complex_type.pop
78
+ end
79
+
80
+ def array_start(key)
81
+ JsonLint.logger.debug { "array_start: #{key.inspect}" }
82
+
83
+ case @complex_type.last
84
+ when :hash
85
+ @key_components.push(key)
86
+ when :array
87
+ @key_components.push(@array_positions.last)
88
+ @array_positions[-1] += 1
89
+ end
90
+
91
+ @complex_type.push(:array)
92
+ @array_positions.push(0)
93
+ check_for_overlap!
94
+ end
95
+
96
+ def array_end(key)
97
+ JsonLint.logger.debug { "array_end: #{key.inspect}" }
98
+ @key_components.pop
99
+ @complex_type.pop
100
+ @array_positions.pop
101
+ end
102
+
103
+ def add_value(value, key)
104
+ JsonLint.logger.debug { "add_value: #{value.inspect}, #{key.inspect}" }
105
+ case @complex_type.last
106
+ when :hash
107
+ @key_components.push(key)
108
+ check_for_overlap!
109
+ @key_components.pop
110
+ when :array
111
+ @key_components.push(@array_positions.last)
112
+ check_for_overlap!
113
+ @array_positions[-1] += 1
114
+ @key_components.pop
115
+ end
116
+ end
117
+
118
+ def error(message, line, column)
119
+ JsonLint.logger.debug { "error: #{message.inspect}, #{line.inspect}, #{column.inspect}" }
120
+ end
121
+
122
+ private
123
+ def check_for_overlap!
124
+ full_key = @key_components.dup
125
+ JsonLint.logger.debug { "Checking #{full_key.join('.')} for overlap" }
126
+
127
+ unless @seen_keys.add?(full_key)
128
+ JsonLint.logger.debug { "Overlapping key #{full_key.join('.')}" }
129
+ @overlapping_keys << full_key
130
+ end
131
+ end
132
+ end
133
+
134
+ def check_overlapping_keys(path)
135
+ overlap_detector = KeyOverlapDetector.new
136
+ File.open(path, 'r') do |f|
137
+ Oj.saj_parse(overlap_detector, f)
138
+ end
139
+
140
+ overlap_detector.overlapping_keys.each do |key|
141
+ errors[path] << "The same key is defined twice: #{key.join('.')}"
142
+ end
143
+
144
+ !! overlap_detector.overlapping_keys.empty?
145
+ end
146
+ end
147
+ end
@@ -0,0 +1,37 @@
1
+ require 'rake'
2
+ require 'rake/tasklib'
3
+
4
+ require 'jsonlint'
5
+
6
+ module JsonLint
7
+ class RakeTask < Rake::TaskLib
8
+ attr_accessor :name
9
+ attr_accessor :paths
10
+
11
+ def initialize(name = :jsonlint)
12
+ @name = name
13
+
14
+ yield self if block_given?
15
+
16
+ define_task
17
+ end
18
+
19
+ private
20
+
21
+ def define_task
22
+ desc 'Run jsonlint' unless ::Rake.application.last_comment
23
+
24
+ task(self.name) do
25
+ files_to_check = Rake::FileList.new(self.paths)
26
+
27
+ linter = ::JsonLint::Linter.new
28
+ linter.check_all(files_to_check)
29
+
30
+ if linter.has_errors?
31
+ linter.display_errors
32
+ abort('JSON lint found')
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module JsonLint
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,13 @@
1
+ {
2
+ "foo": {
3
+ "bar": {
4
+ "a": 1
5
+ },
6
+ "baz": {
7
+ "a": 1
8
+ },
9
+ "bar": {
10
+ "b": 1
11
+ }
12
+ }
13
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "foo": {
3
+ "bar": 1
4
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "foo": 1
3
+ "bar": 2
4
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "foo": "foo",
3
+ "foo": "bar"
4
+ }
@@ -0,0 +1,11 @@
1
+ {
2
+ "an_int": 1,
3
+ "a_float": 1.0,
4
+ "a_string": "foo",
5
+ "a_boolean": true,
6
+ "an_array": [1,2,3],
7
+ "an_object": {
8
+ "field1": true,
9
+ "field2": ""
10
+ }
11
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "array_of_arrays": [[1], [1]]
3
+ }
@@ -0,0 +1,12 @@
1
+ {
2
+ "objs": [
3
+ {
4
+ "name": "foo",
5
+ "value": 1
6
+ },
7
+ {
8
+ "name": "bar",
9
+ "value": 1
10
+ }
11
+ ]
12
+ }
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+ require 'jsonlint/linter'
3
+
4
+ describe 'JsonLint::Linter' do
5
+ let(:linter) { JsonLint::Linter.new }
6
+
7
+ it 'should throw an exception if given a bogus path' do
8
+ expect { linter.check('/does/not/exist') }.to raise_error
9
+ end
10
+
11
+ it 'should be happy with a valid JSON file' do
12
+ expect(linter.check(spec_data('valid.json'))).to be(true)
13
+ expect(linter.check(spec_data('valid_array_of_objects.json'))).to be(true)
14
+ expect(linter.check(spec_data('valid_array_of_arrays.json'))).to be(true)
15
+ end
16
+
17
+ it 'should be unhappy with an invalid JSON file' do
18
+ expect(linter.check(spec_data('missing_comma.json'))).to be(false)
19
+ end
20
+
21
+ it 'should be unhappy with JSON that has overlapping keys' do
22
+ expect(linter.check(spec_data('overlapping_keys.json'))).to be(false)
23
+ expect(linter.check(spec_data('deep_overlap.json'))).to be(false)
24
+ end
25
+ end
@@ -0,0 +1,13 @@
1
+ require 'rspec'
2
+
3
+ require 'jsonlint'
4
+
5
+ module SpecHelpers
6
+ def spec_data(data_path)
7
+ File.expand_path(File.join('spec/data', data_path))
8
+ end
9
+ end
10
+
11
+ RSpec.configure do |config|
12
+ config.include SpecHelpers
13
+ end
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsonlint
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Doug Barth
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-07-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: oj
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '2'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.6'
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: '1.6'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
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: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Checks JSON files for correct syntax and no silly mistakes
79
+ email:
80
+ - dougbarth@gmail.com
81
+ executables:
82
+ - jsonlint
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .gitignore
87
+ - .travis.yml
88
+ - Gemfile
89
+ - LICENSE.txt
90
+ - README.md
91
+ - Rakefile
92
+ - bin/jsonlint
93
+ - jsonlint.gemspec
94
+ - lib/jsonlint.rb
95
+ - lib/jsonlint/cli.rb
96
+ - lib/jsonlint/errors.rb
97
+ - lib/jsonlint/linter.rb
98
+ - lib/jsonlint/rake_task.rb
99
+ - lib/jsonlint/version.rb
100
+ - spec/data/deep_overlap.json
101
+ - spec/data/missing_brace.json
102
+ - spec/data/missing_comma.json
103
+ - spec/data/overlapping_keys.json
104
+ - spec/data/valid.json
105
+ - spec/data/valid_array_of_arrays.json
106
+ - spec/data/valid_array_of_objects.json
107
+ - spec/linter_spec.rb
108
+ - spec/spec_helper.rb
109
+ homepage:
110
+ licenses:
111
+ - MIT
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
119
+ - - ! '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ segments:
123
+ - 0
124
+ hash: -4326837317699917705
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ none: false
127
+ requirements:
128
+ - - ! '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ segments:
132
+ - 0
133
+ hash: -4326837317699917705
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 1.8.24
137
+ signing_key:
138
+ specification_version: 3
139
+ summary: JSON lint checker
140
+ test_files:
141
+ - spec/data/deep_overlap.json
142
+ - spec/data/missing_brace.json
143
+ - spec/data/missing_comma.json
144
+ - spec/data/overlapping_keys.json
145
+ - spec/data/valid.json
146
+ - spec/data/valid_array_of_arrays.json
147
+ - spec/data/valid_array_of_objects.json
148
+ - spec/linter_spec.rb
149
+ - spec/spec_helper.rb