1 files changed,
+44,
-0
+44,
-0
1@@ -194,6 +194,50 @@ func (o *Output) Binary() ([]byte, bool) {
2 return result, true
3 }
4
5+// JSONBytes returns the raw JSON bytes for TagJSON, TagJSONArr, or TagJSONMap values.
6+// For TagJSON, returns the raw JSON string directly.
7+// For TagJSONArr, converts the array to JSON bytes.
8+// For TagJSONMap, converts the map to JSON bytes.
9+// This is useful for unmarshaling directly into structs without going through interface{}.
10+// Returns nil and false if the output is not a JSON type.
11+func (o *Output) JSONBytes() ([]byte, bool) {
12+ switch o.Tag() {
13+ case TagJSON:
14+ ptr := C.youtput_read_string(o.ptr)
15+ if ptr == nil {
16+ return nil, false
17+ }
18+ // The string is owned by the YOutput and will be freed when
19+ // youtput_destroy is called. We must NOT free it here.
20+ // Return as byte slice for direct JSON unmarshaling.
21+ return []byte(C.GoString(ptr)), true
22+ case TagJSONArr:
23+ // Convert the JSON array back to bytes
24+ arr, err := o.convertToJSONArray()
25+ if err != nil {
26+ return nil, false
27+ }
28+ bytes, err := json.Marshal(arr)
29+ if err != nil {
30+ return nil, false
31+ }
32+ return bytes, true
33+ case TagJSONMap:
34+ // Convert the JSON map back to bytes
35+ m, err := o.convertToJSONMap()
36+ if err != nil {
37+ return nil, false
38+ }
39+ bytes, err := json.Marshal(m)
40+ if err != nil {
41+ return nil, false
42+ }
43+ return bytes, true
44+ default:
45+ return nil, false
46+ }
47+}
48+
49 // ToInterface converts the output to a Go interface{} value based on its type tag.
50 // Returns nil and an error if the type cannot be converted.
51 func (o *Output) ToInterface() (interface{}, error) {