MCPcopy
hub / github.com/keploy/keploy / DocToJSON

Function DocToJSON

pkg/platform/yaml/codec.go:73–93  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

71// encoded document (e.g. json.NewEncoder(w).Encode(jsonDoc)) can do so without
72// re-allocating a []byte.
73func 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).

Callers 3

MarshalDocFunction · 0.85
MarshalDocIndentFunction · 0.85
EncodeDocToFunction · 0.85

Calls 1

MarshalMethod · 0.80

Tested by

no test coverage detected