ProtoWireToJSON takes a MessageDescriptor, compiled files, and a wire-format []byte, and returns the JSON encoding ([]byte). The files parameter is crucial for resolving google.protobuf.Any types which require access to all message types in the compiled schema.
(md protoreflect.MessageDescriptor, files []protoreflect.FileDescriptor, wire []byte)
| 349 | // and returns the JSON encoding ([]byte). The files parameter is crucial for resolving |
| 350 | // google.protobuf.Any types which require access to all message types in the compiled schema. |
| 351 | func ProtoWireToJSON(md protoreflect.MessageDescriptor, files []protoreflect.FileDescriptor, wire []byte) ([]byte, error) { |
| 352 | // Create type resolver for Any type resolution - this fixes the error: |
| 353 | // "proto: google.protobuf.Any: unable to resolve \"type.googleapis.com/fuzz.Inner\": not found" |
| 354 | typeResolver := createTypeResolver(files) |
| 355 | |
| 356 | // Unmarshal into dynamic message |
| 357 | msg := dynamicpb.NewMessage(md) |
| 358 | if err := proto.Unmarshal(wire, msg); err != nil { |
| 359 | return nil, err |
| 360 | } |
| 361 | |
| 362 | // Marshal to JSON with custom type resolver |
| 363 | actRespJson, err := protojson.MarshalOptions{ |
| 364 | Indent: " ", |
| 365 | EmitUnpopulated: true, // include false/0/""/empty fields |
| 366 | UseProtoNames: true, // snake_case field names |
| 367 | Resolver: typeResolver, // Custom resolver for Any types |
| 368 | }.Marshal(msg) |
| 369 | if err != nil { |
| 370 | return nil, err |
| 371 | } |
| 372 | |
| 373 | return actRespJson, nil |
| 374 | } |
| 375 | |
| 376 | // ProtoTextToJSON converts a Protoscope text payload to JSON via: |
| 377 | // |
no test coverage detected