Python 如何将具有numpy.int类型的dict编码为protobuf已知类型值?
考虑以下python dictPython 如何将具有numpy.int类型的dict编码为protobuf已知类型值?,python,json,protocol-buffers,proto,Python,Json,Protocol Buffers,Proto,考虑以下python dict json_obj = { "numpy_integer": np.int32(3), } 我想用protobuf编码它,使用众所周知的类型 导致以下错误值3具有意外类型 是否有方法指示proto编码器将numpy.int32识别为int 完全错误 $ python3 dummy.py Traceback (most recent call last): File "dummy.py", line 11, in
json_obj = {
"numpy_integer": np.int32(3),
}
我想用protobuf编码它,使用众所周知的类型
导致以下错误值3具有意外类型
是否有方法指示proto编码器将numpy.int32
识别为int
完全错误
$ python3 dummy.py
Traceback (most recent call last):
File "dummy.py", line 11, in <module>
proto_value = ParseDict(json_obj, Value())
File "/usr/local/lib/python3.8/site-packages/google/protobuf/json_format.py", line 454, in ParseDict
parser.ConvertMessage(js_dict, message)
File "/usr/local/lib/python3.8/site-packages/google/protobuf/json_format.py", line 483, in ConvertMessage
methodcaller(_WKTJSONMETHODS[full_name][1], value, message)(self)
File "/usr/local/lib/python3.8/site-packages/google/protobuf/json_format.py", line 642, in _ConvertValueMessage
self._ConvertStructMessage(value, message.struct_value)
File "/usr/local/lib/python3.8/site-packages/google/protobuf/json_format.py", line 675, in _ConvertStructMessage
self._ConvertValueMessage(value[key], message.fields[key])
File "/usr/local/lib/python3.8/site-packages/google/protobuf/json_format.py", line 654, in _ConvertValueMessage
raise ParseError('Value {0} has unexpected type {1}.'.format(
google.protobuf.json_format.ParseError: Value 3 has unexpected type <class 'numpy.int32'>.
完整代码:
import json
import numpy as np
from google.protobuf.json_format import Parse
from google.protobuf.json_format import ParseDict
from google.protobuf.struct_pb2 import Value
class NumpyAwareEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.int32):
return int(obj)
return super().default(obj)
json_obj = {
"numpy_integer": np.int32(3),
}
json_string = json.dumps(json_obj, cls=NumpyAwareEncoder)
proto_value_from_string = Parse(json_string, Value())
print(proto_value_from_string)
proto_value_from_dict = ParseDict(json_obj, Value())
print(proto_value_from_dict)
class NumpyAwareEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.int32):
return int(obj)
return super().default(obj)
json_string = json.dumps(json_obj, cls=NumpyAwareEncoder)
proto_value_from_string = Parse(json_string, Value())
print(proto_value_from_string)
import json
import numpy as np
from google.protobuf.json_format import Parse
from google.protobuf.json_format import ParseDict
from google.protobuf.struct_pb2 import Value
class NumpyAwareEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.int32):
return int(obj)
return super().default(obj)
json_obj = {
"numpy_integer": np.int32(3),
}
json_string = json.dumps(json_obj, cls=NumpyAwareEncoder)
proto_value_from_string = Parse(json_string, Value())
print(proto_value_from_string)
proto_value_from_dict = ParseDict(json_obj, Value())
print(proto_value_from_dict)