summaryrefslogtreecommitdiffstats
path: root/modules/templates/util_json.go
blob: 71a4e23d364f3c6d9e71aefcb9ae3e04fb041e24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package templates

import (
	"bytes"

	"code.gitea.io/gitea/modules/json"
)

type JsonUtils struct{} //nolint:revive

var jsonUtils = JsonUtils{}

func NewJsonUtils() *JsonUtils { //nolint:revive
	return &jsonUtils
}

func (su *JsonUtils) EncodeToString(v any) string {
	out, err := json.Marshal(v)
	if err != nil {
		return ""
	}
	return string(out)
}

func (su *JsonUtils) PrettyIndent(s string) string {
	var out bytes.Buffer
	err := json.Indent(&out, []byte(s), "", "  ")
	if err != nil {
		return ""
	}
	return out.String()
}