【问题标题】:Invalid JSON reported when using unsigned integers in ctype structure在 ctype 结构中使用无符号整数时报告无效 JSON
【发布时间】:2020-01-25 21:16:09
【问题描述】:

使用 Python 2.6,我创建了一个嵌套的 ctypes 结构如下:

class TestStruct1(BigEndianStructure):
_pack_ = 1
_fields_ = [
    ("temp1", c_uint32),
    ("temp2", c_uint32)
]

class TestStruct2(BigEndianStructure):
    _pack_ = 1
    _fields_ = [
        ("temp1", c_uint32),
        ("temp2", c_uint32)
    ]

class TestStruct3(BigEndianStructure):
    _pack_ = 1
    _fields_ = [
        ("TestStruct1", TestStruct1),
        ("TestStruct2", TestStruct2)
    ]

然后我将数据推送到这个结构中:

elements = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF]
data = bytes(elements)

foo = TestStruct3()
memmove(addressof(foo), data, sizeof(foo))

我可以将值一个一个打印出来,得到预期的输出:

print(format(foo.TestStruct1.temp1, '08X'))
print(format(foo.TestStruct1.temp2, '08X'))
print(format(foo.TestStruct2.temp1, '08X'))
print(format(foo.TestStruct2.temp2, '08X'))

00112233 44556677 8899AABB CCDDEEFF

但想利用 json.dumps 以 JSON 格式转储结构。这在 Python 3 中运行良好,如下所示:

https://onlinegdb.com/rJvQ0fFvS

但在 Python 2.6 中为无符号类型返回以下错误:

encoder.py",第 184 行,默认 raise TypeError(repr(o) + "不是 JSON 可序列化") TypeError: 1122867L 不是 JSON 可序列化的

我猜它被 1122867 中附加的“L”弄糊涂了L (0x00112233)

我是否必须在我的 JSONEncoder 类中添加某种无符号类型?

下面是我创建的一个显示问题的示例。运行脚本的主机上的字节序不同,因此输出不匹配,但它至少显示了操作中的错误:

https://rextester.com/UAWZK83752

任何帮助将不胜感激。

谢谢!

【问题讨论】:

    标签: python ctypes


    【解决方案1】:

    希望这对其他人有所帮助。再调试一下,我最终找到了错误对应的文件(encoder.py),发现我在没有处理实例的情况下调用了 JSONEncoder.default。在这种情况下,我假设 c_uint32 是 int 的一个实例:

    class ReportEncoder(JSONEncoder):
    
        def default(self, obj):
    
            if isinstance(obj, (Array, list)):
                return [self.default(e) for e in obj]
    
            if isinstance(obj, _Pointer):
                return self.default(obj.contents) if obj else None
    
            if isinstance(obj, _SimpleCData):
                return self.default(obj.value)
    
            if isinstance(obj, (bool, int, float, str)):
                return obj
    
            if obj is None:
                return obj
    
            if isinstance(obj, (Structure, Union)):
                result = {}
                anonymous = getattr(obj, '_anonymous_', [])
    
                for key, _ in getattr(obj, '_fields_', []):
                    value = getattr(obj, key)
    
                    # private fields don't encode
                    if key.startswith('_'):
                        continue
    
                    if key in anonymous:
                        result.update(self.default(value))
                    else:
                        result[key] = self.default(value)
    
                return result
    
            return JSONEncoder.default(self, obj)
    

    但显然 c_uint32 在 long 的实例中,而不是 int。我在实例列表中添加了 long:

    if isinstance(obj, (bool, int, float, long, str)):
        return obj
    

    一切都从那里开始。完整的 JSONEncoder 类可以在下面找到:

    class ReportEncoder(JSONEncoder):
    
        def default(self, obj):
    
            if isinstance(obj, (Array, list)):
                return [self.default(e) for e in obj]
    
            if isinstance(obj, _Pointer):
                return self.default(obj.contents) if obj else None
    
            if isinstance(obj, _SimpleCData):
                return self.default(obj.value)
    
            if isinstance(obj, (bool, int, float, long, str)):
                return obj
    
            if obj is None:
                return obj
    
            if isinstance(obj, (Structure, Union)):
                result = {}
                anonymous = getattr(obj, '_anonymous_', [])
    
                for key, _ in getattr(obj, '_fields_', []):
                    value = getattr(obj, key)
    
                    # private fields don't encode
                    if key.startswith('_'):
                        continue
    
                    if key in anonymous:
                        result.update(self.default(value))
                    else:
                        result[key] = self.default(value)
    
                return result
    
            return JSONEncoder.default(self, obj)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      相关资源
      最近更新 更多