code.gitea.io/gitea@v1.22.3/modules/templates/util_json.go (about)

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