diff --git a/g/encoding/gjson/gjson.go b/g/encoding/gjson/gjson.go
index 96df83bf9..3f028fb9f 100644
--- a/g/encoding/gjson/gjson.go
+++ b/g/encoding/gjson/gjson.go
@@ -12,10 +12,10 @@ import (
"strconv"
"io/ioutil"
"encoding/json"
- "gitee.com/johng/gf/g/util/gconv"
"gitee.com/johng/gf/g/os/gfile"
- "errors"
+ "gitee.com/johng/gf/g/util/gconv"
"gitee.com/johng/gf/g/encoding/gxml"
+ "gitee.com/johng/gf/g/encoding/gyaml"
)
// json解析结果存放数组
@@ -57,20 +57,33 @@ func DecodeToJson (b []byte) (*Json, error) {
}
// 支持多种配置文件类型转换为json格式内容并解析为gjson.Json对象
-// 支持的配置文件格式:xml, json, yml
func Load (path string) (*Json, error) {
- var result interface{}
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
- switch gfile.Ext(path) {
+ return LoadContent(data, gfile.Ext(path))
+}
+
+// 支持的配置文件格式:xml, json, yml
+func LoadContent (data []byte, t string) (*Json, error) {
+ var err error
+ var result interface{}
+ switch t {
+ case "xml": fallthrough
case ".xml":
data, err = gxml.ToJson(data)
if err != nil {
return nil, err
}
- case ".yml":
+ case "yml": fallthrough
+ case "yaml": fallthrough
+ case ".yml": fallthrough
+ case ".yaml":
+ data, err = gyaml.ToJson(data)
+ if err != nil {
+ return nil, err
+ }
}
if err := json.Unmarshal(data, &result); err != nil {
return nil, err
@@ -201,10 +214,9 @@ func (p *Json) Get(pattern string) interface{} {
// 转换为map[string]interface{}类型,如果转换失败,返回nil
func (p *Json) ToMap() map[string]interface{} {
- pointer := p.value
- switch (*pointer).(type) {
+ switch (*(p.value)).(type) {
case map[string]interface{}:
- return (*pointer).(map[string]interface{})
+ return (*(p.value)).(map[string]interface{})
default:
return nil
}
@@ -212,15 +224,33 @@ func (p *Json) ToMap() map[string]interface{} {
// 转换为[]interface{}类型,如果转换失败,返回nil
func (p *Json) ToArray() []interface{} {
- pointer := p.value
- switch (*pointer).(type) {
+ switch (*(p.value)).(type) {
case []interface{}:
- return (*pointer).([]interface{})
+ return (*(p.value)).([]interface{})
default:
return nil
}
}
+func (p *Json) ToXml(rootTag...string) ([]byte, error) {
+ return gxml.Encode(p.ToMap(), rootTag...)
+}
+
+func (p *Json) ToXmlIndent(rootTag...string) ([]byte, error) {
+ return gxml.EncodeWithIndent(p.ToMap(), rootTag...)
+}
+
+func (p *Json) ToJson() ([]byte, error) {
+ return Encode(*(p.value))
+}
+
+func (p *Json) ToJsonIndent() ([]byte, error) {
+ return json.MarshalIndent(*(p.value), "", "\t")
+}
+
+func (p *Json) ToYaml() ([]byte, error) {
+ return gyaml.Encode(*(p.value))
+}
// 判断所给字符串是否为数字
func isNumeric(s string) bool {
diff --git a/g/encoding/gxml/gxml.go b/g/encoding/gxml/gxml.go
index b54b7916a..4a132546b 100644
--- a/g/encoding/gxml/gxml.go
+++ b/g/encoding/gxml/gxml.go
@@ -17,8 +17,12 @@ func Decode(xmlbyte []byte) (map[string]interface{}, error) {
}
// 将map变量解析为XML格式内容
-func Encode(v map[string]interface{}) ([]byte, error) {
- return mxj.Map(v).Xml()
+func Encode(v map[string]interface{}, rootTag...string) ([]byte, error) {
+ return mxj.Map(v).Xml(rootTag...)
+}
+
+func EncodeWithIndent(v map[string]interface{}, rootTag...string) ([]byte, error) {
+ return mxj.Map(v).XmlIndent("", "\t", rootTag...)
}
// XML格式内容直接转换为JSON格式内容
diff --git a/g/encoding/gyaml/gyaml.go b/g/encoding/gyaml/gyaml.go
new file mode 100644
index 000000000..356e25d26
--- /dev/null
+++ b/g/encoding/gyaml/gyaml.go
@@ -0,0 +1,27 @@
+// Copyright 2017 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
+//
+// This Source Code Form is subject to the terms of the MIT License.
+// If a copy of the MIT was not distributed with this file,
+// You can obtain one at https://gitee.com/johng/gf.
+
+// YAML
+package gyaml
+
+import "github.com/ghodss/yaml"
+
+func Encode(v interface{}) ([]byte, error) {
+ return yaml.Marshal(v)
+}
+
+func Decode(v []byte) error {
+ var result interface{}
+ return yaml.Unmarshal(v, &result)
+}
+
+func DecodeTo(v []byte, result interface{}) error {
+ return yaml.Unmarshal(v, &result)
+}
+
+func ToJson(v []byte) ([]byte, error) {
+ return yaml.YAMLToJSON(v)
+}
\ No newline at end of file
diff --git a/g/frame/gcfg/gcfg.go b/g/frame/gcfg/gcfg.go
index aaef0f8f3..75c2aaeb6 100644
--- a/g/frame/gcfg/gcfg.go
+++ b/g/frame/gcfg/gcfg.go
@@ -4,7 +4,8 @@
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://gitee.com/johng/gf.
-// 配置管理
+// 配置管理.
+// 配置文件格式支持:json, xml, yml
package gcfg
import (
diff --git a/geg/frame/config.xml b/geg/frame/config.xml
new file mode 100644
index 000000000..42c1bdf97
--- /dev/null
+++ b/geg/frame/config.xml
@@ -0,0 +1 @@
+
\ No newline at end of file
diff --git a/geg/frame/config.yml b/geg/frame/config.yml
new file mode 100644
index 000000000..433dcf4a9
--- /dev/null
+++ b/geg/frame/config.yml
@@ -0,0 +1,29 @@
+{
+ "viewpath" : "/home/www/templates/",
+ "database" : {
+ "default" : [
+ {
+ "host" : "127.0.0.1",
+ "port" : "3306",
+ "user" : "root",
+ "pass" : "123456",
+ "name" : "test",
+ "type" : "mysql",
+ "role" : "master",
+ "charset" : "utf8",
+ "priority" : "1"
+ },
+ {
+ "host" : "127.0.0.1",
+ "port" : "3306",
+ "user" : "root",
+ "pass" : "123456",
+ "name" : "test",
+ "type" : "mysql",
+ "role" : "master",
+ "charset" : "utf8",
+ "priority" : "1"
+ }
+ ]
+ }
+}
\ No newline at end of file
diff --git a/geg/other/test.go b/geg/other/test.go
index c614d0644..d04c615bd 100644
--- a/geg/other/test.go
+++ b/geg/other/test.go
@@ -2,56 +2,11 @@ package main
import (
"fmt"
- "gitee.com/johng/gf/g/encoding/gxml"
- "gitee.com/johng/gf/g/os/gfile"
+ "gitee.com/johng/gf/g/encoding/gjson"
)
func main() {
- //json := gfile.GetBinContents("/home/john/Workspace/Go/GOPATH/src/gitee.com/johng/gf/geg/frame/config.json")
- //y, err := yaml.JSONToYAML(json)
- //fmt.Println(err)
- //fmt.Println(string(y))
- //
- //j, err := yaml.YAMLToJSON(y)
- //fmt.Println(err)
- //fmt.Println(string(j))
-
- x := `
-
-