json.gno
2.72 Kb · 134 lines
1package hearthcore
2
3import (
4 "strconv"
5 "strings"
6)
7
8// JSONBuilder constructs deterministic, RFC-8259-compliant JSON objects without
9// reflection or external dependencies so realms can expose structured queries
10// to AI agents and MCP servers via vm/qeval.
11type JSONBuilder struct {
12 b strings.Builder
13 count int
14}
15
16// NewJSONObject starts a new JSON object builder.
17func NewJSONObject() *JSONBuilder {
18 jb := &JSONBuilder{}
19 jb.b.WriteByte('{')
20 return jb
21}
22
23func (jb *JSONBuilder) writeKey(key string) {
24 if jb.count > 0 {
25 jb.b.WriteByte(',')
26 }
27 jb.b.WriteString(QuoteJSON(key))
28 jb.b.WriteByte(':')
29 jb.count++
30}
31
32// Str appends a string field.
33func (jb *JSONBuilder) Str(key, val string) *JSONBuilder {
34 jb.writeKey(key)
35 jb.b.WriteString(QuoteJSON(val))
36 return jb
37}
38
39// Int64 appends a signed 64-bit integer field.
40func (jb *JSONBuilder) Int64(key string, val int64) *JSONBuilder {
41 jb.writeKey(key)
42 jb.b.WriteString(strconv.FormatInt(val, 10))
43 return jb
44}
45
46// Uint64 appends an unsigned 64-bit integer field.
47func (jb *JSONBuilder) Uint64(key string, val uint64) *JSONBuilder {
48 jb.writeKey(key)
49 jb.b.WriteString(strconv.FormatUint(val, 10))
50 return jb
51}
52
53// Bool appends a boolean field.
54func (jb *JSONBuilder) Bool(key string, val bool) *JSONBuilder {
55 jb.writeKey(key)
56 if val {
57 jb.b.WriteString("true")
58 } else {
59 jb.b.WriteString("false")
60 }
61 return jb
62}
63
64// Raw appends a pre-encoded JSON value (array or nested object).
65func (jb *JSONBuilder) Raw(key, rawJSON string) *JSONBuilder {
66 jb.writeKey(key)
67 jb.b.WriteString(rawJSON)
68 return jb
69}
70
71// String closes and returns the encoded JSON object.
72func (jb *JSONBuilder) String() string {
73 return jb.b.String() + "}"
74}
75
76// JoinJSONArray joins pre-encoded JSON elements into a JSON array.
77func JoinJSONArray(items []string) string {
78 if len(items) == 0 {
79 return "[]"
80 }
81 var b strings.Builder
82 b.WriteByte('[')
83 for i, item := range items {
84 if i > 0 {
85 b.WriteByte(',')
86 }
87 b.WriteString(item)
88 }
89 b.WriteByte(']')
90 return b.String()
91}
92
93// QuoteJSON encodes s as a valid RFC-8259 JSON string literal.
94func QuoteJSON(s string) string {
95 var b strings.Builder
96 b.Grow(len(s) + 2)
97 b.WriteByte('"')
98 for i := 0; i < len(s); i++ {
99 c := s[i]
100 switch c {
101 case '"':
102 b.WriteString(`\"`)
103 case '\\':
104 b.WriteString(`\\`)
105 case '\b':
106 b.WriteString(`\b`)
107 case '\f':
108 b.WriteString(`\f`)
109 case '\n':
110 b.WriteString(`\n`)
111 case '\r':
112 b.WriteString(`\r`)
113 case '\t':
114 b.WriteString(`\t`)
115 default:
116 if c < 0x20 {
117 b.WriteString(`\u00`)
118 b.WriteByte(hexDigit(c >> 4))
119 b.WriteByte(hexDigit(c & 0x0f))
120 } else {
121 b.WriteByte(c)
122 }
123 }
124 }
125 b.WriteByte('"')
126 return b.String()
127}
128
129func hexDigit(n byte) byte {
130 if n < 10 {
131 return '0' + n
132 }
133 return 'a' + (n - 10)
134}