@@ -1044,7 +1044,7 @@ def FromString(cls: Type[T], data: bytes) -> T:
1044
1044
return cls ().parse (data )
1045
1045
1046
1046
def to_dict (
1047
- self , casing : Casing = Casing .CAMEL , include_default_values : bool = False
1047
+ self , casing : Casing = Casing .CAMEL , include_default_values : bool = False , b64 : bool = False
1048
1048
) -> Dict [str , Any ]:
1049
1049
"""
1050
1050
Returns a JSON serializable dict representation of this object.
@@ -1143,12 +1143,12 @@ def to_dict(
1143
1143
elif meta .proto_type == TYPE_BYTES :
1144
1144
if field_is_repeated :
1145
1145
output [cased_name ] = [
1146
- b64encode (b ).decode ("utf8" ) for b in value
1146
+ b if not b64 else b64encode (b ).decode ("utf8" ) for b in value
1147
1147
]
1148
1148
elif value is None and include_default_values :
1149
1149
output [cased_name ] = value
1150
1150
else :
1151
- output [cased_name ] = b64encode (value ).decode ("utf8" )
1151
+ output [cased_name ] = v if not b64 else b64encode (value ).decode ("utf8" )
1152
1152
elif meta .proto_type == TYPE_ENUM :
1153
1153
if field_is_repeated :
1154
1154
enum_class = field_types [field_name ].__args__ [0 ]
@@ -1264,7 +1264,7 @@ def to_json(
1264
1264
self ,
1265
1265
indent : Union [None , int , str ] = None ,
1266
1266
include_default_values : bool = False ,
1267
- casing : Casing = Casing .CAMEL ,
1267
+ casing : Casing = Casing .CAMEL
1268
1268
) -> str :
1269
1269
"""A helper function to parse the message instance into its JSON
1270
1270
representation.
@@ -1293,7 +1293,7 @@ def to_json(
1293
1293
The JSON representation of the message.
1294
1294
"""
1295
1295
return json .dumps (
1296
- self .to_dict (include_default_values = include_default_values , casing = casing ),
1296
+ self .to_dict (include_default_values = include_default_values , casing = casing , b64 = True ),
1297
1297
indent = indent ,
1298
1298
)
1299
1299
0 commit comments