Giter Site home page Giter Site logo

pbjson's Introduction

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.

pbjson's People

Stargazers

Dan Wang avatar  avatar George Bikas avatar Piotr Justyna avatar Vladislav Botvin avatar  avatar  avatar haiyangding avatar  avatar Andy Britcliffe avatar

Watchers

James Cloos avatar aoeuidht avatar

Forkers

jbregli

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.