2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-05-08 22:04:36 +08:00
|
|
|
//
|
|
|
|
// 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 (
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/encoding/gini"
|
|
|
|
"github.com/gogf/gf/v2/encoding/gtoml"
|
|
|
|
"github.com/gogf/gf/v2/encoding/gxml"
|
|
|
|
"github.com/gogf/gf/v2/encoding/gyaml"
|
|
|
|
"github.com/gogf/gf/v2/internal/json"
|
2019-05-08 22:04:36 +08:00
|
|
|
)
|
|
|
|
|
2019-12-12 11:40:23 +08:00
|
|
|
// ========================================================================
|
|
|
|
// JSON
|
|
|
|
// ========================================================================
|
|
|
|
|
|
|
|
func (j *Json) ToJson() ([]byte, error) {
|
|
|
|
j.mu.RLock()
|
|
|
|
defer j.mu.RUnlock()
|
|
|
|
return Encode(*(j.p))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *Json) ToJsonString() (string, error) {
|
|
|
|
b, e := j.ToJson()
|
|
|
|
return string(b), e
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *Json) ToJsonIndent() ([]byte, error) {
|
|
|
|
j.mu.RLock()
|
|
|
|
defer j.mu.RUnlock()
|
|
|
|
return json.MarshalIndent(*(j.p), "", "\t")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *Json) ToJsonIndentString() (string, error) {
|
|
|
|
b, e := j.ToJsonIndent()
|
|
|
|
return string(b), e
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *Json) MustToJson() []byte {
|
|
|
|
result, err := j.ToJson()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *Json) MustToJsonString() string {
|
2021-09-14 19:30:20 +08:00
|
|
|
return string(j.MustToJson())
|
2019-12-12 11:40:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (j *Json) MustToJsonIndent() []byte {
|
|
|
|
result, err := j.ToJsonIndent()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *Json) MustToJsonIndentString() string {
|
2021-09-14 19:30:20 +08:00
|
|
|
return string(j.MustToJsonIndent())
|
2019-12-12 11:40:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ========================================================================
|
|
|
|
// XML
|
|
|
|
// ========================================================================
|
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
func (j *Json) ToXml(rootTag ...string) ([]byte, error) {
|
2021-09-27 22:47:39 +08:00
|
|
|
return gxml.Encode(j.Var().Map(), 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) {
|
2021-09-27 22:47:39 +08:00
|
|
|
return gxml.EncodeWithIndent(j.Var().Map(), 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
|
|
|
}
|
|
|
|
|
2019-12-12 11:40:23 +08:00
|
|
|
func (j *Json) MustToXml(rootTag ...string) []byte {
|
|
|
|
result, err := j.ToXml(rootTag...)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return result
|
2019-05-08 22:04:36 +08:00
|
|
|
}
|
|
|
|
|
2019-12-12 11:40:23 +08:00
|
|
|
func (j *Json) MustToXmlString(rootTag ...string) string {
|
2021-09-14 19:30:20 +08:00
|
|
|
return string(j.MustToXml(rootTag...))
|
2019-05-08 22:04:36 +08:00
|
|
|
}
|
|
|
|
|
2019-12-12 11:40:23 +08:00
|
|
|
func (j *Json) MustToXmlIndent(rootTag ...string) []byte {
|
|
|
|
result, err := j.ToXmlIndent(rootTag...)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return result
|
2019-05-08 22:04:36 +08:00
|
|
|
}
|
|
|
|
|
2019-12-12 11:40:23 +08:00
|
|
|
func (j *Json) MustToXmlIndentString(rootTag ...string) string {
|
2021-09-14 19:30:20 +08:00
|
|
|
return string(j.MustToXmlIndent(rootTag...))
|
2019-05-08 22:04:36 +08:00
|
|
|
}
|
|
|
|
|
2019-12-12 11:40:23 +08:00
|
|
|
// ========================================================================
|
|
|
|
// YAML
|
|
|
|
// ========================================================================
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-01-17 17:10:44 +08:00
|
|
|
func (j *Json) ToYamlIndent(indent string) ([]byte, error) {
|
|
|
|
j.mu.RLock()
|
|
|
|
defer j.mu.RUnlock()
|
|
|
|
return gyaml.EncodeIndent(*(j.p), indent)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-12-12 11:40:23 +08:00
|
|
|
func (j *Json) MustToYaml() []byte {
|
|
|
|
result, err := j.ToYaml()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *Json) MustToYamlString() string {
|
2021-09-14 19:30:20 +08:00
|
|
|
return string(j.MustToYaml())
|
2019-12-12 11:40:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ========================================================================
|
|
|
|
// TOML
|
|
|
|
// ========================================================================
|
|
|
|
|
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
|
|
|
|
2019-12-12 11:40:23 +08:00
|
|
|
func (j *Json) MustToToml() []byte {
|
|
|
|
result, err := j.ToToml()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *Json) MustToTomlString() string {
|
2021-09-14 19:30:20 +08:00
|
|
|
return string(j.MustToToml())
|
2019-12-12 11:40:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ========================================================================
|
|
|
|
// INI
|
|
|
|
// ========================================================================
|
|
|
|
|
2021-08-29 12:23:22 +08:00
|
|
|
// ToIni json to ini
|
2019-08-12 16:53:07 +08:00
|
|
|
func (j *Json) ToIni() ([]byte, error) {
|
2022-01-17 15:55:45 +08:00
|
|
|
return gini.Encode(j.Map())
|
2019-08-12 16:53:07 +08:00
|
|
|
}
|
|
|
|
|
2021-08-29 12:23:22 +08:00
|
|
|
// ToIniString ini to string
|
2019-08-12 16:53:07 +08:00
|
|
|
func (j *Json) ToIniString() (string, error) {
|
2021-08-29 12:23:22 +08:00
|
|
|
b, e := j.ToIni()
|
2019-08-12 16:53:07 +08:00
|
|
|
return string(b), e
|
|
|
|
}
|
2019-12-12 11:40:23 +08:00
|
|
|
|
|
|
|
func (j *Json) MustToIni() []byte {
|
|
|
|
result, err := j.ToIni()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-08-29 12:23:22 +08:00
|
|
|
// MustToIniString .
|
2019-12-12 11:40:23 +08:00
|
|
|
func (j *Json) MustToIniString() string {
|
2021-09-14 19:30:20 +08:00
|
|
|
return string(j.MustToIni())
|
2019-12-12 11:40:23 +08:00
|
|
|
}
|