簡體   English   中英

ProtobufJS重復字節字段未正確編碼Uint8Array

[英]ProtobufJS repeated bytes field not encoding Uint8Array properly

我有一個protobuf消息定義為:

message examplemessage
{
    string field1 = 1;
    string field2 = 2;
    repeated bytes field3 = 3;
}

我用以下方法加載protobuf:

protobuf.load(path).then(root => {
    // global for example
    examplemessage = root.lookupType("test.examplemessage");
    resolve(); 
});

我用創建一個protobuf消息對象

let createdMessage = examplemessage.create({
    field1: "test1",
    field2: "test2",
    field3: new Uint8Array([0,0,0,33])
});

然后我將其編碼

let encoded = examplemessage.encode(createdMessage).finish();

然后我解碼並期望

{
    field1: "test1",
    field2: "test2",
    field3: Uint8Array(4) [0, 0, 0, 33]
}

相反,我看到了

{
    field1: "test1",
    field2: "test2",
    [Uint8Array(0), Uint8Array(0), Uint8Array(0), Uint8Array(0)]
}

然后,我將protobuf加載更改為JSON

const root = protobuf.Root.fromJSON(json);

可以正常運行,沒有其他更改。

我是在做錯什么還是錯誤?

謝謝

Protoubuf版本:6.8.6

瀏覽器:Chrome

具有JSON加載功能的JSFiddle示例: https ://jsfiddle.net/740snmu6/12/

repeated bytes表示一個Array字節 (正如你可能已經知道, 字節對應的類型是Uint8ArrayArray中的JavaScript),因此使其工作,你應該以這種方式創建消息:

 { field1: "test1", field2: "test2", field3: [new Uint8Array([0, 0, 0, 33]), new Uint8Array([0, 0, 0, 33]), new Uint8Array([0, 0, 0, 33])] } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM