-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringify.go
36 lines (31 loc) · 965 Bytes
/
stringify.go
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
36
package fetch
import (
"github.com/glossd/fetch/internal/json"
)
// Stringify tries to marshal v.
// If an error happens, Stringify returns an empty string.
// An empty string is not a valid JSON, indicating Stringify failed.
//func Stringify(v any) string {
// s, err := StringifySafe(v)
// if err != nil {
// return ""
// }
// return s
//}
//// StringifySafe tries to fix possible errors during marshalling and then calls Marshal.
//func StringifySafe(v any) (string, error) {
// // skip unsupported types e.g. channel fields
// // I can't rely on Go's encoding/json to escape unsupported fields.
// return Marshal(v)
//}
/*
Marshal calls the patched json.Marshal function.
There are only two patches
1. It lowercases the first letter of the public struct fields.
2. It omits empty fields by default.
They are only applied if the `json` tag is not specified.
*/
func Marshal(v any) (string, error) {
rs, err := json.Marshal(v)
return string(rs), err
}