Difference between revisions of "Protobuf"
(→Python) |
|||
Line 53: | Line 53: | ||
}) |
}) |
||
</pre> |
</pre> |
||
− | ''Décodage :'' |
+ | '''Décodage :''' |
<pre> |
<pre> |
||
var obj = messages.Test.decode(buf) |
var obj = messages.Test.decode(buf) |
||
Line 64: | Line 64: | ||
==Python== |
==Python== |
||
+ | ===Installation=== |
||
− | Un guide d'installation détaillé se trouve [https://github.com/google/protobuf/tree/master/python ici] |
+ | Un guide d'installation détaillé se trouve [https://github.com/google/protobuf/tree/master/python ici]. |
+ | |||
+ | ===Utilisation=== |
||
+ | <pre> |
||
+ | </pre> |
||
https://developers.google.com/protocol-buffers/docs/pythontutorial |
https://developers.google.com/protocol-buffers/docs/pythontutorial |
||
Line 72: | Line 77: | ||
=Conversion= |
=Conversion= |
||
+ | |||
+ | Il existe plusieurs méthode et outils de conversion des 'JSON'' en ''ProtoBuf''.<br> |
||
+ | |||
+ | ==''ProtoBuf'' to ''JSON''== |
||
+ | |||
+ | Une méthode de conversion de ''ProtoBuf'' vers ''JSON'' consiste à utiliser [https://code.google.com/archive/p/protobuf-java-format/ protobuf-java-format] ainsi : |
||
+ | <pre> |
||
+ | JsonFormat.printToString(protoMessage) |
||
+ | </pre> |
||
+ | Protobuf <--> JSON, XML : https://code.google.com/archive/p/protobuf-java-format/ |
||
+ | |||
+ | |||
+ | ==''JSON'' to ''ProtoBuf''== |
||
+ | |||
+ | Une méthode de conversion de ''ProtoBuf'' vers ''JSON'' consiste à utiliser [https://code.google.com/archive/p/protobuf-java-format/ protobuf-java-format] |
||
Protobuf <--> JSON, XML : https://code.google.com/archive/p/protobuf-java-format/ |
Protobuf <--> JSON, XML : https://code.google.com/archive/p/protobuf-java-format/ |
Revision as of 13:07, 12 April 2016
Sérialisateur de messages (C++, Java, Node.js, ...)
Alternative : BSON, Avro, Thrift, Cap’n Proto, Msgpack
http://fr.slideshare.net/IgorAnishchenko/pb-vs-thrift-vs-avro
API
ProtoBuf ou Protocol Buffers est un sérialisateur de structures de données. Il implique dans un premier temps, l'utilisation d'une "Interface de Description de Message" qui décrit la structure de données à sérialiser. Il implique dans un second temps, l'utilisation d'un programme (compilateur) générant le code source à partir de ces description. Ce code source est utilisé pour créer et parser des flux de données au format décrit par les messages.
Exemple de description de Message :
message Luminosité { required int intensité = 50; // La propiété "intensité" est obligatoire. optional string type = "naturelle"; // La propiété "type" est optionnelle. } message Humidité { required double taux = 60.0; // La propiété "taux" est obligatoire. optional string lieu = "sol"; // La propiété "lieu" est optionnelle. }
La conception de Protobuf par Google s'est axée sur un usage interne. Cependant plusieurs compilateurs pour différents langages (C++, C#, Python, Java, Ruby, NodeJS, etc.) ont été développé et mis à disposition sous licence gratuite. Voici quelques exemples d'usages pour les langages Node.js, Python et Java.
Node.js
Installation
Rien de plus simple pour l'installer:
npm install protocol-buffers
Utilisation
Soit un fichier test.proto
message Luminosité { required int intensité = 50; // La propiété "intensité" est obligatoire. optional string type = "solaire"; // La propiété "type" est optionnelle. }
Encodage :
var protobuf = require('protocol-buffers') var messages = protobuf(fs.readFileSync('test.proto')) var buf = messages.Test.encode({ intensité: 42, type: 'néon' })
Décodage :
var obj = messages.Test.decode(buf) console.log(obj) // devrait afficher un objet similaire à celui du dessus
Voici quelques liens utiles pour d'autres exemples https://github.com/mafintosh/protocol-buffers https://www.npmjs.org/package/node-protobuf
Python
Installation
Un guide d'installation détaillé se trouve ici.
Utilisation
https://developers.google.com/protocol-buffers/docs/pythontutorial
Java
With Maven : http://stackoverflow.com/questions/1578456/integrate-protocol-buffers-into-maven2-build
Conversion
Il existe plusieurs méthode et outils de conversion des 'JSON en ProtoBuf.
ProtoBuf to JSON
Une méthode de conversion de ProtoBuf vers JSON consiste à utiliser protobuf-java-format ainsi :
JsonFormat.printToString(protoMessage)
Protobuf <--> JSON, XML : https://code.google.com/archive/p/protobuf-java-format/
JSON to ProtoBuf
Une méthode de conversion de ProtoBuf vers JSON consiste à utiliser protobuf-java-format Protobuf <--> JSON, XML : https://code.google.com/archive/p/protobuf-java-format/