json_minify 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4b1a521047dc7e5c7f8ca2db29a83790e894c16c
4
+ data.tar.gz: 6faea50280d8e213df26861dab2dbbb7c393a7a0
5
+ SHA512:
6
+ metadata.gz: 48eb3797b93ae45ce392356c35b3b0580e4f29d19d19ee751d47635aa5cce236ac99bcc9565bea32e7332f5d2c824360eb94050e1953c625583f5f87768a1a5d
7
+ data.tar.gz: c34a2c0c33b72a25864082f5b618298666ba1d5560817ebe3cf799c4c20933d8df577090409f227d6565bcc0961dd667412ec17a6e0824743c1f338cb9a7f9f7
data/bin/json_minify ADDED
@@ -0,0 +1,39 @@
1
+ #!/usr/bin/env ruby
2
+ require 'json_minify'
3
+
4
+ # Trap interrupts to quit cleanly. See
5
+ # https://twitter.com/mitchellh/status/283014103189053442
6
+ Signal.trap('INT') { abort }
7
+
8
+ require 'pp'
9
+ require 'optparse'
10
+ options = {}
11
+
12
+ opt_parser = OptionParser.new do |opt|
13
+ opt.banner = "Usage: json_minify.rb Filename [OPTIONS]"
14
+ opt.separator ""
15
+ opt.separator "Options"
16
+
17
+ opt.on("-p", "--pretty-print", "pretty print the JSON output") do
18
+ options[:pretty] = true
19
+ end
20
+
21
+ opt.on_tail("-v", "--version") do
22
+ puts JsonMinify::VERSION
23
+ exit
24
+ end
25
+
26
+ opt.on_tail("-h", "--help", "print this help text") do
27
+ puts opt_parser
28
+ exit
29
+ end
30
+ end
31
+
32
+ opt_parser.parse!
33
+ if (input = ARGF.read)
34
+ if options[:pretty]
35
+ puts JsonMinify.minify_pretty(input)
36
+ else
37
+ puts JsonMinify.minify(input)
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module JsonMinify
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,60 @@
1
+ require 'strscan'
2
+ require 'multi_json'
3
+ require "json_minify/version"
4
+
5
+ module JsonMinify
6
+ def minify(str)
7
+ ss, buf = StringScanner.new(str), ''
8
+
9
+ until ss.eos?
10
+ # Remove whitespace
11
+ if s = ss.scan(/\s+/)
12
+
13
+ # Scan punctuation
14
+ elsif s = ss.scan(/[{},:\]\[]/)
15
+ buf << s
16
+
17
+ # Scan strings
18
+ elsif s = ss.scan(/"(.*?[^\\])?"/)
19
+ buf << s
20
+
21
+ # Scan reserved words
22
+ elsif s = ss.scan(/(true|false|null)/)
23
+ buf << s
24
+
25
+ # Scan numbers
26
+ elsif s = ss.scan(/-?\d+([.]\d+)?([eE][-+]?[0-9]+)?/)
27
+ buf << s
28
+
29
+ # Remove C++ style comments
30
+ elsif s = ss.scan(%r<//>)
31
+ ss.scan_until(/(\r?\n|$)/)
32
+
33
+ # Remove C style comments
34
+ elsif s = ss.scan(%r'/[*]')
35
+ ss.scan_until(%r'[*]/') or raise SyntaxError, "Unterminated /*...*/ comment - #{ss.rest}"
36
+
37
+ # Anything else is invalid JSON
38
+ else
39
+ raise SyntaxError, "Unable to pre-scan string: #{ss.rest}"
40
+ end
41
+ end
42
+ buf
43
+ end
44
+
45
+ def minify_parse(buf)
46
+ MultiJson.load(minify(buf))
47
+ end
48
+
49
+ def minify_pretty(buf)
50
+ MultiJson.dump(minify_parse(buf), pretty: true)
51
+ end
52
+ end
53
+
54
+ MultiJson.class_eval do
55
+ extend JsonMinify
56
+ end
57
+
58
+ JsonMinify.class_eval do
59
+ extend self
60
+ end
@@ -0,0 +1,23 @@
1
+ require 'json_minify'
2
+ require 'rspec/autorun'
3
+
4
+ describe JsonMinify do
5
+ it "should remove whitespace from MultiJson" do
6
+ expect(MultiJson.minify("{ }")).to eql("{}")
7
+ expect(MultiJson.minify(%Q<{"foo": "bar"\n}\n>)).to eql(%Q<{"foo":"bar"}>)
8
+ end
9
+
10
+ it "should remove comments from MultiJson" do
11
+ expect(MultiJson.minify("{ /* foo */ } /* bar */")).to eql("{}")
12
+ expect(MultiJson.minify(%Q<{"foo": "bar"\n}\n // this is a comment>)).to eql(%Q<{"foo":"bar"}>)
13
+ end
14
+
15
+ it "should throw a SyntaxError on invalid MultiJson" do
16
+ expect { MultiJson.minify("{ /* foo */ } /* bar ") }.to raise_error(SyntaxError)
17
+ expect { MultiJson.minify(%Q<{ "foo": new Date(1023) }>) }.to raise_error(SyntaxError)
18
+ end
19
+
20
+ it "should cope with the example from https://github.com/getify/MultiJson.minify" do
21
+ expect(MultiJson.minify( %Q'{ /* comment */ "foo": 42 \n }' )).to eql('{"foo":42}')
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json_minify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Geoff Youngs, Alan Cohen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.1'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.14'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.14'
55
+ - !ruby/object:Gem::Dependency
56
+ name: multi_json
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ description: |-
70
+ Pre-parser for JSON that removes C/C++ style comments
71
+ and whitespace from formatted JSON, similar to
72
+ https://github.com/getify/JSON.minify.
73
+ email:
74
+ - git@intersect-uk.co.uk
75
+ - acohen@climate.com
76
+ executables:
77
+ - json_minify
78
+ extensions: []
79
+ extra_rdoc_files: []
80
+ files:
81
+ - bin/json_minify
82
+ - lib/json_minify.rb
83
+ - lib/json_minify/version.rb
84
+ - spec/minify_spec.rb
85
+ homepage: http://github.com/lumberj/json-minify-rb
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.2.1
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: JSON.minify implementation
109
+ test_files:
110
+ - spec/minify_spec.rb