{"id":15823883,"url":"https://github.com/stefh/protobufjsonconverter","last_synced_at":"2025-12-24T09:41:22.286Z","repository":{"id":213682131,"uuid":"734410581","full_name":"StefH/ProtoBufJsonConverter","owner":"StefH","description":"Convert a protobuf message to a JSON string or object using the proto definition file.","archived":false,"fork":false,"pushed_at":"2024-03-06T17:55:22.000Z","size":792,"stargazers_count":4,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-05-22T21:30:39.607Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/StefH.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2023-12-21T15:58:29.000Z","updated_at":"2023-12-29T17:06:20.000Z","dependencies_parsed_at":"2024-03-28T13:20:26.162Z","dependency_job_id":"0d902a4f-02e5-424b-8c3d-a134bd7c0931","html_url":"https://github.com/StefH/ProtoBufJsonConverter","commit_stats":{"total_commits":32,"total_committers":2,"mean_commits":16.0,"dds":0.4375,"last_synced_commit":"ffd1e059d3c7a484147acc11b4d1309df980d789"},"previous_names":["stefh/protobufjsonconverter"],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StefH%2FProtoBufJsonConverter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StefH%2FProtoBufJsonConverter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StefH%2FProtoBufJsonConverter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StefH%2FProtoBufJsonConverter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/StefH","download_url":"https://codeload.github.com/StefH/ProtoBufJsonConverter/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243674645,"owners_count":20329134,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-05T08:24:00.297Z","updated_at":"2025-12-24T09:41:22.280Z","avatar_url":"https://github.com/StefH.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ![Icon](./resources/icon-32x32.png) ProtoBufJsonConverter\n\n## This project uses [protobuf-net](https://github.com/protobuf-net/protobuf-net) to:\n- Convert a protobuf message to a JSON string using the proto definition file.\n- Convert a protobuf message to an object using the proto definition file.\n- Convert a JSON string or an object to a protobuf message using the proto definition file.\n- Get information about the package names, message types and C# namespaces in the proto definition file.\n\n## NuGet\n[![NuGet Badge](https://img.shields.io/nuget/v/ProtoBufJsonConverter)](https://www.nuget.org/packages/ProtoBufJsonConverter) \n\n## Usage\n\n### Proto Definition\n``` proto\nsyntax = \"proto3\";\n\n// Package name\npackage greet;\n\n// The greeting service definition.\nservice Greeter {\n  rpc SayHello (HelloRequest) returns (HelloReply);\n}\n\n// The request message containing the user's name.\nmessage HelloRequest {\n  string name = 1;\n}\n\n// The response message containing the greetings\nmessage HelloReply {\n  string message = 1;\n}\n```\n\n### :one: Convert ProtoBuf `byte[]` to a JSON `string`\n\n``` mermaid\n---\ntitle: \"Convert ProtoBuf byte[] to a JSON string\"\n---\nflowchart LR\n\tDef[\"ProtoBuf Definition\\n(.proto)\"] --\u003e protobuf_net[\"protobuf-net:\\n\\nGenerate C# code\"]\n\tBytes[\"ProtoBuf bytes\"] --\u003e Des[\"protobuf-net:\\n\\nDeserialize ProtoBuf bytes to instance\\n(using the MessageType\\nand the object-type)\"]\n        MessageType[\"Message Type\"] --\u003e Des \n\tprotobuf_net --\u003e CodeCompile[\"Compile C# code\\nto Assembly\"]\n\tCodeCompile --\u003e CodeType[\"typeof(object)\"]\n        CodeType --\u003e Des\n\tDes --\u003e JSON2OBJ[\"Serialize instance\\nto JSON\"]\n```\n\n#### Code\n``` csharp\nvar protoDefinition = \"...\". // See above\n\nvar bytes = Convert.FromBase64String(\"CgRzdGVm\");\n\nvar request = new ConvertToJsonRequest(protoDefinition, \"greet.HelloRequest\", bytes);\n\nvar converter = new Converter();\n\nvar json = await converter.ConvertAsync(request);\n```\n\n#### JSON\n``` json\n{\"name\":\"stef\"}\n```\n\n### :two: Convert ProtoBuf `byte[]` to an object\n\n#### Code\n``` csharp\nvar protoDefinition = \"...\". // See above\n\nvar bytes = Convert.FromBase64String(\"CgRzdGVm\");\n\nvar request = new ConvertToObjectRequest(protoDefinition, \"greet.HelloRequest\", bytes);\n\nvar converter = new Converter();\n\nvar @object = await converter.ConvertAsync(request);\n```\n\n### :three: Convert JSON `string` to a ProtoBuf `byte[]`\n#### Code\n``` csharp\nvar protoDefinition = \"...\". // See above\n\nvar json = @\"{\"\"name\"\":\"\"stef\"\"}\";\n\nvar request = new ConvertToProtoBufRequest(protoDefinition, \"greet.HelloRequest\", json);\n\nvar converter = new Converter();\n\nvar protobuf = await converter.ConvertAsync(request);\n```\n\n### :four: Convert any `object` to a ProtoBuf `byte[]`\n\n``` mermaid\n---\ntitle: \"Convert an object to a ProtoBuf byte[]\"\n---\nflowchart LR\n\tDef[\"ProtoBuf Definition\\n(.proto)\"] --\u003e protobuf_net[\"protobuf-net:\\n\\nGenerate C# code\"]\n        protobuf_net --\u003e CodeCompile[\"Compile C# code\\nto Assembly\"]\n        CodeCompile --\u003e CodeType[\"typeof(object)\"]\n        CodeType --\u003e Ser\n\tObject[\"Object\"] --\u003e OBJ2JSON[\"Serialize object\\nto JSON\"]\n        OBJ2JSON --\u003e JSON2INS[\"Deserialize JSON to instance\\n(using the object-type)\"]\n        JSON2INS --\u003e Ser[\"protobuf-net:\\n\\nSerialize instance to ProtoBuf bytes\\n(using the MessageType)\"]\n        MessageType[\"Message Type\"] --\u003e Ser\n```\n\n#### Code\n``` csharp\nvar protoDefinition = \"...\". // See above\n\nvar obj = new\n{\n    name = \"stef\"\n};\n\nvar request = new ConvertToProtoBufRequest(protoDefinition, \"greet.HelloRequest\", obj);\n\nvar converter = new Converter();\n\nvar protobuf = await converter.ConvertAsync(request);\n```\n\n### :five: Convert any `object` to a ProtoBuf `byte[]` including the Grpc Header\n#### Code\n``` csharp\nvar protoDefinition = \"...\". // See above\n\nvar obj = new\n{\n    name = \"stef\"\n};\n\nvar request = new ConvertToProtoBufRequest(protoDefinition, \"greet.HelloRequest\", obj)\n    .WithGrpcHeader();\n\nvar converter = new Converter();\n\nvar protobufWithGrpcHeader = await ConvertAsync.Convert(request);\n```\n\n---\n\n## Using in Blazor WebAssembly\nIn order to use this library in a Blazor WebAssembly application, you need to provide a specific Blazor implementation for the `IMetadataReferenceService`, the [BlazorWasmMetadataReferenceService](https://github.com/StefH/ProtoBufJsonConverter/blob/main/examples/ProtoBufJsonConverter.Blazor/BlazorWasmMetadataReferenceService.cs).\n\n### Convert ProtoBuf `byte[]` to a JSON `string`\n\n#### Dependency Injection\n``` csharp\npublic class Program\n{\n    public static async Task Main(string[] args)\n    {\n        // ...\n\n        // Add AddSingleton registrations for the IMetadataReferenceService and IConverter\n        builder.Services.AddSingleton\u003cIMetadataReferenceService, BlazorWasmMetadataReferenceService\u003e();\n        builder.Services.AddSingleton\u003cIConverter, Converter\u003e();\n\n        await builder.Build().RunAsync();\n    }\n}\n```\n\n#### Blazor Page\n``` csharp\npublic partial class Home\n{\n    [Inject]\n    public required IConverter Converter { get; set; }\n\n    private State _state = State.None;\n    private string _protoDefinition = \"...\"; // See above\n    private string _messageType = \"greet.HelloRequest\";\n    private string _protobufAsBase64 = \"CgRzdGVm\";\n    private bool _skipGrpcHeader = true;\n    private bool _addGrpcHeader = true;\n    private string _json = string.Empty;\n\n    private async Task OnClick()\n    {\n        await ConvertToJsonAsync();\n    }\n\n    private async Task ConvertToJsonAsync()\n    {\n        _json = string.Empty;\n\n        var bytes = Convert.FromBase64String(_protobufAsBase64);\n\n        var convertToJsonRequest = new ConvertToJsonRequest(_protoDefinition, _messageType, bytes)\n            .WithSkipGrpcHeader(_skipGrpcHeader)\n            .WithWriteIndented();\n\n        _json = await Converter.ConvertAsync(convertToJsonRequest);\n    }\n}\n```\n\nFor a full example, see [examples/ProtoBufJsonConverter.Blazor](https://github.com/StefH/ProtoBufJsonConverter/tree/main/examples/ProtoBufJsonConverter.Blazor).\n\n### :six: Get information about the package names, message types and C# namespaces\n#### Code\n``` csharp\nvar protoDefinition = \"...\". // See above\n\nvar request = new GetInformationRequest(protoDefinition);\n\nvar response = await _sut.GetInformationAsync(request);\nvar packageNames = response.PackageNames;\nvar messageTypes = response.MessageTypes;\nvar namespaces = response.CSharpNamespaces;\n```\n\n---\n\n## :computer: Examples\n- [Blazor WASM](https://protobuf.heyenrath.nl)\n- [Blazor Static Web App](https://zealous-desert-029b2f003.4.azurestaticapps.net/)\n\n\n## :books: Resources\n- [protogen](https://protogen.marcgravell.com)\n- [protobufpal](https://www.protobufpal.com)\n- [protobuf-decoder](https://protobuf-decoder.netlify.app)\n\n- \n## Sponsors\n\n[Entity Framework Extensions](https://entityframework-extensions.net/?utm_source=StefH) and [Dapper Plus](https://dapper-plus.net/?utm_source=StefH) are major sponsors and proud to contribute to the development of **ProtoBufJsonConverter**.\n\n[![Entity Framework Extensions](https://raw.githubusercontent.com/StefH/resources/main/sponsor/entity-framework-extensions-sponsor.png)](https://entityframework-extensions.net/bulk-insert?utm_source=StefH)\n\n[![Dapper Plus](https://raw.githubusercontent.com/StefH/resources/main/sponsor/dapper-plus-sponsor.png)](https://dapper-plus.net/bulk-insert?utm_source=StefH)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstefh%2Fprotobufjsonconverter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstefh%2Fprotobufjsonconverter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstefh%2Fprotobufjsonconverter/lists"}