2019-05-08 22:04:36 +08:00
|
|
|
// Copyright 2017 gf Author(https://github.com/gogf/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://github.com/gogf/gf.
|
|
|
|
|
|
|
|
|
|
package gjson
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
2019-08-12 16:53:07 +08:00
|
|
|
"github.com/gogf/gf/encoding/gini"
|
2019-07-29 21:01:19 +08:00
|
|
|
"github.com/gogf/gf/encoding/gtoml"
|
|
|
|
|
"github.com/gogf/gf/encoding/gxml"
|
|
|
|
|
"github.com/gogf/gf/encoding/gyaml"
|
2019-05-08 22:04:36 +08:00
|
|
|
)
|
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
func (j *Json) ToXml(rootTag ...string) ([]byte, error) {
|
|
|
|
|
return gxml.Encode(j.ToMap(), rootTag...)
|
2019-05-08 22:04:36 +08:00
|
|
|
}
|
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
func (j *Json) ToXmlString(rootTag ...string) (string, error) {
|
|
|
|
|
b, e := j.ToXml(rootTag...)
|
|
|
|
|
return string(b), e
|
2019-05-08 22:04:36 +08:00
|
|
|
}
|
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
func (j *Json) ToXmlIndent(rootTag ...string) ([]byte, error) {
|
|
|
|
|
return gxml.EncodeWithIndent(j.ToMap(), rootTag...)
|
2019-05-08 22:04:36 +08:00
|
|
|
}
|
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
func (j *Json) ToXmlIndentString(rootTag ...string) (string, error) {
|
|
|
|
|
b, e := j.ToXmlIndent(rootTag...)
|
|
|
|
|
return string(b), e
|
2019-05-08 22:04:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (j *Json) ToJson() ([]byte, error) {
|
2019-06-19 09:06:52 +08:00
|
|
|
j.mu.RLock()
|
|
|
|
|
defer j.mu.RUnlock()
|
|
|
|
|
return Encode(*(j.p))
|
2019-05-08 22:04:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (j *Json) ToJsonString() (string, error) {
|
2019-06-19 09:06:52 +08:00
|
|
|
b, e := j.ToJson()
|
|
|
|
|
return string(b), e
|
2019-05-08 22:04:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (j *Json) ToJsonIndent() ([]byte, error) {
|
2019-06-19 09:06:52 +08:00
|
|
|
j.mu.RLock()
|
|
|
|
|
defer j.mu.RUnlock()
|
|
|
|
|
return json.MarshalIndent(*(j.p), "", "\t")
|
2019-05-08 22:04:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (j *Json) ToJsonIndentString() (string, error) {
|
2019-06-19 09:06:52 +08:00
|
|
|
b, e := j.ToJsonIndent()
|
|
|
|
|
return string(b), e
|
2019-05-08 22:04:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (j *Json) ToYaml() ([]byte, error) {
|
2019-06-19 09:06:52 +08:00
|
|
|
j.mu.RLock()
|
|
|
|
|
defer j.mu.RUnlock()
|
|
|
|
|
return gyaml.Encode(*(j.p))
|
2019-05-08 22:04:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (j *Json) ToYamlString() (string, error) {
|
2019-06-19 09:06:52 +08:00
|
|
|
b, e := j.ToYaml()
|
|
|
|
|
return string(b), e
|
2019-05-08 22:04:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (j *Json) ToToml() ([]byte, error) {
|
2019-06-19 09:06:52 +08:00
|
|
|
j.mu.RLock()
|
|
|
|
|
defer j.mu.RUnlock()
|
|
|
|
|
return gtoml.Encode(*(j.p))
|
2019-05-08 22:04:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (j *Json) ToTomlString() (string, error) {
|
2019-06-19 09:06:52 +08:00
|
|
|
b, e := j.ToToml()
|
|
|
|
|
return string(b), e
|
2019-05-08 22:04:36 +08:00
|
|
|
}
|
2019-08-12 16:53:07 +08:00
|
|
|
|
|
|
|
|
func (j *Json) ToIni() ([]byte, error) {
|
|
|
|
|
j.mu.RLock()
|
|
|
|
|
defer j.mu.RUnlock()
|
|
|
|
|
return gini.Encode((*(j.p)).(map[string]interface{}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (j *Json) ToIniString() (string, error) {
|
|
|
|
|
b, e := j.ToToml()
|
|
|
|
|
return string(b), e
|
|
|
|
|
}
|