MCPcopy Index your code
hub / github.com/foxcpp/maddy / marshalOrderedJSON

Function marshalOrderedJSON

framework/log/orderedjson.go:39–85  ·  view source on GitHub ↗
(output *strings.Builder, m map[string]interface{})

Source from the content-addressed store, hash-verified

37}
38
39func marshalOrderedJSON(output *strings.Builder, m map[string]interface{}) error {
40 order := make([]string, 0, len(m))
41 for k := range m {
42 order = append(order, k)
43 }
44 sort.Strings(order)
45
46 output.WriteRune('{')
47 for i, key := range order {
48 if i != 0 {
49 output.WriteRune(',')
50 }
51
52 jsonKey, err := json.Marshal(key)
53 if err != nil {
54 return err
55 }
56
57 output.Write(jsonKey)
58 output.WriteString(":")
59
60 val := m[key]
61 switch casted := val.(type) {
62 case time.Time:
63 val = casted.Format("2006-01-02T15:04:05.000")
64 case time.Duration:
65 val = casted.String()
66 case Formatter:
67 val = casted.FormatLog()
68 case fmt.Stringer:
69 val = casted.String()
70 case module:
71 val = casted.Name() + "/" + casted.InstanceName()
72 case error:
73 val = casted.Error()
74 }
75
76 jsonValue, err := json.Marshal(val)
77 if err != nil {
78 return err
79 }
80 output.Write(jsonValue)
81 }
82 output.WriteRune('}')
83
84 return nil
85}

Callers 1

formatMsgMethod · 0.85

Calls 6

WriteMethod · 0.65
FormatLogMethod · 0.65
NameMethod · 0.65
InstanceNameMethod · 0.65
StringMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected