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

Function jsonEncodeValue

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

Source from the content-addressed store, hash-verified

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

Callers 1

CompileOptionsMethod · 0.85

Calls 1

ConvertToNativeMethod · 0.65

Tested by

no test coverage detected