diff --git a/encoding/gjson/gjson_api_new_load.go b/encoding/gjson/gjson_api_new_load.go index 9caf40081..52a5374d7 100644 --- a/encoding/gjson/gjson_api_new_load.go +++ b/encoding/gjson/gjson_api_new_load.go @@ -10,9 +10,10 @@ import ( "bytes" "errors" "fmt" - "github.com/gogf/gf/internal/json" "reflect" + "github.com/gogf/gf/internal/json" + "github.com/gogf/gf/encoding/gini" "github.com/gogf/gf/encoding/gtoml" "github.com/gogf/gf/encoding/gxml" @@ -188,6 +189,12 @@ func LoadContent(data interface{}, safe ...bool) (*Json, error) { if len(content) == 0 { return New(nil, safe...), nil } + + //ignore UTF8-BOM + if content[0] == 0xEF && content[1] == 0xBB && content[2] == 0xBF { + content = content[3:] + } + return doLoadContent(checkDataType(content), content, safe...) } diff --git a/os/gcfg/gcfg_z_unit_test.go b/os/gcfg/gcfg_z_unit_test.go index 2c81ca6a0..3cec43324 100644 --- a/os/gcfg/gcfg_z_unit_test.go +++ b/os/gcfg/gcfg_z_unit_test.go @@ -9,11 +9,12 @@ package gcfg_test import ( - "github.com/gogf/gf/debug/gdebug" - "github.com/gogf/gf/os/gtime" "os" "testing" + "github.com/gogf/gf/debug/gdebug" + "github.com/gogf/gf/os/gtime" + "github.com/gogf/gf/encoding/gjson" "github.com/gogf/gf/frame/g" "github.com/gogf/gf/os/gcfg" @@ -475,3 +476,13 @@ func TestCfg_Config(t *testing.T) { t.Assert(gcfg.GetContent("name"), "") }) } + +func TestCfg_With_UTF8_BOM(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + cfg := g.Cfg("test-cfg-with-utf8-bom") + t.Assert(cfg.SetPath("testdata"), nil) + cfg.SetFileName("cfg-with-utf8-bom.toml") + t.Assert(cfg.GetInt("test.testInt"), 1) + t.Assert(cfg.GetString("test.testStr"), "test") + }) +} diff --git a/os/gcfg/testdata/cfg-with-utf8-bom.toml b/os/gcfg/testdata/cfg-with-utf8-bom.toml new file mode 100644 index 000000000..19f8005ff --- /dev/null +++ b/os/gcfg/testdata/cfg-with-utf8-bom.toml @@ -0,0 +1,4 @@ + +[test] +testInt=1 +testStr="test"