.NETFramework4.6.2net6.0.NETStandard2.0.NETStandard2.1
Convert a protobuf message to a JSON string or object (and back) using the proto definition file.
Published
Dec 28, 2023
2y ago
Total Downloads
27.9M
27,905,610
Install
dotnet add package ProtoBufJsonConverter --version 0.0.6
| Product | Versions |
|---|---|
| .NET | |
| .NET Standard | |
| Other |
README
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;
}
: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 = converter.Convert(request);
JSON
{"name":"stef"}
:one: 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 = converter.Convert(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 = converter.Convert(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 = converter.Convert(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 = converter.Convert(request);
Changes from Previous Version
No structural changes between v0.0.5 and v0.0.6.
Adoption Assessment
Assess this package against its supported frameworks, license, maintenance signals and dependency graph before making it a project standard.
Compatibility metadata lists 4 target frameworks; confirm the exact target used by your application.The published metadata declares 24 direct dependency entries across framework groups. Inspect the resolved dependency graph for your target framework.
Related Guides & Workflows
Learn how to evaluate, integrate, or audit this package in your .NET project.