File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- # jsonc - https://pkg.go.dev/github.com/fpatron/jsonc
1+ # jsonc
22
33[ ![ Go CI] ( https://github.com/fpatron/jsonc/actions/workflows/go.yml/badge.svg )] ( https://github.com/fpatron/jsonc/actions/workflows/go.yml )
4+ [ ![ Go Doc] ( https://img.shields.io/badge/godoc-reference-blue.svg )] ( http://godoc.org/github.com/fpatron/jsonc )
45
5- ` jsonc ` is an extension of the JSON data format that allows single and multi line comments and provides accurate parsing errors .
6+ ` jsonc ` is an extension of the JSON data format that allows single and multi line comments.
67
78## Instalation
89To install ` jsonc ` , use ` go get ` :
@@ -13,7 +14,6 @@ To install `jsonc`, use `go get`:
1314## Features
1415
1516- ** Comment Support** : Easily parse JSON files with single-line and multi-line comments.
16- - ** Accurate Errors** : Errors while parsing will match original commented JSON.
1717- ** Simple API** : Uses a familiar API similar to the standard ` encoding/json ` package.
1818
1919## Usage
Original file line number Diff line number Diff line change @@ -18,6 +18,32 @@ func stripComments(data []byte) []byte {
1818 )
1919
2020 state := OUTSIDE
21+ hasComment := false
22+ scanLoop:
23+ for i := 0 ; i < len (data ); i ++ {
24+ switch state {
25+ case OUTSIDE :
26+ if data [i ] == '/' && i + 1 < len (data ) && (data [i + 1 ] == '/' || data [i + 1 ] == '*' ) {
27+ hasComment = true
28+ break scanLoop
29+ }
30+ if data [i ] == '"' {
31+ state = IN_STRING
32+ }
33+ case IN_STRING :
34+ if data [i ] == '\\' && i + 1 < len (data ) {
35+ i ++
36+ } else if data [i ] == '"' {
37+ state = OUTSIDE
38+ }
39+ }
40+ }
41+
42+ if ! hasComment {
43+ return data
44+ }
45+
46+ state = OUTSIDE
2147 result := make ([]byte , 0 , len (data ))
2248
2349 for i := 0 ; i < len (data ); i ++ {
Original file line number Diff line number Diff line change @@ -16,6 +16,32 @@ func stripComments(data []byte) []byte {
1616 )
1717
1818 state := OUTSIDE
19+ hasComment := false
20+ scanLoop:
21+ for i := 0 ; i < len (data ); i ++ {
22+ switch state {
23+ case OUTSIDE :
24+ if data [i ] == '/' && i + 1 < len (data ) && (data [i + 1 ] == '/' || data [i + 1 ] == '*' ) {
25+ hasComment = true
26+ break scanLoop
27+ }
28+ if data [i ] == '"' {
29+ state = IN_STRING
30+ }
31+ case IN_STRING :
32+ if data [i ] == '\\' && i + 1 < len (data ) {
33+ i ++
34+ } else if data [i ] == '"' {
35+ state = OUTSIDE
36+ }
37+ }
38+ }
39+
40+ if ! hasComment {
41+ return data
42+ }
43+
44+ state = OUTSIDE
1945 result := make ([]byte , 0 , len (data ))
2046
2147 for i := 0 ; i < len (data ); i ++ {
You can’t perform that action at this time.
0 commit comments