-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: encoding/json: return the postion for validation failures #67423
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
Comments
Change https://go.dev/cl/585995 mentions this issue: |
The v2 json draft proposal includes a |
I've tried to make my code work with the v2 draft, but this doesn't seem to work: package main
import (
"fmt"
"strings"
"github.com/go-json-experiment/json/jsontext"
)
func main() {
inputs := []string{
`{}`,
`{"foo": "bar"}`,
`{"foo: "bar"}`,
`{"foo": "bar", "bar": foo}`,
`{"foo": "bar", "bar", foo}`,
}
for _, input := range inputs {
val := jsontext.Value([]byte(input))
v := val.IsValid()
pos := 0
fmt.Printf("'%s': valid: %#v, pos: %d\n", input, v, pos)
if !v {
fmt.Printf(strings.Repeat(" ", int(pos)) + "^\n")
}
}
}
As you can see I've noticed The v2 json draft looks interesting, but I'm not fully convinced this should make this one redundant. Let's first see if my example can be modified to work with v2, then this can be used as feedback on v2. After that we can see if it makes sense to apply this to v1 or not. |
Try this instead: https://go.dev/play/p/ze0FqocVe-8 |
Thanks. That's great. This covers my usecase to get the offsets and even adds a useful error message. I think a |
Proposal Details
We use
json.Valid()
in https://github.com/pingcap/tidb to implement theJSON_VALID()
SQL function and would like to return the position on which the validation failed to the user.https://github.com/pingcap/tidb/blob/0875abde25467897e4e81ae6c18c5e8792ca28a9/pkg/expression/builtin_json.go#L1019
Example usage:
Output:
This would make it easier to see why validation failed, especially for larger JSON documents.
The text was updated successfully, but these errors were encountered: