Skip to content

aoeuidht/pbjson

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

pbjson

About

pbjson is a util for json - protocol buffers converting written in python.

This repo is part of my work when I was asked to compare data-size and cpu usage during decoding/unserializing(for no reason). It's kind of strange to compare between schema based protocol and self-explained protocol. Anyway, I write two scripts. One to pasrse json source file and generate .proto file and convert json(dict) to pb msg using another.

Usage

There are two python source files in this repo:

  1. schema_parse.py
  2. pb.py

generate .proto file

The first schema_parse can parse json string(saved in a file) and generate a .proto file automatically. Just put your .json file as the first commandline parameter like this:

python schema_parse.py PATH_TO_JSON_FILE

The .proto file will printed out to STDOUT(whit out formating). You may want to save it to disk using redirect.

convert .proto file to python class definition

When you got the .proto file(let's say msg.proto), you can generate a python source file using protoc by this command

protoc -I=. --python_out=. ./msg.proto

The default generated python sourec is msg_pb2.py, you can see how the msg class defined.

Assign python dict to pb msg

pb.py import file ``msg_pb2.py'' generated by step 2 and assign json object to protocol buffer message recursively. To assign json to msg object, json file should be the first commander line parameter

python pb.py PATH_TO_JSON_FILE

Summary

A bash script ``test.sh'' shows how the whole util used.

About

Json to protocol buffers proto file and pb msg object

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages