Skip to content

Commit

Permalink
Merge pull request #12 from fivitti/master
Browse files Browse the repository at this point in the history
Add support for hash style comments
  • Loading branch information
muhammadmuzzammil1998 authored Dec 30, 2021
2 parents 95c35fd + 4bc0e2e commit a1565b7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
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
}

0 comments on commit a1565b7

Please sign in to comment.