You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

util_json.go 622B

1234567891011121314151617181920212223242526272829303132333435
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package templates
  4. import (
  5. "bytes"
  6. "code.gitea.io/gitea/modules/json"
  7. )
  8. type JsonUtils struct{} //nolint:revive
  9. var jsonUtils = JsonUtils{}
  10. func NewJsonUtils() *JsonUtils { //nolint:revive
  11. return &jsonUtils
  12. }
  13. func (su *JsonUtils) EncodeToString(v any) string {
  14. out, err := json.Marshal(v)
  15. if err != nil {
  16. return ""
  17. }
  18. return string(out)
  19. }
  20. func (su *JsonUtils) PrettyIndent(s string) string {
  21. var out bytes.Buffer
  22. err := json.Indent(&out, []byte(s), "", " ")
  23. if err != nil {
  24. return ""
  25. }
  26. return out.String()
  27. }