prettify_json 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.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +142 -0
- data/bin/prettify_json +98 -0
- data/lib/prettify_json/version.rb +3 -0
- data/lib/prettify_json.rb +131 -0
- metadata +70 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d86347bba51955e7614b451d3170173d7c573e34b96983ee35d985c2f45c2e1e
|
4
|
+
data.tar.gz: c316527c4c6c2a0722732db77856ec18f7adebd62d8ef534c8bf5da2884cbaff
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 79c18a2224eac2b6b2191aff72408f6ddfb3dd0f269f8b80ad74094b5e401848d1b99ab5fc1027f310887390fd4abd07d1328bbec0e1727171cfeed5645f4caa
|
7
|
+
data.tar.gz: 9705c8b350aa01cb71eca5763c20b998734ade52c1cd4712d242b809dc10edf0947b3803f35b692744e8f059deeed3bb97d40cc9239fc0f55a123eadde0b25c8
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 talaatmagdyx
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
# PrettifyJson
|
2
|
+
|
3
|
+
`prettify_json` is a powerful command-line tool designed to enhance the readability of JSON data. Whether you're working with JSON files, strings, or data streamed from the web, `prettify_json` will format it in a clean, colorful, and human-friendly way.
|
4
|
+
|
5
|
+
## Features
|
6
|
+
|
7
|
+
- **Readability**: Formats JSON data for easier reading.
|
8
|
+
- **Flexibility**: Accepts input from files, strings, or standard input.
|
9
|
+
- **Colorful Output**: Adds colors to the JSON keys, strings, numbers, and more for better visual distinction.
|
10
|
+
|
11
|
+
## Dependencies
|
12
|
+
|
13
|
+
- Ruby (version 2.7.0 or higher)
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
gem 'prettify_json'
|
21
|
+
```
|
22
|
+
|
23
|
+
And then execute:
|
24
|
+
|
25
|
+
```sh
|
26
|
+
$ bundle install
|
27
|
+
```
|
28
|
+
|
29
|
+
Or install it yourself as:
|
30
|
+
|
31
|
+
```sh
|
32
|
+
$ gem install prettify_json
|
33
|
+
```
|
34
|
+
|
35
|
+
## Usage
|
36
|
+
|
37
|
+
### From a File
|
38
|
+
|
39
|
+
Prettify JSON data from a file:
|
40
|
+
|
41
|
+
```sh
|
42
|
+
$ prettify_json -f path/to/your/file.json
|
43
|
+
```
|
44
|
+
|
45
|
+
### From a String
|
46
|
+
|
47
|
+
Prettify JSON data from a string:
|
48
|
+
|
49
|
+
```sh
|
50
|
+
$ prettify_json -s '{"key": "value"}'
|
51
|
+
```
|
52
|
+
|
53
|
+
### From Standard Input
|
54
|
+
|
55
|
+
Prettify JSON data from standard input:
|
56
|
+
|
57
|
+
```sh
|
58
|
+
$ echo '{"key": "value"}' | prettify_json -
|
59
|
+
```
|
60
|
+
|
61
|
+
### From a Web API
|
62
|
+
|
63
|
+
Fetch and prettify JSON data from a web API:
|
64
|
+
|
65
|
+
```sh
|
66
|
+
$ curl -s https://api.restful-api.dev/objects | prettify_json -
|
67
|
+
```
|
68
|
+
|
69
|
+
## Examples
|
70
|
+
|
71
|
+
### Example 1: Prettifying a JSON File
|
72
|
+
|
73
|
+
```sh
|
74
|
+
$ prettify_json -f sample.json
|
75
|
+
|
76
|
+
{
|
77
|
+
"name": "John Doe",
|
78
|
+
"age": 30,
|
79
|
+
"city": "New York"
|
80
|
+
}
|
81
|
+
```
|
82
|
+
|
83
|
+
### Example 2: Prettifying a JSON String
|
84
|
+
|
85
|
+
```sh
|
86
|
+
$ prettify_json -s '{"name": "John Doe", "age": 30, "city": "New York"}'
|
87
|
+
|
88
|
+
{
|
89
|
+
"name": "John Doe",
|
90
|
+
"age": 30,
|
91
|
+
"city": "New York"
|
92
|
+
}
|
93
|
+
```
|
94
|
+
|
95
|
+
### Example 3: Prettifying JSON from a REST API
|
96
|
+
|
97
|
+
```sh
|
98
|
+
$ curl -s https://api.restful-api.dev/objects | prettify_json -
|
99
|
+
|
100
|
+
[
|
101
|
+
{
|
102
|
+
"id": 1,
|
103
|
+
"name": "Object 1",
|
104
|
+
"data": {
|
105
|
+
"value": "Sample data"
|
106
|
+
}
|
107
|
+
},
|
108
|
+
{
|
109
|
+
"id": 2,
|
110
|
+
"name": "Object 2",
|
111
|
+
"data": {
|
112
|
+
"value": "More sample data"
|
113
|
+
}
|
114
|
+
}
|
115
|
+
]
|
116
|
+
```
|
117
|
+
|
118
|
+
## Development
|
119
|
+
|
120
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
121
|
+
|
122
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `lib/prettify_json/version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
123
|
+
|
124
|
+
## Contributing
|
125
|
+
|
126
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/talaatmagdyx/prettify_json. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/talaatmagdyx/prettify_json/blob/master/CODE_OF_CONDUCT.md).
|
127
|
+
|
128
|
+
## Reporting Bugs / Feature Requests
|
129
|
+
|
130
|
+
If you encounter any bugs or have suggestions for new features, please [open an issue on GitHub](https://github.com/talaatmagdyx/prettify_json/issues). Your feedback is valuable and helps improve the quality of the gem.
|
131
|
+
|
132
|
+
## License
|
133
|
+
|
134
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
135
|
+
|
136
|
+
## Code of Conduct
|
137
|
+
|
138
|
+
Everyone interacting in the PrettifyJson project's codebases, issue trackers, chat rooms, and mailing lists is expected to follow the [code of conduct](https://github.com/talaatmagdyx/prettify_json/blob/master/CODE_OF_CONDUCT.md).
|
139
|
+
|
140
|
+
---
|
141
|
+
|
142
|
+
This README provides a comprehensive guide to using your `prettify_json` gem, with detailed usage examples and instructions for installation, development, and contribution.
|
data/bin/prettify_json
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'prettify_json'
|
4
|
+
require 'optparse'
|
5
|
+
|
6
|
+
# Display help message for usage instructions
|
7
|
+
def display_help
|
8
|
+
puts help_message
|
9
|
+
exit 0
|
10
|
+
end
|
11
|
+
|
12
|
+
# Help message content
|
13
|
+
def help_message
|
14
|
+
<<~HELP
|
15
|
+
Usage:
|
16
|
+
prettify_json [options]
|
17
|
+
|
18
|
+
[Options]
|
19
|
+
-f, --file FILE Input JSON file
|
20
|
+
-s, --string JSON Input JSON string
|
21
|
+
- Read multiple lines from standard input (no single quotes needed)
|
22
|
+
|
23
|
+
You may specify a file path or a JSON string to be parsed and formatted.
|
24
|
+
IMPORTANT: make sure to surround your JSON string with single quotes to prevent the shell from splitting it by its own.
|
25
|
+
HELP
|
26
|
+
end
|
27
|
+
|
28
|
+
# Parse command line options and arguments
|
29
|
+
def parse_options
|
30
|
+
options = {}
|
31
|
+
opts = option_parser(options)
|
32
|
+
opts.parse!
|
33
|
+
options
|
34
|
+
end
|
35
|
+
|
36
|
+
# OptionParser configuration
|
37
|
+
def option_parser(options)
|
38
|
+
OptionParser.new do |opts|
|
39
|
+
opts.banner = 'Usage: prettify_json [options]'
|
40
|
+
add_file_option(opts, options)
|
41
|
+
add_string_option(opts, options)
|
42
|
+
add_help_option(opts)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Add file option to OptionParser
|
47
|
+
def add_file_option(opts, options)
|
48
|
+
opts.on('-f', '--file FILE', 'Input JSON file') do |file|
|
49
|
+
options[:file] = file
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# Add string option to OptionParser
|
54
|
+
def add_string_option(opts, options)
|
55
|
+
opts.on('-s', '--string JSON', 'Input JSON string') do |json_string|
|
56
|
+
options[:string] = json_string
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# Add help option to OptionParser
|
61
|
+
def add_help_option(opts)
|
62
|
+
opts.on('-h', '--help', 'Show this help message') do
|
63
|
+
display_help
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# Read JSON input from the specified source
|
68
|
+
def read_input(options)
|
69
|
+
if options[:file]
|
70
|
+
File.read(options[:file])
|
71
|
+
elsif options[:string]
|
72
|
+
options[:string]
|
73
|
+
elsif ARGV.include?('-')
|
74
|
+
$stdin.read
|
75
|
+
else
|
76
|
+
display_help
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# Pretty print JSON input
|
81
|
+
def pretty_print_json(input)
|
82
|
+
puts PrettifyJson::JsonPrettifier.new(input).pretty_print
|
83
|
+
rescue JSON::ParserError => e
|
84
|
+
puts "Invalid JSON: #{e.message}"
|
85
|
+
end
|
86
|
+
|
87
|
+
# Main execution logic
|
88
|
+
def main
|
89
|
+
options = parse_options
|
90
|
+
|
91
|
+
display_help if options.empty? && ARGV.empty?
|
92
|
+
|
93
|
+
input = read_input(options)
|
94
|
+
pretty_print_json(input)
|
95
|
+
end
|
96
|
+
|
97
|
+
# Execute main method
|
98
|
+
main
|
@@ -0,0 +1,131 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'optparse'
|
3
|
+
require 'rainbow'
|
4
|
+
require_relative 'prettify_json/version'
|
5
|
+
|
6
|
+
module PrettifyJson
|
7
|
+
class JsonPrettifier
|
8
|
+
attr_reader :json_string
|
9
|
+
|
10
|
+
def initialize(json_string)
|
11
|
+
@json_string = json_string
|
12
|
+
end
|
13
|
+
|
14
|
+
def colorize_json(json_string)
|
15
|
+
regex = /("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(?:\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+-]?\d+)?)/
|
16
|
+
|
17
|
+
json_string.gsub(regex) { |match| colorize_match(match) }
|
18
|
+
end
|
19
|
+
|
20
|
+
def colorize_match(match)
|
21
|
+
case match
|
22
|
+
when /^"/
|
23
|
+
match.include?(':') ? Rainbow(match).yellow : Rainbow(match).green
|
24
|
+
when 'true', 'false'
|
25
|
+
Rainbow(match).blue
|
26
|
+
when 'null'
|
27
|
+
Rainbow(match).magenta
|
28
|
+
else
|
29
|
+
Rainbow(match).red
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def pretty_print
|
34
|
+
parsed_json = JSON.parse(json_string)
|
35
|
+
pretty_json = JSON.pretty_generate(parsed_json)
|
36
|
+
colorize_json(pretty_json)
|
37
|
+
rescue JSON::ParserError => e
|
38
|
+
"Invalid JSON: #{e.message}"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
class CLI
|
43
|
+
def self.parse_options(argv = ARGV)
|
44
|
+
options = {}
|
45
|
+
opts = option_parser(options)
|
46
|
+
opts.parse!(argv)
|
47
|
+
options
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.option_parser(options)
|
51
|
+
OptionParser.new do |opts|
|
52
|
+
opts.banner = "Usage: #{File.basename($PROGRAM_NAME)} [options]"
|
53
|
+
add_file_option(opts, options)
|
54
|
+
add_string_option(opts, options)
|
55
|
+
add_help_option(opts)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.add_file_option(opts, options)
|
60
|
+
opts.on('-f', '--file FILE', 'Input JSON file') do |file|
|
61
|
+
options[:file] = file
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def self.add_string_option(opts, options)
|
66
|
+
opts.on('-s', '--string JSON', 'Input JSON string') do |json_string|
|
67
|
+
options[:string] = json_string
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.add_help_option(opts)
|
72
|
+
opts.on('-h', '--help', 'Show this help message') do
|
73
|
+
puts opts
|
74
|
+
exit
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def self.read_input(options)
|
79
|
+
if options[:file]
|
80
|
+
File.read(options[:file])
|
81
|
+
elsif options[:string]
|
82
|
+
options[:string]
|
83
|
+
elsif ARGV.include?('-')
|
84
|
+
$stdin.read
|
85
|
+
else
|
86
|
+
display_help
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def self.pretty_print_json(input)
|
91
|
+
puts PrettifyJson::JsonPrettifier.new(input).pretty_print
|
92
|
+
rescue JSON::ParserError => e
|
93
|
+
puts "Invalid JSON: #{e.message}"
|
94
|
+
end
|
95
|
+
|
96
|
+
def self.display_help
|
97
|
+
puts help_message
|
98
|
+
exit 0
|
99
|
+
end
|
100
|
+
|
101
|
+
def self.help_message
|
102
|
+
<<~HELP
|
103
|
+
Usage:
|
104
|
+
prettify_json [options]
|
105
|
+
|
106
|
+
[Options]
|
107
|
+
-f, --file FILE Input JSON file
|
108
|
+
-s, --string JSON Input JSON string
|
109
|
+
- Read multiple lines from standard input (no single quotes needed)
|
110
|
+
|
111
|
+
You may specify a file path or a JSON string to be parsed and formatted.
|
112
|
+
IMPORTANT: make sure to surround your JSON string with single quotes to prevent the shell from splitting it by its own.
|
113
|
+
HELP
|
114
|
+
end
|
115
|
+
|
116
|
+
def self.main(argv = ARGV)
|
117
|
+
options = parse_options(argv)
|
118
|
+
|
119
|
+
display_help if options.empty? && argv.empty?
|
120
|
+
|
121
|
+
input = read_input(options)
|
122
|
+
pretty_print_json(input)
|
123
|
+
end
|
124
|
+
|
125
|
+
public_class_method :main
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
if __FILE__ == $PROGRAM_NAME
|
130
|
+
PrettifyJson::CLI.main
|
131
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: prettify_json
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- talaatmagdyx
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-05-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rainbow
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.0'
|
27
|
+
description: A simple command line tool to pretty-print JSON with colors using Ruby.
|
28
|
+
email:
|
29
|
+
- talaatmagdy75@gmail.com
|
30
|
+
executables:
|
31
|
+
- prettify_json
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- LICENSE.txt
|
36
|
+
- README.md
|
37
|
+
- bin/prettify_json
|
38
|
+
- lib/prettify_json.rb
|
39
|
+
- lib/prettify_json/version.rb
|
40
|
+
homepage: https://github.com/talaatmagdyx/prettify_json
|
41
|
+
licenses:
|
42
|
+
- MIT
|
43
|
+
metadata:
|
44
|
+
homepage_uri: https://github.com/talaatmagdyx/prettify_json
|
45
|
+
source_code_uri: https://github.com/talaatmagdyx/prettify_json/main
|
46
|
+
changelog_uri: https://github.com/talaatmagdyx/prettify_json/blob/main/CHANGELOG.md
|
47
|
+
bug_tracker_uri: https://github.com/talaatmagdyx/prettify_json/issues
|
48
|
+
wiki_uri: https://github.com/talaatmagdyx/prettify_json/wiki
|
49
|
+
rubygems_mfa_required: 'true'
|
50
|
+
post_install_message: Thanks for installing! prettify_json is A simple command line
|
51
|
+
tool to pretty-print JSON with colors using Ruby.
|
52
|
+
rdoc_options: []
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: 2.7.0
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
requirements: []
|
66
|
+
rubygems_version: 3.5.10
|
67
|
+
signing_key:
|
68
|
+
specification_version: 4
|
69
|
+
summary: A CLI tool to pretty-print JSON with colors.
|
70
|
+
test_files: []
|