gf/encoding/gjson/gjson_api_encoding.go

200 lines
4.4 KiB
Go
Raw Normal View History

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
)
// ========================================================================
// 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 {
return string(j.MustToJson())
}
func (j *Json) MustToJsonIndent() []byte {
result, err := j.ToJsonIndent()
if err != nil {
panic(err)
}
return result
}
func (j *Json) MustToJsonIndentString() string {
return string(j.MustToJsonIndent())
}
// ========================================================================
// XML
// ========================================================================
2019-06-19 09:06:52 +08:00
func (j *Json) ToXml(rootTag ...string) ([]byte, error) {
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) {
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
}
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
}
func (j *Json) MustToXmlString(rootTag ...string) string {
return string(j.MustToXml(rootTag...))
2019-05-08 22:04:36 +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
}
func (j *Json) MustToXmlIndentString(rootTag ...string) string {
return string(j.MustToXmlIndent(rootTag...))
2019-05-08 22:04:36 +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
}
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
}
func (j *Json) MustToYaml() []byte {
result, err := j.ToYaml()
if err != nil {
panic(err)
}
return result
}
func (j *Json) MustToYamlString() string {
return string(j.MustToYaml())
}
// ========================================================================
// 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
func (j *Json) MustToToml() []byte {
result, err := j.ToToml()
if err != nil {
panic(err)
}
return result
}
func (j *Json) MustToTomlString() string {
return string(j.MustToToml())
}
// ========================================================================
// 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) {
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
}
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 .
func (j *Json) MustToIniString() string {
return string(j.MustToIni())
}