jsonc 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8181db363d5c3a12bad8d2a213121d4b790b41eef2eb47951a2133ab982e97fe
4
+ data.tar.gz: 9ebd700ac4b38e1db67d6b0820115b2e6d29069a672a9dae079c097159f41a16
5
+ SHA512:
6
+ metadata.gz: 04f4d89a5d3b73ea2eb6d1b9a75d3abeda31e95d23b71e447d8b4fb9573670bfaa74197815255caec9af7021f9135fc750804b50f6e27c7bc24acf0908967d97
7
+ data.tar.gz: 60e5016c2360518153ae492a43948f7980cc4810c66e80ab00014799e0a5c885b5bb2ee488e84123e978df090c6f0736959cd9a3cb1ffb078eb3e498e0e62d89
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,15 @@
1
+ plugins:
2
+ - rubocop-rspec
3
+
4
+ AllCops:
5
+ TargetRubyVersion: 3.0
6
+ NewCops: enable
7
+
8
+ Style/Documentation:
9
+ Enabled: false
10
+
11
+ Style/StringLiterals:
12
+ EnforcedStyle: double_quotes
13
+
14
+ Style/StringLiteralsInInterpolation:
15
+ EnforcedStyle: double_quotes
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-08-24
4
+
5
+ - Initial release
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Yoshiki Takagi
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,86 @@
1
+ # JSONC
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/jsonc.svg)](https://badge.fury.io/rb/jsonc)
4
+
5
+ A simple Ruby gem to parse JSON with Comments (JSONC) and trailing commas.
6
+
7
+ This gem provides `JSONC.parse` and `JSONC.load_file` methods that act as drop-in replacements for the standard `JSON` library, with added support for modern JSON extensions.
8
+
9
+ ## Features
10
+
11
+ - **Comments**: Parses JSON with `//` single-line and `/* */` multi-line comments.
12
+ - **Trailing Commas**: Parses JSON with trailing commas in objects and arrays.
13
+ - **Standard Interface**: Forwards all options (e.g., `symbolize_names`) to the standard `JSON.parse` method.
14
+
15
+ ## Installation
16
+
17
+ Install the gem and add to the application's Gemfile by executing:
18
+
19
+ ```bash
20
+ bundle add jsonc
21
+ ```
22
+
23
+ If bundler is not being used to manage dependencies, install the gem by executing:
24
+
25
+ ```bash
26
+ gem install jsonc
27
+ ```
28
+
29
+ ## Usage
30
+
31
+ ### Parsing a String
32
+
33
+ Use `JSONC.parse` in place of `JSON.parse`.
34
+
35
+ ```ruby
36
+ require 'jsonc'
37
+
38
+ jsonc_string = <<-JSONC
39
+ {
40
+ // The name of the user
41
+ "name": "Jules",
42
+ /*
43
+ The user's role.
44
+ Can be "agent" or "user".
45
+ */
46
+ "role": "agent", // Trailing commas are allowed!
47
+ }
48
+ JSONC
49
+
50
+ parsed_hash = JSONC.parse(jsonc_string)
51
+ # => { "name" => "Jules", "role" => "agent" }
52
+
53
+ puts parsed_hash["name"]
54
+ # => Jules
55
+ ```
56
+
57
+ ### Loading a File
58
+
59
+ Use `JSONC.load_file` in place of `JSON.load_file`.
60
+
61
+ ```ruby
62
+ # Given a file named 'config.jsonc' with the content from above:
63
+
64
+ require 'jsonc'
65
+
66
+ parsed_hash = JSONC.load_file('config.jsonc', symbolize_names: true)
67
+ # => { name: "Jules", role: "agent" }
68
+ ```
69
+
70
+ ## Development
71
+
72
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
73
+
74
+ To install this gem onto your local machine, run `bundle exec rake install`.
75
+
76
+ ## Contributing
77
+
78
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ytkg/jsonc. 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/ytkg/jsonc/blob/main/CODE_OF_CONDUCT.md).
79
+
80
+ ## License
81
+
82
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
83
+
84
+ ## Code of Conduct
85
+
86
+ Everyone interacting in the JSONC project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/ytkg/jsonc/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JSONC
4
+ class Parser
5
+ class CommentRemover
6
+ def initialize(string)
7
+ @string = string
8
+ @index = 0
9
+ @result = String.new
10
+ @state = :normal
11
+ end
12
+
13
+ def parse
14
+ while @index < @string.length
15
+ case @state
16
+ when :normal then parse_normal
17
+ when :in_string then parse_string
18
+ when :in_line_comment then parse_line_comment
19
+ when :in_block_comment then parse_block_comment
20
+ end
21
+ end
22
+ raise JSON::ParserError, "Unclosed block comment" if @state == :in_block_comment
23
+
24
+ @result
25
+ end
26
+
27
+ private
28
+
29
+ # rubocop:disable Metrics/MethodLength
30
+ def parse_normal
31
+ char = @string[@index]
32
+ if char == '"'
33
+ @state = :in_string
34
+ @result << char
35
+ @index += 1
36
+ elsif char == "/" && @string[@index + 1] == "/"
37
+ @state = :in_line_comment
38
+ @index += 2
39
+ elsif char == "/" && @string[@index + 1] == "*"
40
+ @state = :in_block_comment
41
+ @index += 2
42
+ else
43
+ @result << char
44
+ @index += 1
45
+ end
46
+ end
47
+ # rubocop:enable Metrics/MethodLength
48
+
49
+ # rubocop:disable Metrics/MethodLength
50
+ def parse_string
51
+ char = @string[@index]
52
+ if char == "\\"
53
+ @result << char << @string[@index + 1]
54
+ @index += 2
55
+ elsif char == '"'
56
+ @state = :normal
57
+ @result << char
58
+ @index += 1
59
+ else
60
+ @result << char
61
+ @index += 1
62
+ end
63
+ end
64
+ # rubocop:enable Metrics/MethodLength
65
+
66
+ def parse_line_comment
67
+ char = @string[@index]
68
+ if char == "\n"
69
+ @state = :normal
70
+ @result << char
71
+ end
72
+ @index += 1
73
+ end
74
+
75
+ def parse_block_comment
76
+ char = @string[@index]
77
+ if char == "*" && @string[@index + 1] == "/"
78
+ @state = :normal
79
+ @index += 2
80
+ else
81
+ @result << "\n" if char == "\n"
82
+ @index += 1
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JSONC
4
+ class Parser
5
+ class TrailingCommaRemover
6
+ def initialize(string)
7
+ @string = string
8
+ @index = 0
9
+ @result = String.new
10
+ @state = :normal
11
+ end
12
+
13
+ def parse
14
+ while @index < @string.length
15
+ case @state
16
+ when :normal then parse_normal
17
+ when :in_string then parse_string
18
+ end
19
+ end
20
+ @result
21
+ end
22
+
23
+ private
24
+
25
+ def parse_normal
26
+ char = @string[@index]
27
+ if char == '"'
28
+ @state = :in_string
29
+ @result << char
30
+ elsif char == ","
31
+ @result << char unless find_next_meaningful_char_is_closing_bracket?
32
+ else
33
+ @result << char
34
+ end
35
+ @index += 1
36
+ end
37
+
38
+ def parse_string # rubocop:disable Metrics/MethodLength
39
+ char = @string[@index]
40
+ if char == "\\"
41
+ @result << char << @string[@index + 1]
42
+ @index += 2
43
+ elsif char == '"'
44
+ @state = :normal
45
+ @result << char
46
+ @index += 1
47
+ else
48
+ @result << char
49
+ @index += 1
50
+ end
51
+ end
52
+
53
+ def find_next_meaningful_char_is_closing_bracket?
54
+ i = @index + 1
55
+ while i < @string.length
56
+ char = @string[i]
57
+ return true if ["]", "}"].include?(char)
58
+ return false unless char.strip.empty?
59
+
60
+ i += 1
61
+ end
62
+ false
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "parser/comment_remover"
4
+ require_relative "parser/trailing_comma_remover"
5
+
6
+ module JSONC
7
+ # The main parser class. It orchestrates the removal of comments and trailing commas.
8
+ class Parser
9
+ def self.parse(string)
10
+ comment_free_string = CommentRemover.new(string).parse
11
+ TrailingCommaRemover.new(comment_free_string).parse
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JSONC
4
+ VERSION = "0.1.0"
5
+ end
data/lib/jsonc.rb ADDED
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require_relative "jsonc/version"
5
+ require_relative "jsonc/parser"
6
+
7
+ module JSONC
8
+ class Error < StandardError; end
9
+
10
+ def self.parse(string, **opts)
11
+ sanitized_string = Parser.parse(string)
12
+ JSON.parse(sanitized_string, **opts)
13
+ end
14
+
15
+ def self.load_file(path, **opts)
16
+ parse(File.read(path), **opts)
17
+ end
18
+ end
data/sig/jsonc.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Jsonc
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsonc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yoshiki Takagi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-08-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple parser for JSONC (JSON with comments).
14
+ email:
15
+ - yoshiki.tkg@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - CHANGELOG.md
23
+ - CODE_OF_CONDUCT.md
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - lib/jsonc.rb
28
+ - lib/jsonc/parser.rb
29
+ - lib/jsonc/parser/comment_remover.rb
30
+ - lib/jsonc/parser/trailing_comma_remover.rb
31
+ - lib/jsonc/version.rb
32
+ - sig/jsonc.rbs
33
+ homepage: https://github.com/ytkg/jsonc
34
+ licenses:
35
+ - MIT
36
+ metadata:
37
+ allowed_push_host: https://rubygems.org
38
+ homepage_uri: https://github.com/ytkg/jsonc
39
+ source_code_uri: https://github.com/ytkg/jsonc
40
+ changelog_uri: https://github.com/ytkg/jsonc/blob/main/CHANGELOG.md
41
+ rubygems_mfa_required: 'true'
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 3.0.0
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubygems_version: 3.5.16
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: A simple parser for JSON with comments (JSONC).
61
+ test_files: []