gparser_api_encoding.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://gitee.com/johng/gp.
  6. package gparser
  7. // ========================================================================
  8. // JSON
  9. // ========================================================================
  10. func VarToJson(value interface{}) ([]byte, error) {
  11. return New(value).ToJson()
  12. }
  13. func VarToJsonString(value interface{}) (string, error) {
  14. return New(value).ToJsonString()
  15. }
  16. func VarToJsonIndent(value interface{}) ([]byte, error) {
  17. return New(value).ToJsonIndent()
  18. }
  19. func VarToJsonIndentString(value interface{}) (string, error) {
  20. return New(value).ToJsonIndentString()
  21. }
  22. func MustToJson(value interface{}) []byte {
  23. return New(value).MustToJson()
  24. }
  25. func MustToJsonString(value interface{}) string {
  26. return New(value).MustToJsonString()
  27. }
  28. func MustToJsonIndent(value interface{}) []byte {
  29. return New(value).MustToJsonIndent()
  30. }
  31. func MustToJsonIndentString(value interface{}) string {
  32. return New(value).MustToJsonIndentString()
  33. }
  34. // ========================================================================
  35. // XML
  36. // ========================================================================
  37. func VarToXml(value interface{}, rootTag ...string) ([]byte, error) {
  38. return NewWithTag(value, "xml").ToXml(rootTag...)
  39. }
  40. func VarToXmlString(value interface{}, rootTag ...string) (string, error) {
  41. return NewWithTag(value, "xml").ToXmlString(rootTag...)
  42. }
  43. func VarToXmlIndent(value interface{}, rootTag ...string) ([]byte, error) {
  44. return NewWithTag(value, "xml").ToXmlIndent(rootTag...)
  45. }
  46. func VarToXmlIndentString(value interface{}, rootTag ...string) (string, error) {
  47. return NewWithTag(value, "xml").ToXmlIndentString(rootTag...)
  48. }
  49. func MustToXml(value interface{}, rootTag ...string) []byte {
  50. return NewWithTag(value, "xml").MustToXml(rootTag...)
  51. }
  52. func MustToXmlString(value interface{}, rootTag ...string) string {
  53. return NewWithTag(value, "xml").MustToXmlString(rootTag...)
  54. }
  55. func MustToXmlIndent(value interface{}, rootTag ...string) []byte {
  56. return NewWithTag(value, "xml").MustToXmlIndent(rootTag...)
  57. }
  58. func MustToXmlIndentString(value interface{}, rootTag ...string) string {
  59. return NewWithTag(value, "xml").MustToXmlIndentString(rootTag...)
  60. }
  61. // ========================================================================
  62. // YAML
  63. // ========================================================================
  64. func VarToYaml(value interface{}) ([]byte, error) {
  65. return NewWithTag(value, "yaml").ToYaml()
  66. }
  67. func VarToYamlString(value interface{}) (string, error) {
  68. return NewWithTag(value, "yaml").ToYamlString()
  69. }
  70. func MustToYaml(value interface{}) []byte {
  71. return NewWithTag(value, "yaml").MustToYaml()
  72. }
  73. func MustToYamlString(value interface{}) string {
  74. return NewWithTag(value, "yaml").MustToYamlString()
  75. }
  76. // ========================================================================
  77. // TOML
  78. // ========================================================================
  79. func VarToToml(value interface{}) ([]byte, error) {
  80. return NewWithTag(value, "toml").ToToml()
  81. }
  82. func VarToTomlString(value interface{}) (string, error) {
  83. return NewWithTag(value, "toml").ToTomlString()
  84. }
  85. func MustToToml(value interface{}) []byte {
  86. return NewWithTag(value, "toml").MustToToml()
  87. }
  88. func MustToTomlString(value interface{}) string {
  89. return NewWithTag(value, "toml").MustToTomlString()
  90. }
  91. // ========================================================================
  92. // INI
  93. // ========================================================================
  94. func VarToIni(value interface{}) ([]byte, error) {
  95. return NewWithTag(value, "ini").ToIni()
  96. }
  97. func VarToIniString(value interface{}) (string, error) {
  98. return NewWithTag(value, "ini").ToIniString()
  99. }
  100. func MustToIni(value interface{}) []byte {
  101. return NewWithTag(value, "ini").MustToIni()
  102. }
  103. func MustToIniString(value interface{}) string {
  104. return NewWithTag(value, "ini").MustToIniString()
  105. }