Dominick
Dominick

Reputation: 476

Using Protobuf-net .proto schema in Angular

I have an existing Web API project, which after running some tests, I want to start implementing protobuf. Using the Serializer.GetProto<>() method, I got the following data (showing just the beginning):

syntax = "proto2";
package MyProject.Models;
import "protobuf-net/bcl.proto"; // schema for protobuf-net's handling of core .NET types

message AssemblyComponentDetailsModel {
   optional .bcl.Guid ID = 1 [default = 00000000-0000-0000-0000-000000000000];
   optional string AssemblyName = 2;
   optional string ReferenceNumber = 3;
   optional .bcl.Guid TypeID = 4 [default = 00000000-0000-0000-0000-000000000000];
   optional .bcl.Guid CategoryID = 5 [default = 00000000-0000-0000-0000-000000000000];
   optional string Symbol = 6;
   repeated ComponentDetailsModel ComponentDetails = 7;
   optional .bcl.Decimal MsrpTotal = 8 [default = 0];
}

The frontend of the project is using Angular 7 and I am using the information on this page to implement it: https://medium.com/francesco-pongetti/using-protocol-buffers-in-a-node-js-angular-web-application-fba17df8ab51.

My question is, the proto file has the import statement import "protobuf-net/bcl.proto" and items from this file (e.g. optional .bcl.Guid) are being used. Is this going to cause issue deserializing in Angular? If so, what is the easiest way to go about fixing this? There are a lot of classes in the project and I am trying to avoid having to try and write all the .proto files by hand.

Any guidance/suggestions are greatly appreciated as this is the first time I'm working with protobuf on a real project.

Thank you in advance.

Upvotes: 2

Views: 1072

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1063338

In theory it should just work fine - bcl.proto is a perfectly normal .proto schema - however, the types in bcl.proto essentially describe how I hacked .NET types into protobuf. Where possible, I would recommend avoiding that when possible - for example, DateTime and TimeSpan in bcl.proto work but are incredibly awkward to work with if you're not in .NET, but protobuf-net also allows you to use the Google types that were added later: Duration and Timestamp. The reason protobuf-net doesn't default to these is that Google added them much later than protobuf-net's support. To use this option (on the types where it works, which is limited): use:

[ProtoMember(n, DataFormat = DataFormat.WellKnown)]

on the property/field. GetProto/GetSchema will automatically emit the correct import statements to match the schema you're generating.

However, in the case of Guid and decimal, I'd say the options are less clear. For Guid, I'd be tempted to use string if possible - the reality is that guids have so many ambiguities in the binary representation, that using a simple string is the most pragmatic option. And decimal is very unfriendly to non-.NET platforms: if possible, consider using double - or if you need to avoid floating point: either use fixed-point via a manually scaled integer, or again: use a string.

Upvotes: 1

Related Questions