ProtoBufJsonConverter 0.2.0-preview-04

This is a prerelease version of ProtoBufJsonConverter.
There is a newer version of this package available.
See the version list below for details.
dotnet add package ProtoBufJsonConverter --version 0.2.0-preview-04                
NuGet\Install-Package ProtoBufJsonConverter -Version 0.2.0-preview-04                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ProtoBufJsonConverter" Version="0.2.0-preview-04" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ProtoBufJsonConverter --version 0.2.0-preview-04                
#r "nuget: ProtoBufJsonConverter, 0.2.0-preview-04"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ProtoBufJsonConverter as a Cake Addin
#addin nuget:?package=ProtoBufJsonConverter&version=0.2.0-preview-04&prerelease

// Install ProtoBufJsonConverter as a Cake Tool
#tool nuget:?package=ProtoBufJsonConverter&version=0.2.0-preview-04&prerelease                

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.

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;
}

1️⃣ 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"}

1️⃣ 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);

3️⃣ 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);

4️⃣ 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);

5️⃣ 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)
            .WithJsonConverterOptions(new JsonConverterOptions { WriteIndented = true });

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

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


Examples

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on ProtoBufJsonConverter:

Package Downloads
WireMock.Net

Lightweight Http Mocking Server for .Net, inspired by WireMock from the Java landscape.

WireMock.Net.ProtoBuf

ProtoBuf and gRPC support for WireMock.Net

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on ProtoBufJsonConverter:

Repository Stars
WireMock-Net/WireMock.Net
WireMock.Net is a flexible product for stubbing and mocking web HTTP responses using advanced request matching and response templating. Based on the functionality from http://WireMock.org, but extended with more functionality.
Version Downloads Last updated
0.5.0 108,960 10/17/2024
0.5.0-preview-01 74 10/17/2024
0.4.0 129 10/15/2024
0.4.0-preview-09 82 10/14/2024
0.4.0-preview-08 149 9/23/2024
0.4.0-preview-07 133 9/4/2024
0.4.0-preview-06 106 9/3/2024
0.4.0-preview-05 82 9/2/2024
0.4.0-preview-04 82 9/2/2024
0.4.0-preview-03 74 9/2/2024
0.4.0-preview-02 125 8/5/2024
0.4.0-preview-01 56 8/4/2024
0.3.0 2,404,041 3/6/2024
0.2.0 204,554 2/9/2024
0.2.0-preview-04 134 1/31/2024
0.2.0-preview-03 91 1/27/2024
0.2.0-preview-01 94 1/25/2024
0.1.0 229 1/6/2024
0.0.6 320 12/28/2023
0.0.5 176 12/27/2023
0.0.4 184 12/25/2023
0.0.3 178 12/23/2023
0.0.2 141 12/22/2023

See CHANGELOG.md