Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 from google.protobuf.descriptor import FieldDescriptor as fd | |
| 6 | |
| 7 def convert(pb): | |
| 8 """Convert protobuf to plain-old-python-object""" | |
| 9 obj = {} | |
| 10 for field, value in pb.ListFields(): | |
| 11 if field.label == fd.LABEL_REPEATED: | |
| 12 obj[field.name] = list(_FD_TO_JSON[field.type](v) for v in value) | |
|
Sergey Berezin
2016/07/01 22:05:21
nit: just to be on the safe side, I'd use _FD_TO_J
tnn
2016/07/01 22:31:39
If a proto changes, crashing an application is bet
Sergey Berezin
2016/07/06 17:18:39
For application code a crash may be preferred in s
tnn
2016/07/06 19:28:06
Fair enough. I'll use unicode and log something he
| |
| 13 else: | |
| 14 obj[field.name] = _FD_TO_JSON[field.type](value) | |
| 15 return obj | |
| 16 | |
| 17 | |
| 18 _FD_TO_JSON = { | |
| 19 fd.TYPE_BOOL: bool, | |
| 20 fd.TYPE_DOUBLE: float, | |
| 21 fd.TYPE_ENUM: int, | |
| 22 fd.TYPE_FIXED32: float, | |
| 23 fd.TYPE_FIXED64: float, | |
| 24 fd.TYPE_FLOAT: float, | |
| 25 fd.TYPE_INT32: int, | |
| 26 fd.TYPE_INT64: long, | |
| 27 fd.TYPE_SFIXED32: float, | |
| 28 fd.TYPE_SFIXED64: float, | |
| 29 fd.TYPE_SINT32: int, | |
| 30 fd.TYPE_SINT64: long, | |
| 31 fd.TYPE_STRING: unicode, | |
| 32 fd.TYPE_UINT32: int, | |
| 33 fd.TYPE_UINT64: long, | |
| 34 fd.TYPE_MESSAGE: convert | |
| 35 } | |
| OLD | NEW |