diff --git a/encoding/gjson/gjson_api_new_load.go b/encoding/gjson/gjson_api_new_load.go index a21d35c9c..23d761fef 100644 --- a/encoding/gjson/gjson_api_new_load.go +++ b/encoding/gjson/gjson_api_new_load.go @@ -175,6 +175,17 @@ func LoadToml(data interface{}, safe ...bool) (*Json, error) { return doLoadContentWithOptions(gconv.Bytes(data), option) } +// LoadProperties creates a Json object from given TOML format content. +func LoadProperties(data interface{}, safe ...bool) (*Json, error) { + option := Options{ + Type: ContentTypeProperties, + } + if len(safe) > 0 && safe[0] { + option.Safe = true + } + return doLoadContentWithOptions(gconv.Bytes(data), option) +} + // LoadContent creates a Json object from given content, it checks the data type of `content` // automatically, supporting data content type as follows: // JSON, XML, INI, YAML and TOML. @@ -223,7 +234,8 @@ func IsValidDataType(dataType string) bool { ContentTypeYaml, ContentTypeYml, ContentTypeToml, - ContentTypeIni: + ContentTypeIni, + ContentTypeProperties: return true } return false diff --git a/encoding/gjson/gjson_z_example_load_test.go b/encoding/gjson/gjson_z_example_load_test.go index 41fe9189d..102029f6f 100644 --- a/encoding/gjson/gjson_z_example_load_test.go +++ b/encoding/gjson/gjson_z_example_load_test.go @@ -182,6 +182,7 @@ func ExampleIsValidDataType() { fmt.Println(gjson.IsValidDataType("txt")) fmt.Println(gjson.IsValidDataType("")) fmt.Println(gjson.IsValidDataType(".json")) + fmt.Println(gjson.IsValidDataType(".properties")) // Output: // true @@ -192,6 +193,7 @@ func ExampleIsValidDataType() { // false // false // true + // true } func ExampleLoad_Xml() { @@ -200,3 +202,16 @@ func ExampleLoad_Xml() { fmt.Println(j.Get("doc.name")) fmt.Println(j.Get("doc.score")) } + +func ExampleLoad_Properties() { + jsonFilePath := gtest.DataPath("properties", "data1.properties") + j, _ := gjson.Load(jsonFilePath) + fmt.Println(j.Get("pr.name")) + fmt.Println(j.Get("pr.score")) + fmt.Println(j.Get("pr.sex")) + + //Output: + // john + // 100 + // 0 +} diff --git a/encoding/gjson/gjson_z_example_test.go b/encoding/gjson/gjson_z_example_test.go index fa765be83..ce7c0c4c9 100644 --- a/encoding/gjson/gjson_z_example_test.go +++ b/encoding/gjson/gjson_z_example_test.go @@ -641,6 +641,81 @@ func ExampleJson_MustToIniString() { //Name=John } +// ======================================================================== +// Properties +// ======================================================================== +func ExampleJson_ToProperties() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + j := gjson.New(info) + pr, _ := j.ToProperties() + fmt.Println(string(pr)) + + // May Output: + // name = John + // age = 18 +} + +func ExampleJson_ToPropertiesString() { + type BaseInfo struct { + Name string + } + + info := BaseInfo{ + Name: "John", + } + + j := gjson.New(info) + pr, _ := j.ToPropertiesString() + fmt.Println(pr) + + // Output: + // name = John +} + +func ExampleJson_MustToProperties() { + type BaseInfo struct { + Name string + } + + info := BaseInfo{ + Name: "John", + } + + j := gjson.New(info) + pr := j.MustToProperties() + fmt.Println(string(pr)) + + // Output: + // name = John +} + +func ExampleJson_MustToPropertiesString() { + type BaseInfo struct { + Name string + } + + info := BaseInfo{ + Name: "John", + } + + j := gjson.New(info) + pr := j.MustToPropertiesString() + fmt.Println(pr) + + // Output: + // name = John +} + + func ExampleJson_MarshalJSON() { type BaseInfo struct { Name string @@ -1111,3 +1186,5 @@ func ExampleJson_Dump() { // "age": "18", //} } + + diff --git a/encoding/gjson/gjson_z_unit_feature_load_test.go b/encoding/gjson/gjson_z_unit_feature_load_test.go index 4517989a4..51f9a2d8b 100644 --- a/encoding/gjson/gjson_z_unit_feature_load_test.go +++ b/encoding/gjson/gjson_z_unit_feature_load_test.go @@ -361,3 +361,58 @@ gfcli: t.AssertNil(err) }) } + +func Test_Load_Properties(t *testing.T) { + var data = ` + +#注释 + + +addr.ip = 127.0.0.1 +addr.port=9001 +addr.enable=true +DBINFO.type=mysql +DBINFO.user=root +DBINFO.password=password + +` + + gtest.C(t, func(t *gtest.T) { + j, err := gjson.LoadContent(data) + if err != nil { + gtest.Fatal(err) + } + + t.Assert(j.Get("addr.ip").String(), "127.0.0.1") + t.Assert(j.Get("addr.port").String(), "9001") + t.Assert(j.Get("addr.enable").String(), "true") + t.Assert(j.Get("DBINFO.type").String(), "mysql") + t.Assert(j.Get("DBINFO.user").String(), "root") + t.Assert(j.Get("DBINFO.password").String(), "password") + + _, err = j.ToProperties() + if err != nil { + gtest.Fatal(err) + } + }) + + gtest.C(t, func(t *gtest.T) { + j, err := gjson.LoadProperties(data, true) + if err != nil { + gtest.Fatal(err) + } + + t.Assert(j.Get("addr.ip").String(), "127.0.0.1") + t.Assert(j.Get("addr.port").String(), "9001") + t.Assert(j.Get("addr.enable").String(), "true") + t.Assert(j.Get("DBINFO.type").String(), "mysql") + t.Assert(j.Get("DBINFO.user").String(), "root") + t.Assert(j.Get("DBINFO.password").String(), "password") + }) + + gtest.C(t, func(t *gtest.T) { + errData := []byte("i\\u1 : 123456789") + _, err := gjson.LoadContentType("properties", errData, true) + t.AssertNE(err, nil) + }) +} diff --git a/encoding/gjson/testdata/properties/data1.properties b/encoding/gjson/testdata/properties/data1.properties new file mode 100644 index 000000000..8f9dad5d9 --- /dev/null +++ b/encoding/gjson/testdata/properties/data1.properties @@ -0,0 +1,3 @@ +pr.name=john +pr.score=100 +pr.sex=0 \ No newline at end of file