Skip to content

Commit

Permalink
Remove old jsonc library and allow for comments at the top of the file
Browse files Browse the repository at this point in the history
  • Loading branch information
stirante committed Sep 28, 2022
1 parent 589ddab commit 2e1c56d
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 32 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ require (
github.com/fatih/color v1.13.0
github.com/gammazero/deque v0.2.0
github.com/gobwas/glob v0.2.3
github.com/stirante/jsonc v0.0.0-20220913161200-c4ec34866fdd
go.uber.org/zap v1.23.0
golang.org/x/text v0.3.7
)
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stirante/jsonc v0.0.0-20220913161200-c4ec34866fdd h1:+s6imB5Hhq+xYfNn+KIIbYpU6jUGr8MNqwu9ZzofsMk=
github.com/stirante/jsonc v0.0.0-20220913161200-c4ec34866fdd/go.mod h1:cZCwzwRtBT6mI2NyjisNOQHJWSbEVjUASlGxQFgrU1w=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
Expand Down
25 changes: 11 additions & 14 deletions jsonte/functions/minecraft_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"archive/zip"
"github.com/MCDevKit/jsonte/jsonte/safeio"
"github.com/MCDevKit/jsonte/jsonte/utils"
"github.com/stirante/jsonc"
"io"
"io/ioutil"
"os"
Expand Down Expand Up @@ -413,22 +412,20 @@ func findPackVersions(isBp bool, uuid string) (utils.NavigableMap[string, string
}
return versions, utils.WrapErrorf(err, "Failed to read manifest.json in %s", p)
}
var manifest map[string]interface{}
err = jsonc.Unmarshal(f, &manifest)
var manifest utils.NavigableMap[string, interface{}]
manifest, err = utils.ParseJson(f)
if err != nil {
return versions, utils.WrapErrorf(err, "Failed to parse manifest.json in %s", p)
}
if header, ok := manifest["header"]; ok {
if headerMap, ok := header.(map[string]interface{}); ok {
if headerMap["uuid"] != uuid {
continue
}
if version, ok := headerMap["version"]; ok {
if versionArr, ok := version.([]interface{}); ok {
array := utils.ParseSemverArray(versionArr)
versions.Put(array.String(), p)
}
}
if manifest.ContainsKey("header") {
header := manifest.Get("header").(utils.NavigableMap[string, interface{}])
if header.Get("uuid") != uuid {
continue
}
if header.ContainsKey("version") {
version := utils.UnwrapContainers(header.Get("version")).([]interface{})
array := utils.ParseSemverArray(version)
versions.Put(array.String(), p)
}
}
}
Expand Down
26 changes: 15 additions & 11 deletions jsonte/utils/json_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,20 @@ func isWhitespace(token byte) bool {
}

func skipWhitespace(str *StringReader) {
canStartComment := false
for {
token := str.Peek()
if !isWhitespace(token) {
break
if isWhitespace(token) {
str.Read()
continue
} else if token == TokenSlash {
canStartComment = parseComment(str, canStartComment)
continue
} else if token == TokenAsterisk {
canStartComment = parseComment(str, canStartComment)
continue
}
str.Read()
break
}
}

Expand All @@ -271,11 +279,9 @@ func parseObject(str *StringReader, p string) (NavigableMap[string, interface{}]
return result, WrappedJsonErrorf(p, "Unexpected end of file at line %d, column %d", str.line, str.column)
} else if isWhitespace(token) {
str.Read()
} else if token == TokenSlash && !canStartComment {
} else if token == TokenSlash {
canStartComment = parseComment(str, canStartComment)
} else if token == TokenSlash && canStartComment {
canStartComment = parseComment(str, canStartComment)
} else if token == TokenAsterisk && canStartComment {
} else if token == TokenAsterisk {
canStartComment = parseComment(str, canStartComment)
} else if token == TokenDoubleQuote && (comma || open) {
comma = false
Expand Down Expand Up @@ -459,11 +465,9 @@ func parseArray(str *StringReader, p string) ([]interface{}, error) {
return result, WrappedJsonErrorf(p, "Unexpected token at line %d, column %d", str.line, str.column)
} else if isWhitespace(token) {
str.Read()
} else if token == TokenSlash && !canStartComment {
canStartComment = parseComment(str, canStartComment)
} else if token == TokenSlash && canStartComment {
} else if token == TokenSlash {
canStartComment = parseComment(str, canStartComment)
} else if token == TokenAsterisk && canStartComment {
} else if token == TokenAsterisk {
canStartComment = parseComment(str, canStartComment)
} else if token == TokenOpenArray && (comma || open) {
value, err := parseArray(str, fmt.Sprintf("%s[%d]", p, len(result)))
Expand Down
4 changes: 2 additions & 2 deletions jsonte/utils/json_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func ToString(obj interface{}) string {
if b, ok := obj.(string); ok {
return b
}
str, err := MarshalJSONCObject(UnwrapContainers(obj).(NavigableMap[string, interface{}]), false, false)
str, err := MarshalJSONCObject(UnwrapContainers(obj).(NavigableMap[string, interface{}]), false)
if err != nil {
return "null"
}
Expand Down Expand Up @@ -246,7 +246,7 @@ func ToPrettyString(obj interface{}) string {
if b, ok := obj.(string); ok {
return b
}
str, err := MarshalJSONCObject(UnwrapContainers(obj).(NavigableMap[string, interface{}]), true, true)
str, err := MarshalJSONCObject(UnwrapContainers(obj).(NavigableMap[string, interface{}]), true)
if err != nil {
return "null"
}
Expand Down
2 changes: 1 addition & 1 deletion jsonte/utils/semver.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ func ParseSemverString(version string) (Semver, error) {

// ParseSemverArray parses an array representation of a semantic version
func ParseSemverArray(version []interface{}) Semver {
return Semver{int(version[0].(float64)), int(version[1].(float64)), int(version[2].(float64))}
return Semver{int(version[0].(int32)), int(version[1].(int32)), int(version[2].(int32))}
}
2 changes: 1 addition & 1 deletion test/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ func TestModuleOverride(t *testing.T) {
}

func TestJsonParser(t *testing.T) {
template := `{
template := `/*comment*/{
"obj": {
// This is a comment!
"decimal": 0.0,
Expand Down

0 comments on commit 2e1c56d

Please sign in to comment.