MCPcopy Create free account
hub / github.com/cel-expr/cel-go / jsonEncodeValue

Function jsonEncodeValue

ext/encoders.go:223–246  ·  view source on GitHub ↗
(val ref.Val)

Source from the content-addressed store, hash-verified

221}
222
223func jsonEncodeValue(val ref.Val) (string, error) {
224 native, err := val.ConvertToNative(types.JSONValueType)
225 if err != nil {
226 return "", err
227 }
228 jsonValue, ok := native.(*structpb.Value)
229 if !ok {
230 return "", fmt.Errorf("cannot convert %T to JSON value", native)
231 }
232 jsonBytes, err := protojson.Marshal(jsonValue)
233 if err != nil {
234 return "", err
235 }
236 var obj interface{}
237 if err := json.Unmarshal(jsonBytes, &obj); err != nil {
238 return "", fmt.Errorf("unmarshaling protojson: %w", err)
239 }
240 // Re-marshal with standard json.Marshal for deterministic compact output
241 jsonBytes, err = json.Marshal(obj)
242 if err != nil {
243 return "", fmt.Errorf("re-marshaling value: %w", err)
244 }
245 return string(jsonBytes), nil
246}

Callers 1

CompileOptionsMethod · 0.85

Calls 1

ConvertToNativeMethod · 0.65

Tested by

no test coverage detected