Skip to content
Draft
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
4 changes: 2 additions & 2 deletions edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Edit struct {
// insert the value (given the existing properties, in order).
//
// Source: https://github.com/Microsoft/vscode/blob/c0bc1ace7ca3ce2d6b1aeb2bde9d1bb0f4b4bae6/src/vs/base/common/jsonEdit.ts#L14
func ComputePropertyEdit(text string, path Path, value interface{}, insertionIndex func(properties []string) int, options FormatOptions) ([]Edit, []ParseErrorCode, error) {
func ComputePropertyEdit(text string, path Path, value any, insertionIndex func(properties []string) int, options FormatOptions) ([]Edit, []ParseErrorCode, error) {
if value == nil {
value = json.RawMessage("null") // otherwise would remove property
}
Expand All @@ -45,7 +45,7 @@ func ComputePropertyRemoval(text string, path Path, options FormatOptions) ([]Ed
return computePropertyEdit(text, path, nil, nil, options)
}

func computePropertyEdit(text string, path Path, valueObj interface{}, insertionIndex func(properties []string) int, options FormatOptions) ([]Edit, []ParseErrorCode, error) {
func computePropertyEdit(text string, path Path, valueObj any, insertionIndex func(properties []string) int, options FormatOptions) ([]Edit, []ParseErrorCode, error) {
// Tolerate errors in value if it's json.RawMessage.
var value string
if v, ok := valueObj.(json.RawMessage); ok {
Expand Down
2 changes: 1 addition & 1 deletion edit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestComputePropertyEdit(t *testing.T) {
type testCase struct {
input string
path Path
value interface{}
value any
remove bool
insertIndex func(properties []string) int
options *FormatOptions
Expand Down
5 changes: 1 addition & 4 deletions format.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,7 @@ func (f *formatter) format(rangeStart, rangeEnd int) (editOperations []Edit) {
}

func (f *formatter) newLineAndIndent() string {
n := f.initialIndentLevel + f.indentLevel
if n < 0 {
n = 0
}
n := max(f.initialIndentLevel+f.indentLevel, 0)
return f.eol + strings.Repeat(f.indentValue, n)
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/sourcegraph/jsonx

go 1.25.9
go 1.26.4
14 changes: 7 additions & 7 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ func ParseWithDetailedErrors(text string, options ParseOptions) ([]byte, []Parse
valid bool
}
type parent struct {
array *[]interface{}
object map[string]interface{}
array *[]any
object map[string]any
}
currentParent := parent{array: &[]interface{}{}}
currentParent := parent{array: &[]any{}}
previousParents := []parent{}

onValue := func(value interface{}) {
onValue := func(value any) {
if currentParent.array != nil {
*currentParent.array = append(*currentParent.array, value)
} else if currentProperty.valid {
Expand All @@ -62,7 +62,7 @@ func ParseWithDetailedErrors(text string, options ParseOptions) ([]byte, []Parse
var errors []ParseError
visitor := Visitor{
OnObjectBegin: func(offset, length int) {
object := map[string]interface{}{}
object := map[string]any{}
onValue(object)
previousParents = append(previousParents, currentParent)
currentParent = parent{object: object}
Expand All @@ -78,7 +78,7 @@ func ParseWithDetailedErrors(text string, options ParseOptions) ([]byte, []Parse
previousParents = previousParents[:len(previousParents)-1]
},
OnArrayBegin: func(offset, length int) {
array := &[]interface{}{}
array := &[]any{}
onValue(array)
previousParents = append(previousParents, currentParent)
currentParent = parent{array: array}
Expand All @@ -89,7 +89,7 @@ func ParseWithDetailedErrors(text string, options ParseOptions) ([]byte, []Parse
currentParent = previousParents[len(previousParents)-1]
previousParents = previousParents[:len(previousParents)-1]
},
OnLiteralValue: func(value interface{}, offset, length int) {
OnLiteralValue: func(value any, offset, length int) {
onValue(value)
},
OnError: func(errorCode ParseErrorCode, offset, length int) {
Expand Down
28 changes: 14 additions & 14 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ const (
//
// Source: https://github.com/Microsoft/vscode/blob/c0bc1ace7ca3ce2d6b1aeb2bde9d1bb0f4b4bae6/src/vs/base/common/json.ts#L616
type Node struct {
Type NodeType // the node's type
Value interface{} // the node's value
Offset int // character offset of the node's starting position in the document
Length int // the length (in characters) of the node
ColumnOffset int // character offset of the property's separator
Parent *Node // the node's parent or nil if this is the root node
Children []*Node // the node's children
Type NodeType // the node's type
Value any // the node's value
Offset int // character offset of the node's starting position in the document
Length int // the length (in characters) of the node
ColumnOffset int // character offset of the property's separator
Parent *Node // the node's parent or nil if this is the root node
Children []*Node // the node's children
}

// A Segment is a component of a JSON key path. It is either an object
Expand Down Expand Up @@ -64,7 +64,7 @@ func (s Segment) MarshalJSON() ([]byte, error) {
// UnmarshalJSON implements json.Unmarshaler.
func (s *Segment) UnmarshalJSON(data []byte) error {
var t Segment
var target interface{}
var target any
if len(data) > 0 && data[0] == '"' {
t.IsProperty = true
target = &t.Property
Expand Down Expand Up @@ -98,7 +98,7 @@ func PropertyPath(properties ...string) Path {
// each of which may be either a string (which is treated as a property segment)
// or an int (which is treated as an array index). Any other type causes it
// to panic.
func MakePath(components ...interface{}) Path {
func MakePath(components ...any) Path {
segments := make([]Segment, len(components))
for i, c := range components {
switch v := c.(type) {
Expand Down Expand Up @@ -154,7 +154,7 @@ func ParseTree(text string, options ParseOptions) (root *Node, errors []ParseErr
currentParent = currentParent.Parent
ensurePropertyComplete(offset + length)
},
OnLiteralValue: func(value interface{}, offset, length int) {
OnLiteralValue: func(value any, offset, length int) {
onValue(&Node{Type: literalNodeType(value), Offset: offset, Length: length, Parent: currentParent, Value: value})
ensurePropertyComplete(offset + length)
},
Expand All @@ -180,7 +180,7 @@ func ParseTree(text string, options ParseOptions) (root *Node, errors []ParseErr
return root, errors
}

func literalNodeType(value interface{}) NodeType {
func literalNodeType(value any) NodeType {
switch value.(type) {
case bool:
return Boolean
Expand Down Expand Up @@ -254,17 +254,17 @@ func FindNodeAtLocation(root *Node, path Path) *Node {
// NodeValue returns the JSON parse tree node's value.
//
// Source: https://github.com/Microsoft/vscode/blob/c0bc1ace7ca3ce2d6b1aeb2bde9d1bb0f4b4bae6/src/vs/base/common/json.ts#L782
func NodeValue(node Node) interface{} {
func NodeValue(node Node) any {
switch node.Type {
case Array:
array := make([]interface{}, len(node.Children))
array := make([]any, len(node.Children))
for i, child := range node.Children {
array[i] = NodeValue(*child)
}
return array

case Object:
object := make(map[string]interface{}, len(node.Children))
object := make(map[string]any, len(node.Children))
for _, prop := range node.Children {
object[prop.Children[0].Value.(string)] = NodeValue(*prop.Children[1])
}
Expand Down
4 changes: 2 additions & 2 deletions visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Visitor struct {

// Invoked when a literal value is encountered. The offset and length represent
// the location of the literal value.
OnLiteralValue func(value interface{}, offset, length int)
OnLiteralValue func(value any, offset, length int)

// Invoked when a comma or colon separator is encountered. The offset and length
// represent the location of the separator.
Expand Down Expand Up @@ -105,7 +105,7 @@ func (w *walker) onArrayEnd() {
}
}

func (w *walker) onLiteralValue(value interface{}) {
func (w *walker) onLiteralValue(value any) {
if w.visitor.OnLiteralValue != nil {
w.visitor.OnLiteralValue(value, w.scanner.TokenOffset(), w.scanner.TokenLength())
}
Expand Down