Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for hash style comments #12

Merged
merged 3 commits into from
Dec 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<i><a href="https://travis-ci.com/muhammadmuzzammil1998/jsonc" target="_blank"><img src="https://travis-ci.com/muhammadmuzzammil1998/jsonc.svg?branch=master" alt="travisci"></a></i>
</p>

JSONC is a superset of JSON which supports comments. JSON formatted files are readable to humans but the lack of comments decreases readability. With JSONC, you can use block (`/* */`) and single line (`//`) comments to describe the functionality. Microsoft VS Code also uses this format in their configuration files like `settings.json`, `keybindings.json`, `launch.json`, etc.
JSONC is a superset of JSON which supports comments. JSON formatted files are readable to humans but the lack of comments decreases readability. With JSONC, you can use block (`/* */`) and single line (`//` of `#`) comments to describe the functionality. Microsoft VS Code also uses this format in their configuration files like `settings.json`, `keybindings.json`, `launch.json`, etc.

![jsonc](carbon.png)

Expand Down
15 changes: 15 additions & 0 deletions jsonc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type testsStruct struct {
validSingle []byte
invalidBlock []byte
invalidSingle []byte
validHash []byte
invalidHash []byte
}

var jsonTest, jsoncTest testsStruct
Expand All @@ -30,6 +32,8 @@ func init() {
invalidBlock: b(`{"foo": /* this is a block comment "bar foo", "true": false, "number": 42, "object": { "test": "done" }, "array" : [1, 2, 3], "url" : "https://github.com", "escape":"\"wo//rking }`),
validSingle: b("{\"foo\": // this is a single line comm\\\"ent\n\"bar foo\", \"true\": false, \"number\": 42, \"object\": { \"test\": \"done\" }, \"array\" : [1, 2, 3], \"url\" : \"https://github.com\", \"escape\":\"\\\"wo//rking\" }"),
invalidSingle: b(`{"foo": // this is a single line comment "bar foo", "true": false, "number": 42, "object": { "test": "done" }, "array" : [1, 2, 3], "url" : "https://github.com", "escape":"\"wo//rking" }`),
validHash: b("{\"foo\": # this is a single line comm\\\"ent\n\"bar foo\", \"true\": false, \"number\": 42, \"object\": { \"test\": \"done\" }, \"array\" : [1, 2, 3], \"url\" : \"https://github.com\", \"escape\":\"\\\"wo//rking\" }"),
invalidHash: b(`{"foo": # this is a single line comment "bar foo", "true": false, "number": 42, "object": { "test": "done" }, "array" : [1, 2, 3], "url" : "https://github.com", "escape":"\"wo//rking" }`),
}
}

Expand Down Expand Up @@ -58,6 +62,15 @@ func TestToJSON(t *testing.T) {
arg: jsoncTest.invalidSingle,
want: jsonTest.invalidBlock,
wantErr: true,
}, {
name: "Test for valid single line comment started with hash.",
arg: jsoncTest.validHash,
want: jsonTest.validBlock,
}, {
name: "Test for invalid single line comment started with hash.",
arg: jsoncTest.invalidHash,
want: jsonTest.invalidBlock,
wantErr: true,
},
}
for _, tt := range tests {
Expand Down Expand Up @@ -185,12 +198,14 @@ func BenchmarkTranslate(b *testing.B) {
for n := 0; n < b.N; n++ {
translate(jsoncTest.validSingle)
translate(jsoncTest.validBlock)
translate(jsoncTest.validHash)
}
}

func BenchmarkValid(b *testing.B) {
for n := 0; n < b.N; n++ {
Valid(jsoncTest.validSingle)
Valid(jsoncTest.validBlock)
Valid(jsoncTest.validHash)
}
}
7 changes: 6 additions & 1 deletion translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
NEWLINE = 10
ASTERISK = 42
SLASH = 47
HASH = 35
)

func translate(s []byte) []byte {
Expand Down Expand Up @@ -86,6 +87,10 @@ func translate(s []byte) []byte {
comment.canStart = true
continue
}
if ch == HASH {
comment.start(ch)
continue
}
j[i] = ch
i++
}
Expand All @@ -107,5 +112,5 @@ func (c *commentData) stop() {

func (c *commentData) start(ch byte) {
c.startted = true
c.isSingleLined = ch == SLASH
c.isSingleLined = ch == SLASH || ch == HASH
}