Próbuję zrozumieć, jak działa ProtoBuf.js
?Jak odkodować format Json dla plików proto za pomocą Protobuf.js?
Protobuf - https://github.com/dcodeIO/ProtoBuf.js
Mój plik proto gameSetting.proto
wygląda następująco:
message GameSettings {
required string gameName = 1;
repeated Category categories = 2;
}
message Category {
required int32 categoryId = 1;
required string categoryAbbreviation = 2;
required string categoryName = 3;
required string numberInSquad = 4;
required string numberInTeam = 5;
required string captain = 6;
}
tutaj, ja jestem wdrażaniu kodu JavaScript z dekodowania i kodowania danych:
var ProtoBuf = dcodeIO.ProtoBuf;
var protoURL = '../_doc/protobuf/gameSettings.proto';
var dataURL = '../xmlapi?type=game-settings&competitionid=1&outputtype=text';
var builder = ProtoBuf.loadProtoFile(protoURL, function(err, builder) {
// WE CAN ALSO TRY USING JSON FORMAT FOR THE PROTO FILES:
// https://github.com/dcodeIO/ProtoBuf.js/wiki/Builder#using-json-without-the-proto-parser
// encoding test
var GS_build = builder.build('GameSettings');
var GS = new GS_build({
"gameName": "games Test 2015",
"categories": [{
"categoryId": 1,
"categoryAbbreviation": "GK",
"categoryName": "Goalkeeper",
"numberInSquad": "2-2",
"numberInTeam": "1-1",
"captain": "0-1"
}]
});
// encode various types to test
var GS_buffer = GS.encode();
var GS_base64 = GS.encode64(); // this looks like something to use with AJAX
var GS_arrayBuffer = GS_buffer.toArrayBuffer();
// decoding test
var GS_buffer_decoded = GS_build.decode(GS_buffer);
var GS_buffer_decoded_raw = GS_buffer_decoded.toRaw();
var GS_base64_decoded = GS_build.decode64(GS_base64);
var GS_base64_decoded_raw = GS_base64_decoded.toRaw();
var GS_arrayBuffer_decoded = GS_build.decode(GS_arrayBuffer);
var GS_arrayBuffer_decoded_raw = GS_arrayBuffer_decoded.toRaw();
// decoding AJAX test
$.get(dataURL, function(data){
var GS = GS_build.decode(data);
});
});
Kod
// encoding test
i// decoding test
- działa dobrze.
Ale jest jakiś problem w kodzie:
var GS = GS_build.decode(data)
Jak mogę to naprawić?
To nie wygląda jak ten podpis istnieje funkcja w 6.8.0. Czy istnieje alternatywa lub planujesz dodać tę funkcję? –