user golang , write json unmarshal, error happened, Because of comma before "]".
import (
"encoding/json"
"github.com/c2h5oh/datasize"
xdsboot "github.com/envoyproxy/go-control-plane/envoy/config/bootstrap/v2"
"github.com/golang/protobuf/jsonpb"
)
err = json.Unmarshal(content, cfg)
if err != nil {
log.StartLogger.Fatalf("[config] [default load] json unmarshal config failed, error: %v", err)
}
return cfg
return error:
2021-07-14 06:53:39,637 [FATAL] [config] [default load] json unmarshal config failed, error: invalid character ']' looking for beginning of value
I use unit test case to run, find file input
func TestMosnEnvoyMode(t *testing.T) {
content, _ := ioutil.ReadFile("./test.json") // this file is input file
cfg := &MOSNConfig{}
if err := json.Unmarshal([]byte(content), cfg); err != nil {
t.Fatal(err)
}
if cfg.Mode() != Mix {
t.Fatalf("config mode is %d", cfg.Mode())
}
}
file content:
{
"stats_matcher": {
"inclusion_list": {
"patterns": [
{
"prefix": "cluster.xds-grpc"
},
{
"suffix": "ssl_context_update_by_sds"
}, // the error cause by here, ","
]
}
}
},
}
if I delete comma here
{
"suffix": "ssl_context_update_by_sds"
}, // the error cause by here, ","
It is OK!
Now, why and which json lib should I use? Because the input file can not change.