json-minify 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aeb41b79a66fbd6809e151954d72c477f243ddc8
4
- data.tar.gz: 485ae54778599198f619d9900c5a340eb877fb96
3
+ metadata.gz: f59536b9ca22b0f66e6d56ddddf3ad4463b00306
4
+ data.tar.gz: 457e5b20f346292efbb79673dad976cd66758e0b
5
5
  SHA512:
6
- metadata.gz: 94daa535c56dd78b04aed9fe637bbe3c8faa44dc450970930b082a05c92a89e713277d822fa708674645a1321123b9968f8980f7f0078422881ca92273e0bbeb
7
- data.tar.gz: 8c5da8da575610179c06955fa2cc9dad8119d3954b007c836b8524e03b34326aef2d118004b790d34412af8afc6798f787f46c9047762cb2f97fbe27bab3b707
6
+ metadata.gz: 6e7d87ff3d41f7cc9955605b6b8bdd8a18510fea314f536101664a98d7ce7530e031cf877d938b7db6db980c1505584c64245db5fa8fe44d63149eab5af75cf7
7
+ data.tar.gz: 9017ab6ea18c659dc348fbd88759a2943186ed7e4e01567df951dd834bc67572f2e1a0ded6b09a10ecf3c8652f0a6dc4b94872162a19997353ec48d37462442f
data/Rakefile CHANGED
@@ -1,4 +1,4 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
2
 
3
3
  require 'rspec/core/rake_task'
4
4
 
@@ -4,22 +4,22 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'json/minify/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "json-minify"
7
+ spec.name = 'json-minify'
8
8
  spec.version = JSON::Minify::VERSION
9
9
  spec.authors = ["Geoff Youngs\n\n\n"]
10
- spec.email = ["git@intersect-uk.co.uk"]
11
- spec.summary = %q{JSON.minify implementation}
12
- spec.description = %q{Pre-parser for JSON that removes C/C++ style comments and whitespace from formatted JSON, similar to https://github.com/getify/JSON.minify.}
13
- spec.homepage = "http://github.com/geoffyoungs/json-minify-rb"
14
- spec.license = "MIT"
10
+ spec.email = ['git@intersect-uk.co.uk']
11
+ spec.summary = 'JSON.minify implementation'
12
+ spec.description = 'Pre-parser for JSON that removes C/C++ style comments and whitespace from formatted JSON, similar to https://github.com/getify/JSON.minify.'
13
+ spec.homepage = 'http://github.com/geoffyoungs/json-minify-rb'
14
+ spec.license = 'MIT'
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0")
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
19
+ spec.require_paths = ['lib']
20
20
 
21
- spec.add_development_dependency "bundler", "~> 1.5"
22
- spec.add_development_dependency "rake", '~> 10.1'
23
- spec.add_development_dependency "rspec", "~> 2.14"
24
- spec.add_dependency "json", "> 0"
21
+ spec.add_development_dependency 'bundler', '~> 1.5'
22
+ spec.add_development_dependency 'rake', '~> 10.1'
23
+ spec.add_development_dependency 'rspec', '~> 3.2'
24
+ spec.add_dependency 'json', '> 0'
25
25
  end
@@ -1,19 +1,21 @@
1
1
  require 'strscan'
2
2
  require 'json'
3
- require "json/minify/version"
3
+ require 'json/minify/version'
4
4
 
5
5
  module JSON
6
6
  module Minify
7
7
  def self.included(mod)
8
8
  mod.send(:extend, self)
9
9
  end
10
+
10
11
  def minify(str)
11
- ss, buf = StringScanner.new(str), ''
12
+ ss = StringScanner.new(str)
13
+ buf = ''
12
14
 
13
15
  until ss.eos?
14
16
  # Remove whitespace
15
17
  if s = ss.scan(/\s+/)
16
-
18
+
17
19
  # Scan punctuation
18
20
  elsif s = ss.scan(/[{},:\]\[]/)
19
21
  buf << s
@@ -31,12 +33,12 @@ module JSON
31
33
  buf << s
32
34
 
33
35
  # Remove C++ style comments
34
- elsif s = ss.scan(%r<//>)
36
+ elsif s = ss.scan(%r{//})
35
37
  ss.scan_until(/(\r?\n|$)/)
36
38
 
37
39
  # Remove C style comments
38
- elsif s = ss.scan(%r'/[*]')
39
- ss.scan_until(%r'[*]/') or raise SyntaxError, "Unterminated /*...*/ comment - #{ss.rest}"
40
+ elsif s = ss.scan(%r{/[*]})
41
+ ss.scan_until(%r{[*]/}) || raise(SyntaxError, "Unterminated /*...*/ comment - #{ss.rest}")
40
42
 
41
43
  # Anything else is invalid JSON
42
44
  else
@@ -0,0 +1,83 @@
1
+ require 'strscan'
2
+ require 'json'
3
+ require 'json/minify'
4
+ require 'json/minify/version'
5
+
6
+ module JSON
7
+ module Minify
8
+ #
9
+ class Repairer
10
+ TOKENS = {
11
+ /\s+/ => [:whitespace, :skip],
12
+ /[{},:\]\[]/ => [:punctuation, :keep],
13
+ /""|(".*?[^\\])?"/ => [:string, :keep],
14
+ /(true|false|null)/ => [:reserved, :keep],
15
+ /-?\d+([.]\d+)?([eE][-+]?[0-9]+)?/ => [:number, :keep],
16
+ %r{//.*?(\r?\n|$)} => [:comment, :skip],
17
+ %r{/[*].*?[*]/} => [:comment, :skip]
18
+ }.freeze
19
+
20
+ def self.combine(tokens, type)
21
+ regexes = tokens.map do |key, bits|
22
+ key if bits[1] == type
23
+ end.compact
24
+ Regexp.new(regexes.join('|'))
25
+ end
26
+
27
+ KEEP = combine(TOKENS, :keep)
28
+ SKIP = combine(TOKENS, :skip)
29
+ ALL = Regexp.new(TOKENS.keys.join('|'))
30
+
31
+ def initialize(str)
32
+ @str = str
33
+ end
34
+
35
+ def tokens
36
+ scanner, buf = StringScanner.new(@str), []
37
+
38
+ until scanner.eos?
39
+ token = scanner.scan(KEEP)
40
+ buf << token if token
41
+ skip = scanner.skip(SKIP)
42
+
43
+ # Anything else is invalid JSON
44
+ next if skip || token || scanner.eos?
45
+
46
+ cur = scanner.pos
47
+ extra = scanner.scan_until(ALL)
48
+ if extra
49
+ invalid_string = scanner.pre_match[cur..-1]
50
+ next_token = scanner.matched
51
+ buf << next_token if KEEP.match(next_token)
52
+ else
53
+ invalid_string = scanner.rest
54
+ end
55
+
56
+ alternate_token = transform(invalid_string)
57
+ if alternate_token
58
+ buf << alternate_token
59
+ else
60
+ raise SyntaxError, "Unable to pre-scan string: #{invalid_string}"
61
+ end
62
+ end
63
+
64
+ buf
65
+ end
66
+
67
+ def parse
68
+ JSON.parse(tokens.join)
69
+ end
70
+
71
+ def transform(token)
72
+ case token
73
+ when 'NaN'
74
+ '0'
75
+ end
76
+ end
77
+
78
+ def minify_parse(buf)
79
+ JSON.parse(minify(buf))
80
+ end
81
+ end
82
+ end
83
+ end
@@ -1,5 +1,5 @@
1
1
  module JSON
2
2
  module Minify
3
- VERSION = "0.0.2"
3
+ VERSION = '0.0.3'.freeze
4
4
  end
5
5
  end
@@ -0,0 +1,24 @@
1
+ $LOAD_PATH << File.dirname(__FILE__) + '../lib'
2
+
3
+ require 'json/minify/repairer'
4
+
5
+ describe JSON::Minify::Repairer do
6
+ subject { described_class.new(sample) }
7
+
8
+ context 'with a NaN' do
9
+ let(:sample) { '{ "hello": NaN }' }
10
+ let(:tokens) { ['{', '"hello"', ':', '0', '}'] }
11
+
12
+ it 'should remove whitespace from JSON' do
13
+ expect(subject.tokens).to eql(tokens)
14
+ end
15
+ end
16
+
17
+ context 'when parsing data' do
18
+ let(:sample) { '{ "hello": NaN }' }
19
+
20
+ it 'should remove whitespace from JSON' do
21
+ expect(subject.parse).to include('hello' => 0)
22
+ end
23
+ end
24
+ end
@@ -1,33 +1,33 @@
1
- $: << File.dirname(__FILE__)+"../lib"
1
+ $LOAD_PATH << File.dirname(__FILE__) + '../lib'
2
2
 
3
3
  require 'json/minify'
4
4
 
5
5
  describe JSON::Minify do
6
- it "should remove whitespace from JSON" do
7
- expect(JSON.minify("{ }")).to eql("{}")
8
- expect(JSON.minify(%Q<{"foo": "bar"\n}\n>)).to eql(%Q<{"foo":"bar"}>)
6
+ it 'should remove whitespace from JSON' do
7
+ expect(JSON.minify('{ }')).to eql('{}')
8
+ expect(JSON.minify(%({"foo": "bar"\n}\n))).to eql(%({"foo":"bar"}))
9
9
  end
10
10
 
11
- it "should remove comments from JSON" do
12
- expect(JSON.minify("{ /* foo */ } /* bar */")).to eql("{}")
13
- expect(JSON.minify(%Q<{"foo": "bar"\n}\n // this is a comment>)).to eql(%Q<{"foo":"bar"}>)
11
+ it 'should remove comments from JSON' do
12
+ expect(JSON.minify('{ /* foo */ } /* bar */')).to eql('{}')
13
+ expect(JSON.minify(%({"foo": "bar"\n}\n // this is a comment))).to eql(%({"foo":"bar"}))
14
14
  end
15
15
 
16
- it "should throw a SyntaxError on invalid JSON" do
17
- expect { JSON.minify("{ /* foo */ } /* bar ") }.to raise_error(SyntaxError)
18
- expect { JSON.minify(%Q<{ "foo": new Date(1023) }>) }.to raise_error(SyntaxError)
16
+ it 'should throw a SyntaxError on invalid JSON' do
17
+ expect { JSON.minify('{ /* foo */ } /* bar ') }.to raise_error(SyntaxError)
18
+ expect { JSON.minify(%<{ "foo": new Date(1023) }>) }.to raise_error(SyntaxError)
19
19
  end
20
20
 
21
- it "should cope with the example from https://github.com/getify/JSON.minify" do
22
- expect(JSON.minify( %Q'{ /* comment */ "foo": 42 \n }' )).to eql('{"foo":42}')
21
+ it 'should cope with the example from https://github.com/getify/JSON.minify' do
22
+ expect(JSON.minify(%({ /* comment */ "foo": 42 \n }))).to eql('{"foo":42}')
23
23
  end
24
24
 
25
- it "should cope with the https://gist.github.com/mattheworiordan/72db0dfc933f743622eb" do
25
+ it 'should cope with the https://gist.github.com/mattheworiordan/72db0dfc933f743622eb' do
26
26
  expect(JSON.minify('{ "PolicyName": { "Fn::Join" : [ "", [ "AblySnsPublish-", { "Ref" : "AWS::Region" }, "-", { "Ref" : "DataCenterID" } ] ] } }')).to \
27
27
  eql('{"PolicyName":{"Fn::Join":["",["AblySnsPublish-",{"Ref":"AWS::Region"},"-",{"Ref":"DataCenterID"}]]}}')
28
28
  end
29
29
 
30
- it "should cope with escaped double quoted strings" do
30
+ it 'should cope with escaped double quoted strings' do
31
31
  expect(JSON.minify('{ "Statement1": "he said \"no way\" to the dog", "Statement2": "she said \"really?\"" }')).to \
32
32
  eql('{"Statement1":"he said \"no way\" to the dog","Statement2":"she said \"really?\""}')
33
33
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-minify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - |+
@@ -11,62 +11,62 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2014-08-14 00:00:00.000000000 Z
14
+ date: 2016-09-30 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: bundler
18
18
  requirement: !ruby/object:Gem::Requirement
19
19
  requirements:
20
- - - ~>
20
+ - - "~>"
21
21
  - !ruby/object:Gem::Version
22
22
  version: '1.5'
23
23
  type: :development
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
- - - ~>
27
+ - - "~>"
28
28
  - !ruby/object:Gem::Version
29
29
  version: '1.5'
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: rake
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  requirements:
34
- - - ~>
34
+ - - "~>"
35
35
  - !ruby/object:Gem::Version
36
36
  version: '10.1'
37
37
  type: :development
38
38
  prerelease: false
39
39
  version_requirements: !ruby/object:Gem::Requirement
40
40
  requirements:
41
- - - ~>
41
+ - - "~>"
42
42
  - !ruby/object:Gem::Version
43
43
  version: '10.1'
44
44
  - !ruby/object:Gem::Dependency
45
45
  name: rspec
46
46
  requirement: !ruby/object:Gem::Requirement
47
47
  requirements:
48
- - - ~>
48
+ - - "~>"
49
49
  - !ruby/object:Gem::Version
50
- version: '2.14'
50
+ version: '3.2'
51
51
  type: :development
52
52
  prerelease: false
53
53
  version_requirements: !ruby/object:Gem::Requirement
54
54
  requirements:
55
- - - ~>
55
+ - - "~>"
56
56
  - !ruby/object:Gem::Version
57
- version: '2.14'
57
+ version: '3.2'
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: json
60
60
  requirement: !ruby/object:Gem::Requirement
61
61
  requirements:
62
- - - '>'
62
+ - - ">"
63
63
  - !ruby/object:Gem::Version
64
64
  version: '0'
65
65
  type: :runtime
66
66
  prerelease: false
67
67
  version_requirements: !ruby/object:Gem::Requirement
68
68
  requirements:
69
- - - '>'
69
+ - - ">"
70
70
  - !ruby/object:Gem::Version
71
71
  version: '0'
72
72
  description: Pre-parser for JSON that removes C/C++ style comments and whitespace
@@ -77,14 +77,16 @@ executables: []
77
77
  extensions: []
78
78
  extra_rdoc_files: []
79
79
  files:
80
- - .gitignore
80
+ - ".gitignore"
81
81
  - Gemfile
82
82
  - LICENSE.txt
83
83
  - README.md
84
84
  - Rakefile
85
85
  - json-minify.gemspec
86
86
  - lib/json/minify.rb
87
+ - lib/json/minify/repairer.rb
87
88
  - lib/json/minify/version.rb
89
+ - spec/minify_repairer_spec.rb
88
90
  - spec/minify_spec.rb
89
91
  homepage: http://github.com/geoffyoungs/json-minify-rb
90
92
  licenses:
@@ -96,19 +98,20 @@ require_paths:
96
98
  - lib
97
99
  required_ruby_version: !ruby/object:Gem::Requirement
98
100
  requirements:
99
- - - '>='
101
+ - - ">="
100
102
  - !ruby/object:Gem::Version
101
103
  version: '0'
102
104
  required_rubygems_version: !ruby/object:Gem::Requirement
103
105
  requirements:
104
- - - '>='
106
+ - - ">="
105
107
  - !ruby/object:Gem::Version
106
108
  version: '0'
107
109
  requirements: []
108
110
  rubyforge_project:
109
- rubygems_version: 2.2.1
111
+ rubygems_version: 2.2.5
110
112
  signing_key:
111
113
  specification_version: 4
112
114
  summary: JSON.minify implementation
113
115
  test_files:
116
+ - spec/minify_repairer_spec.rb
114
117
  - spec/minify_spec.rb