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