From df9fb984e35373d78deb069134d4d91a9fa3441a Mon Sep 17 00:00:00 2001 From: John Date: Fri, 19 Jan 2018 16:19:48 +0800 Subject: [PATCH] =?UTF-8?q?yaml,xml,json=E6=95=B0=E6=8D=AE=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E5=B0=81=E8=A3=85=E8=A7=A3=E6=9E=90=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/encoding/gjson/gjson.go | 54 ++++++++++++++++++++++++++++++--------- g/encoding/gxml/gxml.go | 8 ++++-- g/encoding/gyaml/gyaml.go | 27 ++++++++++++++++++++ g/frame/gcfg/gcfg.go | 3 ++- geg/frame/config.xml | 1 + geg/frame/config.yml | 29 +++++++++++++++++++++ geg/other/test.go | 53 +++----------------------------------- 7 files changed, 111 insertions(+), 64 deletions(-) create mode 100644 g/encoding/gyaml/gyaml.go create mode 100644 geg/frame/config.xml create mode 100644 geg/frame/config.yml 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 := ` - - - - Comments for 碎言碎语 - - http://johng.cn - - Fri, 05 Jan 2018 02:56:11 +0000 - hourly - 1 - https://wordpress.org/?v=4.7.3 - - Comment on Go性能优化:string与[ ]byte转换 by John - http://johng.cn/go-optimize-string-bytes/#comment-114 - - Fri, 05 Jan 2018 02:56:11 +0000 - http://johng.cn/?p=3435#comment-114 - - 这篇文章我转自雨痕,由于string和[]byte之间的转换比较常用,所以这篇文章的性能对比才比较惊人。其他基本类型与[]byte这件的转换性能差别不大,struct与[]byte的转换性能差别主要在数据结构设计上,比如socket通信的话,基本都是自己通过二进制转换来组织需要的数据结构。总得来说,在go开发的时候尽量多用二进制参数([]byte)是好的。

-]]>
-
- - - Comment on 层次不同的人,是很难沟通的 by buhuipao - http://johng.cn/hard-for-communitication-between-different-levels/#comment-105 - - Tue, 19 Sep 2017 09:10:08 +0000 - http://johng.cn/?p=2854#comment-105 - - 博主说得很对,认知很重要。

-]]>
-
-
-
` - - j, _ := gxml.ToJson([]byte(x)) - fmt.Println(string(j)) + j, _ := gjson.Load("/home/john/Workspace/Go/GOPATH/src/gitee.com/johng/gf/geg/frame/config.json") + c, _ := j.ToXmlIndent("config") + fmt.Println(string(c)) } \ No newline at end of file