|
|
|
@ -37,12 +37,13 @@ Provide serialization and de-serialization of Google's protobuf Messages into/fr
|
|
|
|
|
# Note that preservation of unknown fields is currently not available for Python (c) google docs
|
|
|
|
|
# extensions is not supported from 0.0.5 (due to gpb2.3 changes)
|
|
|
|
|
|
|
|
|
|
__version__ = '0.0.5'
|
|
|
|
|
__author__ = 'Paul Dovbush <dpp@dpp.su>'
|
|
|
|
|
__version__='0.0.5'
|
|
|
|
|
__author__='Paul Dovbush <dpp@dpp.su>'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import json # py2.6+ TODO: add support for other JSON serialization modules
|
|
|
|
|
import json # py2.6+ TODO: add support for other JSON serialization modules
|
|
|
|
|
from google.protobuf.descriptor import FieldDescriptor as FD
|
|
|
|
|
import binascii
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ParseError(Exception): pass
|
|
|
|
@ -58,7 +59,7 @@ def json2pb(pb, js):
|
|
|
|
|
elif field.type in _js2ftype:
|
|
|
|
|
ftype = _js2ftype[field.type]
|
|
|
|
|
else:
|
|
|
|
|
raise ParseError("Field %s.%s of type '%d' is not supported" % (pb.__class__.__name__, field.name, field.type,))
|
|
|
|
|
raise ParseError("Field %s.%s of type '%d' is not supported" % (pb.__class__.__name__, field.name, field.type, ))
|
|
|
|
|
value = js[field.name]
|
|
|
|
|
if field.label == FD.LABEL_REPEATED:
|
|
|
|
|
pb_value = getattr(pb, field.name, None)
|
|
|
|
@ -76,17 +77,18 @@ def json2pb(pb, js):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def pb2json(pb, js={}):
|
|
|
|
|
def pb2json(pb):
|
|
|
|
|
''' convert google.protobuf.descriptor instance to JSON string '''
|
|
|
|
|
js = {}
|
|
|
|
|
# fields = pb.DESCRIPTOR.fields #all fields
|
|
|
|
|
fields = pb.ListFields() # only filled (including extensions)
|
|
|
|
|
for field, value in fields:
|
|
|
|
|
fields = pb.ListFields() #only filled (including extensions)
|
|
|
|
|
for field,value in fields:
|
|
|
|
|
if field.type == FD.TYPE_MESSAGE:
|
|
|
|
|
ftype = pb2json
|
|
|
|
|
elif field.type in _ftype2js:
|
|
|
|
|
ftype = _ftype2js[field.type]
|
|
|
|
|
else:
|
|
|
|
|
raise ParseError("Field %s.%s of type '%d' is not supported" % (pb.__class__.__name__, field.name, field.type,))
|
|
|
|
|
raise ParseError("Field %s.%s of type '%d' is not supported" % (pb.__class__.__name__, field.name, field.type, ))
|
|
|
|
|
if field.label == FD.LABEL_REPEATED:
|
|
|
|
|
js_value = []
|
|
|
|
|
for v in value:
|
|
|
|
@ -107,8 +109,8 @@ _ftype2js = {
|
|
|
|
|
FD.TYPE_FIXED32: float,
|
|
|
|
|
FD.TYPE_BOOL: bool,
|
|
|
|
|
FD.TYPE_STRING: unicode,
|
|
|
|
|
# FD.TYPE_MESSAGE: pb2json, #handled specially
|
|
|
|
|
FD.TYPE_BYTES: lambda x: x.encode('string_escape'),
|
|
|
|
|
#FD.TYPE_MESSAGE: pb2json, #handled specially
|
|
|
|
|
FD.TYPE_BYTES: lambda x: binascii.hexlify(x),
|
|
|
|
|
FD.TYPE_UINT32: int,
|
|
|
|
|
FD.TYPE_ENUM: int,
|
|
|
|
|
FD.TYPE_SFIXED32: float,
|
|
|
|
@ -128,7 +130,7 @@ _js2ftype = {
|
|
|
|
|
FD.TYPE_BOOL: bool,
|
|
|
|
|
FD.TYPE_STRING: unicode,
|
|
|
|
|
# FD.TYPE_MESSAGE: json2pb, #handled specially
|
|
|
|
|
FD.TYPE_BYTES: lambda x: x.decode('string_escape'),
|
|
|
|
|
FD.TYPE_BYTES: lambda x: binascii.unhexlify(x),
|
|
|
|
|
FD.TYPE_UINT32: int,
|
|
|
|
|
FD.TYPE_ENUM: int,
|
|
|
|
|
FD.TYPE_SFIXED32: float,
|
|
|
|
|