带有.NET类型的Protobuf.js .toObject()

时间:2017-02-27 03:51:02

标签: javascript .net protocol-buffers protobuf-net protobuf.js

我使用了protobuf-net(服务器)和protobuf.js(浏览器客户端)的组合。

我有这样的.proto:

package Testy;
import "bcl.proto"; // schema for protobuf-net's handling of core .NET types

message Foo {
    optional int32 ID = 1 (default = 0);
    optional bcl.Decimal Bar = 2 (default = 0);
}

当我解码传入的Foo ArrayBuffer并调用.toObject()时,如何插入bcl.Decimal转换器?

var testyPackage = "package Testy; import ... (continued)";
var bclPackage = "package bcl; message TimeSpan ... (continued)",
    protoRoot = new protobuf.Root();
protobuf.parse(bclPackage, protoRoot);
protobuf.parse(testyPackage, protoRoot);

var buffer = new Uint8Array(someArrayBufferSomewhere..),
    msg = protoRoot.Testy.Foo.decode(buffer);


function convertsBclDecimalToNumber(bclDecimal) {
    ...
}

var obj = msg.toObject({Decimal: convertsBclDecimalToNumber}) //This won't work

1 个答案:

答案 0 :(得分:0)

这是我如何做到的,但这是图书馆的意图吗?

var x = protoRoot.Testy.Foo.decode(buffer);
protoRoot.lookup("bcl.Decimal").toObject = myBclDecimalConverterFunctionReference
return x.toObject();