2
0
Fork 0
mirror of https://github.com/ii64/sonic.git synced 2026-06-23 18:06:44 +08:00

feat: reformat print error example and clarify v1.6.0 example (#332)

This commit is contained in:
erin liman 2022-12-09 01:00:49 -08:00 committed by GitHub
parent dda5da9f64
commit 01c0d36194
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -190,9 +190,9 @@ import "github.com/bytedance/sonic/decoder"
var data interface{} var data interface{}
err := sonic.UnmarshalString("[[[}]]", &data) err := sonic.UnmarshalString("[[[}]]", &data)
if err != nil { if err != nil {
/*one line by default*/ /* One line by default */
println(e.Error()) // "Syntax error at index 3: invalid char\n\n\t[[[}]]\n\t...^..\n" println(e.Error()) // "Syntax error at index 3: invalid char\n\n\t[[[}]]\n\t...^..\n"
/*pretty print*/ /* Pretty print */
if e, ok := err.(decoder.SyntaxError); ok { if e, ok := err.(decoder.SyntaxError); ok {
/*Syntax error at index 3: invalid char /*Syntax error at index 3: invalid char
@ -200,11 +200,14 @@ if err != nil {
...^.. ...^..
*/ */
print(e.Description()) print(e.Description())
}else if me, ok := err.(decoder.MismatchTypeError); ok { } else if me, ok := err.(*decoder.MismatchTypeError); ok {
print(e.Description() // decoder.MismatchTypeError is new to Sonic v1.6.0
print(me.Description())
} }
} }
``` ```
#### Mismatched Types [Sonic v1.6.0]
If there a **mismatch-typed** value for a given key, sonic will report `decoder.MismatchTypeError` (if there are many, report the last one), but still skip wrong the value and keep decoding next JSON. If there a **mismatch-typed** value for a given key, sonic will report `decoder.MismatchTypeError` (if there are many, report the last one), but still skip wrong the value and keep decoding next JSON.
```go ```go
import "github.com/bytedance/sonic" import "github.com/bytedance/sonic"