Merge pull request #825 from chenall/gjson-support-utf8-with-bom

fix configfile with UTF8-BOM issue
This commit is contained in:
John Guo
2020-07-31 00:08:05 +08:00
committed by GitHub
3 changed files with 25 additions and 3 deletions

View File

@ -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...)
}

View File

@ -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")
})
}

View File

@ -0,0 +1,4 @@

[test]
testInt=1
testStr="test"