DocToJSON converts a NetworkTrafficDoc to its JSON-friendly representation with a json.RawMessage spec. Exported so callers that want to stream the encoded document (e.g. json.NewEncoder(w).Encode(jsonDoc)) can do so without re-allocating a []byte.
(doc *NetworkTrafficDoc)
| 71 | // encoded document (e.g. json.NewEncoder(w).Encode(jsonDoc)) can do so without |
| 72 | // re-allocating a []byte. |
| 73 | func DocToJSON(doc *NetworkTrafficDoc) (*NetworkTrafficDocJSON, error) { |
| 74 | var specData any |
| 75 | if err := doc.Spec.Decode(&specData); err != nil { |
| 76 | return nil, fmt.Errorf("failed to decode yaml.Node spec for JSON conversion: %w", err) |
| 77 | } |
| 78 | specBytes, err := json.Marshal(specData) |
| 79 | if err != nil { |
| 80 | return nil, fmt.Errorf("failed to marshal spec to JSON: %w", err) |
| 81 | } |
| 82 | return &NetworkTrafficDocJSON{ |
| 83 | Version: doc.Version, |
| 84 | Kind: doc.Kind, |
| 85 | Name: doc.Name, |
| 86 | Spec: specBytes, |
| 87 | Noise: doc.Noise, |
| 88 | LastUpdated: doc.LastUpdated, |
| 89 | Curl: doc.Curl, |
| 90 | ConnectionID: doc.ConnectionID, |
| 91 | ReqBodyNoise: doc.ReqBodyNoise, // legacy read-through (see DocNoise) |
| 92 | }, nil |
| 93 | } |
| 94 | |
| 95 | // MarshalDoc serializes a NetworkTrafficDoc to bytes in the specified format. |
| 96 | // For YAML, it marshals the doc directly (using yaml.Node for Spec). |
no test coverage detected