yaml,xml,json数据格式封装解析测试

This commit is contained in:
John
2018-01-19 16:19:48 +08:00
parent 5dab00fddb
commit df9fb984e3
7 changed files with 111 additions and 64 deletions

View File

@ -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 {

View File

@ -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格式内容

27
g/encoding/gyaml/gyaml.go Normal file
View File

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

View File

@ -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 (

1
geg/frame/config.xml Normal file
View File

@ -0,0 +1 @@
<?xml version="1.0" encoding="utf-8"?>

29
geg/frame/config.yml Normal file
View File

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

View File

@ -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 := `
<?xml version="1.0" encoding="UTF-8"?>
<rss kk-name="1">
<channel>
<title>Comments for 碎言碎语</title>
<atom:link href="http://johng.cn/comments/feed/" rel="self" type="application/rss+xml" />
<link>http://johng.cn</link>
<description></description>
<lastBuildDate>Fri, 05 Jan 2018 02:56:11 +0000</lastBuildDate>
<sy:updatePeriod>hourly</sy:updatePeriod>
<sy:updateFrequency>1</sy:updateFrequency>
<generator>https://wordpress.org/?v=4.7.3</generator>
<item>
<title>Comment on Go性能优化string与[ ]byte转换 by John</title>
<link>http://johng.cn/go-optimize-string-bytes/#comment-114</link>
<dc:creator><![CDATA[John]]></dc:creator>
<pubDate>Fri, 05 Jan 2018 02:56:11 +0000</pubDate>
<guid isPermaLink="false">http://johng.cn/?p=3435#comment-114</guid>
<description><![CDATA[这篇文章我转自雨痕由于string和[]byte之间的转换比较常用所以这篇文章的性能对比才比较惊人。其他基本类型与[]byte这件的转换性能差别不大struct与[]byte的转换性能差别主要在数据结构设计上比如socket通信的话基本都是自己通过二进制转换来组织需要的数据结构。总得来说在go开发的时候尽量多用二进制参数([]byte)是好的。]]></description>
<content:encoded><![CDATA[<p>这篇文章我转自雨痕由于string和[]byte之间的转换比较常用所以这篇文章的性能对比才比较惊人。其他基本类型与[]byte这件的转换性能差别不大struct与[]byte的转换性能差别主要在数据结构设计上比如socket通信的话基本都是自己通过二进制转换来组织需要的数据结构。总得来说在go开发的时候尽量多用二进制参数([]byte)是好的。</p>
]]></content:encoded>
</item>
<item>
<title>Comment on 层次不同的人,是很难沟通的 by buhuipao</title>
<link>http://johng.cn/hard-for-communitication-between-different-levels/#comment-105</link>
<dc:creator><![CDATA[buhuipao]]></dc:creator>
<pubDate>Tue, 19 Sep 2017 09:10:08 +0000</pubDate>
<guid isPermaLink="false">http://johng.cn/?p=2854#comment-105</guid>
<description><![CDATA[博主说得很对,认知很重要。]]></description>
<content:encoded><![CDATA[<p>博主说得很对,认知很重要。</p>
]]></content:encoded>
</item>
</channel>
</rss>`
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))
}