ProtoBufJsonConverter 0.10.0.1

ProtoBufJsonConverter

This project uses protobuf-net to:

  • Convert a protobuf message to a JSON string using the proto definition file.
  • Convert a protobuf message to an object using the proto definition file.
  • Convert a JSON string or an object to a protobuf message using the proto definition file.
  • Get information about the package names, message types and C# namespaces in the proto definition file.

Usage

Proto Definition

syntax = "proto3";

// Package name
package greet;

// The greeting service definition.
service Greeter {
  rpc SayHello (HelloRequest) returns (HelloReply);
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

:one: Convert ProtoBuf byte[] to a JSON string

Code
var protoDefinition = "...". // See above

var bytes = Convert.FromBase64String("CgRzdGVm");

var request = new ConvertToJsonRequest(protoDefinition, "greet.HelloRequest", bytes);

var converter = new Converter();

var json = await converter.ConvertAsync(request);
JSON
{"name":"stef"}

:two: Convert ProtoBuf byte[] to an object

Code
var protoDefinition = "...". // See above

var bytes = Convert.FromBase64String("CgRzdGVm");

var request = new ConvertToObjectRequest(protoDefinition, "greet.HelloRequest", bytes);

var converter = new Converter();

var @object = await converter.ConvertAsync(request);

:three: Convert JSON string to a ProtoBuf byte[]

Code
var protoDefinition = "...". // See above

var json = @"{""name"":""stef""}";

var request = new ConvertToProtoBufRequest(protoDefinition, "greet.HelloRequest", json);

var converter = new Converter();

var protobuf = await converter.ConvertAsync(request);

:four: Convert any object to a ProtoBuf byte[]

Code
var protoDefinition = "...". // See above

var obj = new
{
    name = "stef"
};

var request = new ConvertToProtoBufRequest(protoDefinition, "greet.HelloRequest", obj);

var converter = new Converter();

var protobuf = await converter.ConvertAsync(request);

:five: Convert any object to a ProtoBuf byte[] including the Grpc Header

Code
var protoDefinition = "...". // See above

var obj = new
{
    name = "stef"
};

var request = new ConvertToProtoBufRequest(protoDefinition, "greet.HelloRequest", obj)
    .WithGrpcHeader();

var converter = new Converter();

var protobufWithGrpcHeader = await ConvertAsync.Convert(request);

Using in Blazor WebAssembly

In order to use this library in a Blazor WebAssembly application, you need to provide a specific Blazor implementation for the IMetadataReferenceService, the BlazorWasmMetadataReferenceService.

Convert ProtoBuf byte[] to a JSON string

Dependency Injection
public class Program
{
    public static async Task Main(string[] args)
    {
        // ...

        // Add AddSingleton registrations for the IMetadataReferenceService and IConverter
        builder.Services.AddSingleton<IMetadataReferenceService, BlazorWasmMetadataReferenceService>();
        builder.Services.AddSingleton<IConverter, Converter>();

        await builder.Build().RunAsync();
    }
}
Blazor Page
public partial class Home
{
    [Inject]
    public required IConverter Converter { get; set; }

    private State _state = State.None;
    private string _protoDefinition = "..."; // See above
    private string _messageType = "greet.HelloRequest";
    private ConvertType _selectedConvertType = ConvertType.ToJson;
    private string _protobufAsBase64 = "CgRzdGVm";
    private bool _skipGrpcHeader = true;
    private bool _addGrpcHeader = true;
    private string _json = string.Empty;


    private async Task OnClick()
    {
        await ConvertToJsonAsync();
    }

    private async Task ConvertToJsonAsync()
    {
        _json = string.Empty;

        var bytes = Convert.FromBase64String(_protobufAsBase64);

        var convertToJsonRequest = new ConvertToJsonRequest(_protoDefinition, _messageType, bytes)
            .WithSkipGrpcHeader(_skipGrpcHeader)
            .WithWriteIndented();

        _json = await Converter.ConvertAsync(convertToJsonRequest);
    }
}

For a full example, see examples/ProtoBufJsonConverter.Blazor.

:six: Get information about the package names, message types and C# namespaces

Code
var protoDefinition = "...". // See above

var request = new GetInformationRequest(protoDefinition);

var response = await _sut.GetInformationAsync(request);
var packageNames = response.PackageNames;
var messageTypes = response.MessageTypes;
var namespaces = response.CSharpNamespaces;

:computer: Examples

:books: Resources

Sponsors

Entity Framework Extensions and Dapper Plus are major sponsors and proud to contribute to the development of ProtoBufJsonConverter.

Entity Framework Extensions

Dapper Plus

No packages depend on ProtoBufJsonConverter.

See CHANGELOG.md

Version Downloads Last updated
0.10.0.1 2 10.07.2025
0.10.0 4 04.06.2025
0.9.0 5 02.06.2025
0.8.0 5 02.06.2025
0.8.0-preview-01 2 03.06.2025
0.7.0 7 19.03.2025
0.7.0-preview-02 4 19.03.2025
0.7.0-preview-01 5 19.03.2025
0.6.0 6 19.03.2025
0.6.0-preview-03 4 19.03.2025
0.6.0-preview-02 4 19.03.2025
0.6.0-preview-01 4 19.03.2025
0.5.0 6 19.03.2025
0.4.0 6 19.03.2025
0.3.0 6 19.03.2025
0.2.0 6 19.03.2025
0.1.0 5 19.03.2025
0.0.6 6 19.03.2025
0.0.5 6 19.03.2025
0.0.4 6 19.03.2025
0.0.3 6 19.03.2025
0.0.2 5 19.03.2025